Struct serde_spanned::spanned::Spanned
source · pub struct Spanned<T> {
span: Range<usize>,
value: T,
}
Expand description
A spanned value, indicating the range at which it is defined in the source.
Fields§
§span: Range<usize>
Byte range
value: T
The spanned value.
Implementations§
source§impl<T> Spanned<T>
impl<T> Spanned<T>
sourcepub fn new(range: Range<usize>, value: T) -> Self
pub fn new(range: Range<usize>, value: T) -> Self
Create a spanned value encompassing the given byte range.
§Example
Transposing a Spanned<Enum<T>>
into Enum<Spanned<T>>
:
use serde::de::{Deserialize, Deserializer};
use serde_untagged::UntaggedEnumVisitor;
use toml::Spanned;
pub enum Dependency {
Simple(Spanned<String>),
Detailed(Spanned<DetailedDependency>),
}
impl<'de> Deserialize<'de> for Dependency {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
enum DependencyKind {
Simple(String),
Detailed(DetailedDependency),
}
impl<'de> Deserialize<'de> for DependencyKind {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
UntaggedEnumVisitor::new()
.expecting(
"a version string like \"0.9.8\" or a \
detailed dependency like { version = \"0.9.8\" }",
)
.string(|value| Ok(DependencyKind::Simple(value.to_owned())))
.map(|value| value.deserialize().map(DependencyKind::Detailed))
.deserialize(deserializer)
}
}
let spanned: Spanned<DependencyKind> = Deserialize::deserialize(deserializer)?;
let range = spanned.span();
Ok(match spanned.into_inner() {
DependencyKind::Simple(simple) => Dependency::Simple(Spanned::new(range, simple)),
DependencyKind::Detailed(detailed) => Dependency::Detailed(Spanned::new(range, detailed)),
})
}
}
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the spanned value and returns the contained value.
Trait Implementations§
source§impl<'de, T> Deserialize<'de> for Spanned<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for Spanned<T>where
T: Deserialize<'de>,
source§fn deserialize<D>(deserializer: D) -> Result<Spanned<T>, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Spanned<T>, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl<T: Ord> Ord for Spanned<T>
impl<T: Ord> Ord for Spanned<T>
source§impl<T: PartialOrd> PartialOrd for Spanned<T>
impl<T: PartialOrd> PartialOrd for Spanned<T>
impl<T: Eq> Eq for Spanned<T>
Auto Trait Implementations§
impl<T> Freeze for Spanned<T>where
T: Freeze,
impl<T> RefUnwindSafe for Spanned<T>where
T: RefUnwindSafe,
impl<T> Send for Spanned<T>where
T: Send,
impl<T> Sync for Spanned<T>where
T: Sync,
impl<T> Unpin for Spanned<T>where
T: Unpin,
impl<T> UnwindSafe for Spanned<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)