Struct Span

Source
#[repr(C)]
pub struct Span { pub start: u32, pub end: u32, /* private fields */ }
Expand description

A range in text, represented by a zero-indexed start and end offset.

It is a logical error for end to be less than start.

let text = "foo bar baz";
let span = Span::new(4, 7);
assert_eq!(&text[span], "bar");

Spans use u32 for offsets, meaning only files up to 4GB are supported. This is sufficient for “all” reasonable programs. This tradeof cuts the size of Span in half, offering a sizeable performance improvement and memory footprint reduction.

§Creating Spans

Span offers several constructors, each of which is more or less convenient depending on the context. In general, Span::new is sufficient for most cases. If you want to create a span starting at some point of a certain length, you can use Span::sized.

let a = Span::new(5, 10);  // Start and end offsets
let b = Span::sized(5, 5); // Start offset and size
assert_eq!(a, b);

§Re-Sizing Spans

Span offsets can be mutated directly, but it is often more convenient to use one of the expand or shrink methods. Each of these create a new span without modifying the original.

let s = Span::new(5, 10);
assert_eq!(s.shrink(2), Span::new(7, 8));
assert_eq!(s.shrink(2), s.shrink_left(2).shrink_right(2));

assert_eq!(s.expand(5), Span::new(0, 15));
assert_eq!(s.expand(5), s.expand_left(5).expand_right(5));

§Comparison

Span has a normal implementation of PartialEq. If you want to compare two AST nodes without considering their locations (e.g. to see if they have the same content), use ContentEq instead.

§Implementation Notes

See the text-size crate for details. Utility methods can be copied from the text-size crate if they are needed.

Fields§

§start: u32

The zero-based start offset of the span

§end: u32

The zero-based end offset of the span. This may be equal to start if the span is empty, but should not be less than it.

Implementations§

Source§

impl Span

Source

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

Create a new Span from a start and end position.

§Invariants

The start position must be less than or equal to end. Note that this invariant is only checked in debug builds to avoid a performance penalty.

Source

pub fn empty(at: u32) -> Self

Create a new empty Span that starts and ends at an offset position.

§Examples
use oxc_span::Span;

let fifth = Span::empty(5);
assert!(fifth.is_empty());
assert_eq!(fifth, Span::sized(5, 0));
assert_eq!(fifth, Span::new(5, 5));
Source

pub const fn sized(start: u32, size: u32) -> Self

Create a new Span starting at start and covering size bytes.

§Example
use oxc_span::Span;

let span = Span::sized(2, 4);
assert_eq!(span.size(), 4);
assert_eq!(span, Span::new(2, 6));
Source

pub const fn size(self) -> u32

Get the number of bytes covered by the Span.

§Example
use oxc_span::Span;

assert_eq!(Span::new(1, 1).size(), 0);
assert_eq!(Span::new(0, 5).size(), 5);
assert_eq!(Span::new(5, 10).size(), 5);
Source

pub const fn is_empty(self) -> bool

Returns true if self covers a range of zero length.

§Example
use oxc_span::Span;

assert!(Span::new(0, 0).is_empty());
assert!(Span::new(5, 5).is_empty());
assert!(!Span::new(0, 5).is_empty());
Source

pub const fn is_unspanned(self) -> bool

Returns true if self is not a real span. i.e. SPAN which is used for generated nodes which are not in source code.

§Example
use oxc_span::{Span, SPAN};

assert!(SPAN.is_unspanned());
assert!(!Span::new(0, 5).is_unspanned());
assert!(!Span::new(5, 5).is_unspanned());
Source

pub const fn contains_inclusive(self, span: Span) -> bool

Check if this Span contains another Span.

Spans that start & end at the same position as this Span are considered contained.

§Examples
let span = Span::new(5, 10);

assert!(span.contains_inclusive(span)); // always true for itself
assert!(span.contains_inclusive(Span::new(5, 5)));
assert!(span.contains_inclusive(Span::new(6, 10)));
assert!(span.contains_inclusive(Span::empty(5)));

assert!(!span.contains_inclusive(Span::new(4, 10)));
assert!(!span.contains_inclusive(Span::empty(0)));
Source

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

Create a Span covering the maximum range of two Spans.

§Example
use oxc_span::Span;

let span1 = Span::new(0, 5);
let span2 = Span::new(3, 8);
let merged_span = span1.merge(span2);
assert_eq!(merged_span, Span::new(0, 8));
Source

pub fn expand(self, offset: u32) -> Self

Create a Span that is grown by offset on either side.

This is equivalent to span.expand_left(offset).expand_right(offset). See expand_left and expand_right for more info.

§Example
use oxc_span::Span;

let span = Span::new(3, 5);
assert_eq!(span.expand(1), Span::new(2, 6));
// start and end cannot be expanded past `0` and `u32::MAX`, respectively
assert_eq!(span.expand(5), Span::new(0, 10));
Source

