Skip to main content

Spanned

Struct Spanned 

Source
pub struct Spanned<T> {
    pub value: T,
    pub span: Span,
    pub file_id: u16,
}
Expand description

A value with an associated source location (span and file).

PartialEq / Eq / Hash are implemented manually to delegate to the inner value only — two Spanned<T> values are considered equal when their Ts are equal, regardless of where they came from in source. This matches the principle that “what” a value is should not depend on where it lives. Consumers that genuinely need location-sensitive equality compare .span and .file_id explicitly.

Note: the rkyv-archived form (ArchivedSpanned<T>, present under the rkyv feature) does not automatically receive PartialEq / Eq. The host doesn’t compare archived values today; if a future code path needs to, add rkyv(compare = (PartialEq)) to the derive attribute below or hand-roll a manual impl on the archived type.

§#[non_exhaustive] policy

Deliberately NOT #[non_exhaustive], for the same reason as Span: it is constructed via struct literal in hundreds of call sites and the field set is intentionally minimal and stable. Add fields cautiously; if a new field is genuinely needed, prefer a sibling/wrapper type over modifying this one in place.

Fields§

§value: T

The value.

§span: Span

The source span (byte offsets within the file).

§file_id: u16

The source file ID (index into SourceMap). Uses u16 to minimize struct size (max 65,535 files).

Implementations§

Source§

impl<T> Spanned<T>

Source

pub const fn new(value: T, span: Span) -> Spanned<T>

Create a new spanned value with file_id defaulting to 0.

Use with_file_id to set the correct file ID after creation.

Source

pub const fn synthesized(value: T) -> Spanned<T>

Wrap a value that was programmatically synthesized (no source representation). Uses Span::ZERO and SYNTHESIZED_FILE_ID so downstream consumers can detect “no source” without sentinel checks on the inner value’s fields.

Used by plugin-synthesized AST nodes, test fixtures, CLI commands that build directives in-memory, and any other producer that does not parse from source bytes.

Source

pub fn with_file_id(self, file_id: usize) -> Spanned<T>

Set the file ID for this spanned value.

Accepts usize for API convenience but stores as u16 internally.

§Panics

Debug builds will panic if file_id exceeds u16::MAX (65,535).

Source

pub fn map<U, F>(self, f: F) -> Spanned<U>
where F: FnOnce(T) -> U,

Map the inner value, preserving span and file_id.

Source

pub const fn inner(&self) -> &T

Get a reference to the inner value.

Source

pub fn into_inner(self) -> T

Unwrap the spanned value, discarding the span and file_id.

Trait Implementations§

Source§

impl<T> Archive for Spanned<T>

Source§

type Archived = ArchivedSpanned<T>

The archived representation of this type. Read more
Source§

type Resolver = SpannedResolver<T>

The resolver for this type. It must contain all the additional information from serializing needed to make the archived type from the normal type.
Source§

fn resolve( &self, resolver: <Spanned<T> as Archive>::Resolver, out: Place<<Spanned<T> as Archive>::Archived>, )

Creates the archived version of this value at the given position and writes it to the given output. Read more
Source§

const COPY_OPTIMIZATION: CopyOptimization<Self> = _

An optimization flag that allows the bytes of this type to be copied directly to a writer instead of calling serialize. Read more
Source§

impl<T> Clone for Spanned<T>
where T: Clone,

Source§

fn clone(&self) -> Spanned<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Spanned<T>
where T: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<T> Deref for Spanned<T>

