Span

Struct Span 

Source
pub struct Span {
    pub file_id: FileId,
    pub start: u32,
    pub end: u32,
}
Expand description

A span representing a range in the source code.

Spans use byte offsets into the source string and include a file identifier for multi-file compilation. They are designed to be small (12 bytes) and cheap to copy.

Fields§

§file_id: FileId

The file this span belongs to

§start: u32

Start byte offset (inclusive)

§end: u32

End byte offset (exclusive)

Implementations§

Source§

impl Span

Source

pub const fn new(start: u32, end: u32) -> Span

Create a new span from start and end byte offsets.

Uses the default file ID. For multi-file compilation, use with_file.

Source

pub const fn with_file(file_id: FileId, start: u32, end: u32) -> Span

Create a new span with a specific file ID.

Source

pub const fn point(pos: u32) -> Span

Create an empty span at a single position.

Uses the default file ID. For multi-file compilation, use point_in_file.

Source

pub const fn point_in_file(file_id: FileId, pos: u32) -> Span

Create an empty span at a single position in a specific file.

Source

pub const fn cover(a: Span, b: Span) -> Span

Create a span covering two spans (from start of first to end of second).

Uses the file ID from span a. Both spans should be from the same file.

Source

pub const fn extend_to(&self, end: u32) -> Span

Extend this span to a new end position, preserving the file ID.

Creates a span from self.start to end with the same file ID.

Source

pub const fn start(&self) -> u32

Get the start byte offset.

Source

pub const fn len(&self) -> u32

The length of this span in bytes.

Source

pub const fn is_empty(&self) -> bool

Whether this span is empty.

Source

pub const fn contains(&self, other: Span) -> bool

Returns true if other is entirely contained within this span.

A span a contains span b if a.start <= b.start and b.end <= a.end. An empty span at a boundary is considered contained.

§Example
use gruel_span::Span;

let outer = Span::new(5, 20);
let inner = Span::new(10, 15);
let overlapping = Span::new(15, 25);

assert!(outer.contains(inner));
assert!(!outer.contains(overlapping));
assert!(outer.contains(Span::point(10)));
Source

pub const fn contains_pos(&self, pos: u32) -> bool

Returns true if this span contains the given byte position.

The position is contained if self.start <= pos < self.end. Note: the end position is exclusive, so pos == self.end returns false.

§Example
use gruel_span::Span;

let span = Span::new(5, 10);
assert!(!span.contains_pos(4));  // before span
assert!(span.contains_pos(5));   // at start (inclusive)
assert!(span.contains_pos(7));   // in middle
assert!(!span.contains_pos(10)); // at end (exclusive)
Source

pub const fn as_range(&self) -> Range<usize>

Convert to a Range for slicing.

Source

pub fn line_number(&self, source: &str) -> usize

Compute the 1-based line number for this span’s start position.

Returns the line number (1-indexed) where this span begins.

§Panics

In debug builds, panics if self.start exceeds source.len(). In release builds, out-of-bounds offsets are clamped to source.len().

Source

pub fn line_col(&self, source: &str) -> (usize, usize)

Compute the 1-based line and column numbers for this span’s start position.

Returns (line, column) where both are 1-indexed. The column is the number of bytes from the start of the line, plus 1.

§Panics

In debug builds, panics if self.start exceeds source.len(). In release builds, out-of-bounds offsets are clamped to source.len().

Trait Implementations§

Source§

impl Clone for Span

Source§

fn clone(&self) -> Span

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Span

Source§

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

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

impl Default for Span

Source§

fn default() -> Span

Returns the “default value” for a type. Read more
Source§

impl From<Range<u32>> for Span

Source§

fn from(range: Range<u32>) -> Span

Converts to this type from the input type.
Source§

impl From<Range<usize>> for Span

Source§

fn from(range: Range<usize>) -> Span

Converts to this type from the input type.
Source§

impl Hash for Span

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 PartialEq for Span

Source§

fn eq(&self, other: &Span) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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 Copy for Span

Source§

impl Eq for Span

Source§

impl StructuralPartialEq for Span

Auto Trait Implementations§

§

impl Freeze for Span

§

impl RefUnwindSafe for Span

§

impl Send for Span

§

impl Sync for Span

§

impl Unpin for Span

§

impl UnwindSafe for Span

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> 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
§

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

§

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

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

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

§

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

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<'src, T> IntoMaybe<'src, T> for T
where T: 'src,

§

type Proj<U: 'src> = U

§

fn map_maybe<R>( self, _f: impl FnOnce(&'src T) -> &'src R, g: impl FnOnce(T) -> R, ) -> <T as IntoMaybe<'src, T>>::Proj<R>
where R: 'src,

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<'p, T> Seq<'p, T> for T
where T: Clone,

§

type Item<'a> = &'a T where T: 'a

The item yielded by the iterator.
§

type Iter<'a> = Once<&'a T> where T: 'a

An iterator over the items within this container, by reference.
§

fn seq_iter(&self) -> <T as Seq<'p, T>>::Iter<'_>

Iterate over the elements of the container.
§

fn contains(&self, val: &T) -> bool
where T: PartialEq,

Check whether an item is contained within this sequence.
§

fn to_maybe_ref<'b>(item: <T as Seq<'p, T>>::Item<'b>) -> Maybe<T, &'p T>
where 'p: 'b,

Convert an item of the sequence into a [MaybeRef].
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, 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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> OrderedSeq<'_, T> for T
where T: Clone,