summaryrefslogtreecommitdiff
path: root/src/syntax/span.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-08-21 16:38:51 +0200
committerLaurenz <laurmaedje@gmail.com>2021-08-21 17:30:21 +0200
commit0dd4ae0a7ac0c247078df492469ff20b8a90c886 (patch)
tree07a55343b9ccab3fe76b0f1b0de9d1be310d8b14 /src/syntax/span.rs
parentf38eb10c2b54bd13ccef119454839f6a66448462 (diff)
Prune derives
Diffstat (limited to 'src/syntax/span.rs')
-rw-r--r--src/syntax/span.rs42
1 files changed, 25 insertions, 17 deletions
diff --git a/src/syntax/span.rs b/src/syntax/span.rs
index e4a4fd32..bfb9e755 100644
--- a/src/syntax/span.rs
+++ b/src/syntax/span.rs
@@ -1,3 +1,4 @@
+use std::cmp::Ordering;
use std::fmt::{self, Debug, Formatter};
use std::ops::{Add, Range};
@@ -6,8 +7,7 @@ use serde::{Deserialize, Serialize};
use crate::source::SourceId;
/// A value with the span it corresponds to in the source code.
-#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
-#[derive(Serialize, Deserialize)]
+#[derive(Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Spanned<T> {
/// The spanned value.
pub v: T,
@@ -48,8 +48,7 @@ impl<T: Debug> Debug for Spanned<T> {
}
/// Bounds of a slice of source code.
-#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
-#[derive(Serialize, Deserialize)]
+#[derive(Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Span {
/// The id of the source file.
pub source: SourceId,
@@ -127,9 +126,18 @@ impl Debug for Span {
}
}
+impl PartialOrd for Span {
+ fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
+ if self.source == other.source {
+ Some(self.start.cmp(&other.start).then(self.end.cmp(&other.end)))
+ } else {
+ None
+ }
+ }
+}
+
/// A byte position in source code.
-#[derive(Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
-#[derive(Serialize, Deserialize)]
+#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
pub struct Pos(pub u32);
impl Pos {
@@ -148,17 +156,6 @@ impl Debug for Pos {
}
}
-impl<T> Add<T> for Pos
-where
- T: Into<Pos>,
-{
- type Output = Self;
-
- fn add(self, rhs: T) -> Self {
- Pos(self.0 + rhs.into().0)
- }
-}
-
impl From<u32> for Pos {
fn from(index: u32) -> Self {
Self(index)
@@ -171,6 +168,17 @@ impl From<usize> for Pos {
}
}
+impl<T> Add<T> for Pos
+where
+ T: Into<Pos>,
+{
+ type Output = Self;
+
+ fn add(self, rhs: T) -> Self {
+ Pos(self.0 + rhs.into().0)
+ }
+}
+
/// Convert a position or range into a span.
pub trait IntoSpan {
/// Convert into a span by providing the source id.