Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl StableDeref for LocatedSpan #65

Merged
merged 1 commit into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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