diff options
| author | Laurenz <laurmaedje@gmail.com> | 2020-08-15 15:01:55 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2020-08-15 15:01:55 +0200 |
| commit | 84f30fb73518ca23cbc728b1bf414e80b344412a (patch) | |
| tree | 64e313f540f0e174522c60b8d6ba4cde34d0c60e /src/syntax/span.rs | |
| parent | eb9c4b1a49c90e687d70e7bd712848d78ffbd909 (diff) | |
Remove SpanlessEq 🎃
The new solution is slightly hacky, but way more flexible. All types that implement PartialEq can now be compared spanlessly in tests by manipulating a thread-local boolean that is read in Span's PartialEq implementation.
Diffstat (limited to 'src/syntax/span.rs')
| -rw-r--r-- | src/syntax/span.rs | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/src/syntax/span.rs b/src/syntax/span.rs index c93f5c1e..923d13c2 100644 --- a/src/syntax/span.rs +++ b/src/syntax/span.rs @@ -3,9 +3,23 @@ use std::fmt::{self, Debug, Formatter}; use std::ops::{Add, Sub}; +#[cfg(test)] +use std::cell::Cell; + #[cfg(feature = "serialize")] use serde::Serialize; +#[cfg(test)] +thread_local! { + static CMP_SPANS: Cell<bool> = Cell::new(true); +} + +/// When set to `false` comparisons with `PartialEq` ignore spans. +#[cfg(test)] +pub(crate) fn set_cmp(cmp: bool) { + CMP_SPANS.with(|cell| cell.set(cmp)); +} + /// Span offsetting. pub trait Offset { /// Offset all spans contained in `Self` by the given position. @@ -85,7 +99,7 @@ impl<T: Debug> Debug for Spanned<T> { } /// Locates a slice of source code. -#[derive(Copy, Clone, Eq, PartialEq, Hash)] +#[derive(Copy, Clone, Hash)] #[cfg_attr(feature = "serialize", derive(Serialize))] pub struct Span { /// The inclusive start position. @@ -131,6 +145,19 @@ impl Offset for Span { } } +impl Eq for Span {} + +impl PartialEq for Span { + fn eq(&self, other: &Self) -> bool { + #[cfg(test)] + if !CMP_SPANS.with(Cell::get) { + return true; + } + + self.start == other.start && self.end == other.end + } +} + impl Debug for Span { fn fmt(&self, f: &mut Formatter) -> fmt::Result { write!(f, "<{:?}-{:?}>", self.start, self.end) |