Spanned<T> is a transparent wrapper that adds source location to a value. Following the convention used by other transparent wrappers in the standard library (Box<T>, Rc<T>, Cow<'_, T>, MutexGuard<T>), it implements Deref so callers can read inner fields and call inner methods without spelling .value everywhere. Consumers that genuinely need to inspect the source location reach for .span, .file_id, or .value (for ownership) explicitly.

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T> DerefMut for Spanned<T>

Source§

fn deref_mut(&mut self) -> &mut T

Mutably dereferences the value.
Source§

impl<'de, T> Deserialize<'de> for Spanned<T>
where T: Deserialize<'de>,

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Spanned<T>, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<__D, T> Deserialize<Spanned<T>, __D> for <Spanned<T> as Archive>::Archived

Source§

fn deserialize( &self, deserializer: &mut __D, ) -> Result<Spanned<T>, <__D as Fallible>::Error>

Deserializes using the given deserializer
Source§

impl<T> Display for Spanned<T>
where T: Display,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<T> Eq for Spanned<T>
where T: Eq,

Source§

impl<T> Hash for Spanned<T>
where T: Hash,

Source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> PartialEq for Spanned<T>
where T: PartialEq,

Source§

fn eq(&self, other: &Spanned<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Serialize for Spanned<T>
where T: Serialize,

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<__S, T> Serialize<__S> for Spanned<T>
where __S: Fallible + ?Sized, T: Serialize<__S>, Span: Serialize<__S>, u16: Serialize<__S>,

Source§

fn serialize( &self, serializer: &mut __S, ) -> Result<<Spanned<T> as Archive>::Resolver, <__S as Fallible>::Error>

Writes the dependencies for the object and returns a resolver that can create the archived type.
Source§

impl<T> ShiftSpans for Spanned<T>
where T: ShiftSpans,

Source§

fn shift_spans<F>(&mut self, shift: &F)
where F: Fn(&mut Span),

Apply shift to every Span reachable in self.

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> UnsafeUnpin for Spanned<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Spanned<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> ArchivePointee for T

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
Source§

impl<T> ArchiveUnsized for T
where T: Archive,

Source§

type Archived = <T as Archive>::Archived

The archived counterpart of this type. Unlike Archive, it may be unsized. Read more
Source§

fn archived_metadata( &self, ) -> <<T as ArchiveUnsized>::Archived as ArchivePointee>::ArchivedMetadata

Creates the archived version of the metadata for this value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> CryptoRng for T
where T: DerefMut, <T as Deref>::Target: CryptoRng,

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> RngCore for T
where T: DerefMut, <T as Deref>::Target: RngCore,

Source§

fn next_u32(&mut self) -> u32

Return the next random u32. Read more
Source§

fn next_u64(&mut self) -> u64

Return the next random u64. Read more
Source§

fn fill_bytes(&mut self, dst: &mut [u8])

Fill dest with random data. Read more
Source§

impl<T, S> SerializeUnsized<S> for T
where T: Serialize<S>, S: Fallible + Writer + ?Sized,

Source§

fn serialize_unsized( &self, serializer: &mut S, ) -> Result<usize, <S as Fallible>::Error>

Writes the object and returns the position of the archived type.
Source§

impl<T> Source for T
where T: Deref, <T as Deref>::Target: Source,

Source§

type Slice<'a> = <<T as Deref>::Target as Source>::Slice<'a> where T: 'a

A type this Source can be sliced into.
Source§

fn len(&self) -> usize

Length of the source
Source§

fn read<'a, Chunk>(&'a self, offset: usize) -> Option<Chunk>
where Chunk: Chunk<'a>,

Read a chunk of bytes into an array. Returns None when reading out of bounds would occur. Read more
Source§

fn slice(&self, range: Range<usize>) -> Option<<T as Source>::Slice<'_>>

Get a slice of the source at given range. This is analogous to slice::get(range). Read more
Source§

unsafe fn slice_unchecked( &self, range: Range<usize>, ) -> <T as Source>::Slice<'_>

Get a slice of the source at given range. This is analogous to slice::get_unchecked(range). Read more
Source§

fn is_boundary(&self, index: usize) -> bool

Check if index is valid for this Source, that is: Read more
Source§

fn find_boundary(&self, index: usize) -> usize

For &str sources attempts to find the closest char boundary at which source can be sliced, starting from index. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<R> TryCryptoRng for R
where R: CryptoRng + ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<R> TryRngCore for R
where R: RngCore + ?Sized,

Source§

type Error = Infallible

The type returned in the event of a RNG error.
Source§

fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>

Return the next random u32.
Source§

fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>

Return the next random u64.
Source§

fn try_fill_bytes( &mut self, dst: &mut [u8], ) -> Result<(), <R as TryRngCore>::Error>

Fill dest entirely with random data.
Source§

fn unwrap_err(self) -> UnwrapErr<Self>
where Self: Sized,

Wrap RNG with the UnwrapErr wrapper.
Source§

fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self>

Wrap RNG with the UnwrapMut wrapper.