pub fn shrink(self, offset: u32) -> Self

Create a Span that has its start and end positions shrunk by offset amount.

It is a logical error to shrink the start of the Span past its end position. This will panic in debug builds.

This is equivalent to span.shrink_left(offset).shrink_right(offset). See shrink_left and shrink_right for more info.

§Example
use oxc_span::Span;
let span = Span::new(5, 10);
assert_eq!(span.shrink(2), Span::new(7, 8));
Source

pub const fn expand_left(self, offset: u32) -> Self

Create a Span that has its start position moved to the left by offset bytes.

§Example
use oxc_span::Span;

let a = Span::new(5, 10);
assert_eq!(a.expand_left(5), Span::new(0, 10));
§Bounds

The leftmost bound of the span is clamped to 0. It is safe to call this method with a value larger than the start position.

use oxc_span::Span;

let a = Span::new(0, 5);
assert_eq!(a.expand_left(5), Span::new(0, 5));
Source

pub const fn shrink_left(self, offset: u32) -> Self

Create a Span that has its start position moved to the right by offset bytes.

It is a logical error to shrink the start of the Span past its end position.

§Example
use oxc_span::Span;

let a = Span::new(5, 10);
let shrunk = a.shrink_left(5);
assert_eq!(shrunk, Span::new(10, 10));

// Shrinking past the end of the span is a logical error that will panic
// in debug builds.
std::panic::catch_unwind(|| {
   shrunk.shrink_left(5);
});
Source

pub const fn expand_right(self, offset: u32) -> Self

Create a Span that has its end position moved to the right by offset bytes.

§Example
use oxc_span::Span;

let a = Span::new(5, 10);
assert_eq!(a.expand_right(5), Span::new(5, 15));
§Bounds

The rightmost bound of the span is clamped to u32::MAX. It is safe to call this method with a value larger than the end position.

use oxc_span::Span;

let a = Span::new(0, u32::MAX);
assert_eq!(a.expand_right(5), Span::new(0, u32::MAX));
Source

pub const fn shrink_right(self, offset: u32) -> Self

Create a Span that has its end position moved to the left by offset bytes.

It is a logical error to shrink the end of the Span past its start position.

§Example
use oxc_span::Span;

let a = Span::new(5, 10);
let shrunk = a.shrink_right(5);
assert_eq!(shrunk, Span::new(5, 5));

// Shrinking past the start of the span is a logical error that will panic
// in debug builds.
std::panic::catch_unwind(|| {
   shrunk.shrink_right(5);
});
Source

pub fn source_text(self, source_text: &str) -> &str

Get a snippet of text from a source string that the Span covers.

§Example
use oxc_span::Span;

let source = "function add (a, b) { return a + b; }";
let name_span = Span::new(9, 12);
let name = name_span.source_text(source);
assert_eq!(name_span.size(), name.len() as u32);
Source

pub fn label<S: Into<String>>(self, label: S) -> LabeledSpan

Create a LabeledSpan covering this Span with the given label.

Use Span::primary_label if this is the primary span for the diagnostic.

Source

pub fn primary_label<S: Into<String>>(self, label: S) -> LabeledSpan

Creates a primary LabeledSpan covering this Span with the given label.

Trait Implementations§

Source§

impl Clone for Span

Source§

fn clone(&self) -> Span

Returns a copy 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<'a> CloneIn<'a> for Span

Source§

type Cloned = Span

The type of the cloned object. Read more
Source§

