Span

Struct Span 

Source
pub struct Span {
    pub start: usize,
    pub end: usize,
}
Expand description

Span type that tracks a byte range.

§Example

use sipha_core::span::Span;

let span = Span::new(10, 20);
assert_eq!(span.start(), 10);
assert_eq!(span.end(), 20);

Fields§

§start: usize

Start byte offset (inclusive).

§end: usize

End byte offset (exclusive).

Implementations§

Source§

impl Span

Source

pub fn new(start: usize, end: usize) -> Self

Create a new span with start/end positions.

§Panics

Panics if end < start in debug mode.

Source

pub fn from_range(range: Range<usize>) -> Self

Create a span from a range.

Source

pub fn empty(pos: usize) -> Self

Create an empty span at the given position.

Source

pub fn range(&self) -> Range<usize>

Get the range of this span.

Source

pub fn start(&self) -> usize

Get the start byte offset.

Source

pub fn end(&self) -> usize

Get the end byte offset.

Source

pub fn len(&self) -> usize

Get the length of the span in bytes.

Source

pub fn is_empty(&self) -> bool

Check if the span is empty (start == end).

Source

pub fn contains(&self, pos: usize) -> bool

Check if this span contains the given position.

A position is contained if it’s >= start and < end.

Source

pub fn contains_span(&self, other: &Self) -> bool

Check if this span contains another span entirely.

Source

pub fn overlaps(&self, other: &Self) -> bool

Check if this span overlaps with another span.

Source

pub fn is_adjacent_to(&self, other: &Self) -> bool

Check if this span is adjacent to another span (touching but not overlapping).

Source

pub fn join(self, other: Self) -> Option<Self>

Join two adjacent or overlapping spans into one.

Returns None if the spans are not adjacent/overlapping.

Source

pub fn merge(self, other: Self) -> Self

Merge two spans into one, creating the smallest span that contains both.

Source

pub fn intersection(self, other: Self) -> Option<Self>

Get the intersection of two spans, if they overlap.

Returns None if the spans don’t overlap.

Source

pub fn before(&self, start_pos: usize) -> Option<Self>

Get the span that comes before this span, from start_pos to this span’s start.

Returns None if start_pos >= self.start().

§Example
use sipha_core::span::Span;

let span = Span::new(10, 20);
let before = span.before(5);
assert_eq!(before, Some(Span::new(5, 10)));
Source

pub fn after(&self, end_pos: usize) -> Option<Self>

Get the span that comes after this span, from this span’s end to end_pos.

Returns None if end_pos <= self.end().

§Example
use sipha_core::span::Span;

let span = Span::new(10, 20);
let after = span.after(25);
assert_eq!(after, Some(Span::new(20, 25)));
Source

pub fn expand_to_include(&mut self, other: &Self)

Expand this span to include another span.

This is equivalent to self.merge(other) but modifies self in place.

§Example
use sipha_core::span::Span;

let mut span = Span::new(10, 20);
span.expand_to_include(&Span::new(25, 30));
assert_eq!(span, Span::new(10, 30));
Source

pub fn split_at(&self, pos: usize) -> Option<(Self, Self)>

Split this span at the given position, returning two spans.

Returns (before, after) where:

  • before is the span from self.start() to pos (exclusive)
  • after is the span from pos to self.end()

Returns None if pos is not within this span.

§Example
use sipha_core::span::Span;

let span = Span::new(10, 20);
let (before, after) = span.split_at(15).unwrap();
assert_eq!(before, Span::new(10, 15));
assert_eq!(after, Span::new(15, 20));

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

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

impl Default for Span

Source§

fn default() -> Self

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

impl From<Range<usize>> for Span

Source§

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

Converts to this type from the input type.
Source§

impl From<Span> for Range<usize>

Source§

fn from(span: Span) -> Self

Converts to this type from the input type.
Source§

impl Hash for Span

Source§

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

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

impl<T> RuleId for T
where T: Copy + Eq + Hash + Debug + Send + Sync + 'static,

Source§

impl<T> SymbolId for T
where T: Copy + Eq + Hash + Debug + Send + Sync + 'static,