Skip to main content

ByteSpan

Struct ByteSpan 

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

A byte-based span representing a range in source text.

ByteSpan uses byte offsets (not character or line positions) for precise and efficient source location tracking. For LSP communication, use WireRange or convert via LineStartsCache.

§Invariants

  • start <= end (enforced by constructors, but not at type level for Copy)
  • Both start and end are valid byte offsets in the source text
  • Spans are half-open intervals: [start, end)

§Example

use perl_position_tracking::ByteSpan;

let span = ByteSpan::new(0, 10);
assert_eq!(span.len(), 10);
assert!(!span.is_empty());

// Extract the spanned text
let source = "hello world";
let text = span.slice(source);
assert_eq!(text, "hello worl");

Fields§

§start: usize

Starting byte offset in the source text (inclusive)

§end: usize

Ending byte offset in the source text (exclusive)

Implementations§

Source§

impl ByteSpan

Source

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

Creates a new ByteSpan with the given start and end offsets.

§Panics

Panics in debug mode if start > end.

Source

pub const fn empty(pos: usize) -> Self

Creates an empty span at the given position.

Source

pub fn whole(source: &str) -> Self

Creates a span covering the entire source text.

Source

pub const fn len(&self) -> usize

Returns the length of this span in bytes.

Source

pub const fn is_empty(&self) -> bool

Returns true if this span is empty (start == end).

Source

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

Returns true if this span contains the given byte offset.

Source

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

Returns true if this span contains the given span entirely.

Source

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

Returns true if this span overlaps with the given span.

Source

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

Returns the intersection of two spans, or None if they don’t overlap.

Source

pub fn union(&self, other: ByteSpan) -> ByteSpan

Returns a new span that covers both this span and the given span.

Source

pub fn slice<'a>(&self, source: &'a str) -> &'a str

Extracts the slice of source text covered by this span.

§Panics

Panics if the span is out of bounds for the source text.

Source

pub fn try_slice<'a>(&self, source: &'a str) -> Option<&'a str>

Safely extracts the slice of source text, returning None if out of bounds.

Source

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

Converts to a standard Range.

Trait Implementations§

Source§

impl Clone for ByteSpan

Source§

fn clone(&self) -> ByteSpan

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 ByteSpan

Source§

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

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

impl Default for ByteSpan

Source§

fn default() -> ByteSpan

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

impl<'de> Deserialize<'de> for ByteSpan

Source§

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

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

impl Display for ByteSpan

Source§

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

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

impl From<(usize, usize)> for ByteSpan

Source§

fn from((start, end): (usize, usize)) -> Self

Converts to this type from the input type.
Source§

impl From<ByteSpan> for (usize, usize)

Source§

fn from(span: ByteSpan) -> Self

Converts to this type from the input type.
Source§

impl From<ByteSpan> for Range

Convert old SourceLocation to Range (for migration)

Source§

fn from(loc: SourceLocation) -> Self

Converts to this type from the input type.
Source§

impl From<ByteSpan> for Range<usize>

Source§

fn from(span: ByteSpan) -> Self

Converts to this type from the input type.
Source§

impl From<Range<usize>> for ByteSpan

Source§

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

Converts to this type from the input type.
Source§

impl Hash for ByteSpan

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 ByteSpan

Source§

fn eq(&self, other: &ByteSpan) -> 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 Serialize for ByteSpan

Source§

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

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

impl Copy for ByteSpan

Source§

impl Eq for ByteSpan

Source§

impl StructuralPartialEq for ByteSpan

Auto Trait Implementations§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,