fn clone_in(&self, _: &'a Allocator) -> Self

Clone self into the given allocator. allocator may be the same one that self is already in.
Source§

fn clone_in_with_semantic_ids( &self, allocator: &'new_alloc Allocator, ) -> Self::Cloned

Almost identical as clone_in, but for some special type, it will also clone the semantic ids. Please use this method only if you make sure semantic info is synced with the ast node.
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() -> Span

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

impl<'a> Dummy<'a> for Span

Source§

fn dummy(_allocator: &'a Allocator) -> Self

Create a dummy Span.

Source§

impl From<Range<u32>> for Span

Source§

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

Converts to this type from the input type.
Source§

impl From<Span> for LabeledSpan

Source§

fn from(val: Span) -> Self

Converts to this type from the input type.
Source§

impl From<Span> for SourceSpan

Source§

fn from(val: Span) -> Self

Converts to this type from the input type.
Source§

impl GetSpan for Span

Source§

fn span(&self) -> Span

Get the Span for an AST node.
Source§

impl GetSpanMut for Span

Source§

fn span_mut(&mut self) -> &mut Span

Get a mutable reference to an AST node’s Span.
Source§

impl Hash for Span

Source§

fn hash<H: Hasher>(&self, hasher: &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 Index<Span> for CompactStr

Source§

type Output = str

The returned type after indexing.
Source§

fn index(&self, index: Span) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<Span> for str

Source§

type Output = str

The returned type after indexing.
Source§

fn index(&self, index: Span) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<Span> for str

Source§

fn index_mut(&mut self, index: Span) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl Ord for Span

Source§

fn cmp(&self, other: &Span) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Span

Source§

fn eq(&self, other: &Self) -> 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 PartialOrd for Span

Source§

fn partial_cmp(&self, other: &Span) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for Span

Source§

impl Eq 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<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<'a, T> FromIn<'a, T> for T

Source§

fn from_in(t: T, _: &'a Allocator) -> T

Converts to this type from the input type within the given allocator.
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<'a, T, U> IntoIn<'a, U> for T
where U: FromIn<'a, T>,

Source§

fn into_in(self, allocator: &'a Allocator) -> U

Converts this type into the (usually inferred) input type within the given allocator.
Source§

impl<D> OwoColorize for D

Source§

fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>
where C: Color,

Set the foreground color generically Read more
Source§

fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>
where C: Color,

Set the background color generically. Read more
Source§

fn black(&self) -> FgColorDisplay<'_, Black, Self>

Change the foreground color to black
Source§

fn on_black(&self) -> BgColorDisplay<'_, Black, Self>

Change the background color to black
Source§

fn red(&self) -> FgColorDisplay<'_, Red, Self>

Change the foreground color to red
Source§

fn on_red(&self) -> BgColorDisplay<'_, Red, Self>

Change the background color to red
Source§

fn green(&self) -> FgColorDisplay<'_, Green, Self>

Change the foreground color to green
Source§

fn on_green(&self) -> BgColorDisplay<'_, Green, Self>

Change the background color to green
Source§

fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>

Change the foreground color to yellow
Source§

fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>

Change the background color to yellow
Source§

fn blue(&self) -> FgColorDisplay<'_, Blue, Self>

Change the foreground color to blue
Source§

fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>

Change the background color to blue
Source§

fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to magenta
Source§

fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to magenta
Source§

fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to purple
Source§

fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to purple
Source§

fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>

Change the foreground color to cyan
Source§

fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>

Change the background color to cyan
Source§

fn white(&self) -> FgColorDisplay<'_, White, Self>

Change the foreground color to white
Source§

fn on_white(&self) -> BgColorDisplay<'_, White, Self>

Change the background color to white
Source§

fn default_color(&self) -> FgColorDisplay<'_, Default, Self>

Change the foreground color to the terminal default
Source§

fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>

Change the background color to the terminal default
Source§

fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>

Change the foreground color to bright black
Source§

fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>

Change the background color to bright black
Source§

fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>

Change the foreground color to bright red
Source§

fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>

Change the background color to bright red
Source§

fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>

Change the foreground color to bright green
Source§

fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>

Change the background color to bright green
Source§

fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>

Change the foreground color to bright yellow
Source§

fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>

Change the background color to bright yellow
Source§

fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>

Change the foreground color to bright blue
Source§

fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>

Change the background color to bright blue
Source§

fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright magenta
Source§

fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright magenta
Source§

fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright purple
Source§

fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright purple
Source§

fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>

Change the foreground color to bright cyan
Source§

fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>

Change the background color to bright cyan
Source§

fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>

Change the foreground color to bright white
Source§

fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>

Change the background color to bright white
Source§

fn bold(&self) -> BoldDisplay<'_, Self>

Make the text bold
Source§

fn dimmed(&self) -> DimDisplay<'_, Self>

Make the text dim
Source§

fn italic(&self) -> ItalicDisplay<'_, Self>

Make the text italicized
Source§

fn underline(&self) -> UnderlineDisplay<'_, Self>

Make the text underlined
Make the text blink
Make the text blink (but fast!)
Source§

fn reversed(&self) -> ReversedDisplay<'_, Self>

Swap the foreground and background colors
Source§

fn hidden(&self) -> HiddenDisplay<'_, Self>

Hide the text
Source§

fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>

Cross out the text
Source§

fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the foreground color at runtime. Only use if you do not know which color will be used at compile-time. If the color is constant, use either OwoColorize::fg or a color-specific method, such as OwoColorize::green, Read more
Source§

fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the background color at runtime. Only use if you do not know what color to use at compile-time. If the color is constant, use either OwoColorize::bg or a color-specific method, such as OwoColorize::on_yellow, Read more
Source§

fn fg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the foreground color to a specific RGB value.
Source§

fn bg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the background color to a specific RGB value.
Source§

fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>

Sets the foreground color to an RGB value.
Source§

fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>

Sets the background color to an RGB value.
Source§

fn style(&self, style: Style) -> Styled<&Self>

Apply a runtime-determined style
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.