Struct oxc_span::Span

source ·
#[non_exhaustive]
#[repr(C)]
pub struct Span { pub start: u32, pub end: u32, }
Expand description

Newtype for working with text ranges

See the text-size crate for details. Utility methods can be copied from the text-size crate if they are needed. NOTE: u32 is sufficient for “all” reasonable programs. Larger than u32 is a 4GB JS file.

§Hashing

Span’s implementation of Hash is a no-op so that AST nodes can be compared by hash. This makes them unsuitable for use as keys in a hash map.

use std::hash::{Hash, Hasher, DefaultHasher};
use oxc_span::Span;

let mut first = DefaultHasher::new();
let mut second = DefaultHasher::new();

Span::new(0, 5).hash(&mut first);
Span::new(10, 20).hash(&mut second);

assert_eq!(first.finish(), second.finish());

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§start: u32§end: u32

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<'a>(&self, source_text: &'a str) -> &'a 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.

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

§

type Cloned = Span

source§

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

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

source§

impl GetSpanMut for Span

source§

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

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 Index<Span> for CompactStr

§

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

§

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 + PartialOrd,

Restrict a value to a certain interval. 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 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

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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

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

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 italicized
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,

§

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

§

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

§

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.