Skip to content

Commit

Permalink
impl StableDeref for LocatedSpan (#65)
Browse files Browse the repository at this point in the history
LocatedSpan is largely just a wrapper around the contained type `T`, so
this marker trait is safe to implement whenever T already implements
StableDeref.
  • Loading branch information
deepinthebuild committed Oct 16, 2020
1 parent 79cf042 commit ffba5fc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ std = ["nom/std", "alloc"]
alloc = ["nom/alloc"]
generic-simd = ["bytecount/generic-simd"]
runtime-dispatch-simd = ["bytecount/runtime-dispatch-simd"]
stable-deref-trait = ["stable_deref_trait"]

[dependencies]
bytecount = "^0.6"
memchr = ">=1.0.1, <3.0.0" # ^1.0.0 + ^2.0
nom = { version = "5.0.0", default-features = false }
stable_deref_trait = { version = "^1", optional = true, default-features = false }
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ use nom::{
AsBytes, Compare, CompareResult, Err, FindSubstring, FindToken, IResult, InputIter,
InputLength, InputTake, InputTakeAtPosition, Offset, ParseTo, Slice,
};
#[cfg(feature = "stable-deref-trait")]
use stable_deref_trait::StableDeref;

/// A LocatedSpan is a set of meta information about the location of a token, including extra
/// information.
Expand Down Expand Up @@ -146,6 +148,14 @@ impl<T, X> core::ops::Deref for LocatedSpan<T, X> {
}
}

#[cfg(feature = "stable-deref-trait")]
/// Optionally impl StableDeref so that this type works harmoniously with other
/// crates that rely on this marker trait, such as `rental` and `lazy_static`.
/// LocatedSpan is largely just a wrapper around the contained type `T`, so
/// this marker trait is safe to implement whenever T already implements
/// StableDeref.
unsafe impl<T: StableDeref, X> StableDeref for LocatedSpan<T, X> {}

impl<T: AsBytes> LocatedSpan<T, ()> {
/// Create a span for a particular input with default `offset` and
/// `line` values and empty extra data.
Expand Down

0 comments on commit ffba5fc

Please sign in to comment.