Struct gluon_base::pos::Span

source ·
pub struct Span<I> { /* private fields */ }
Expand description

A region of code in a source file

Implementations§

source§

impl<I: Ord> Span<I>

source

pub fn new(start: I, end: I) -> Span<I>

Create a new span

use gluon_base::pos::{ByteIndex, Span};

let span = Span::new(ByteIndex(3), ByteIndex(6));
assert_eq!(span.start(), ByteIndex(3));
assert_eq!(span.end(), ByteIndex(6));

start and end are reordered to maintain the invariant that start <= end

use gluon_base::pos::{ByteIndex, Span};

let span = Span::new(ByteIndex(6), ByteIndex(3));
assert_eq!(span.start(), ByteIndex(3));
assert_eq!(span.end(), ByteIndex(6));
source

pub fn map<F, J>(self, f: F) -> Span<J>
where F: FnMut(I) -> J, J: Ord,

source§

impl<I> Span<I>

source

pub const fn new_unchecked(start: I, end: I) -> Span<I>

Create a span like new but does not check that start <= end

source§

impl<I> Span<I>

source

pub fn start(self) -> I

Get the start index

source

pub fn end(self) -> I

Get the end index

source§

impl<I: Index> Span<I>

source

pub fn subspan(&self, begin: I::Offset, end: I::Offset) -> Span<I>

Makes a span from offsets relative to the start of this span.

source

pub fn from_offset(start: I, off: I::Offset) -> Span<I>

Create a new span from a byte start and an offset

source

pub fn with_start(self, start: I) -> Span<I>

Return a new span with the low byte position replaced with the supplied byte position

use gluon_base::pos::{ByteIndex, Span};

let span = Span::new(ByteIndex(3), ByteIndex(6));
assert_eq!(span.with_start(ByteIndex(2)), Span::new(ByteIndex(2), ByteIndex(6)));
assert_eq!(span.with_start(ByteIndex(5)), Span::new(ByteIndex(5), ByteIndex(6)));
assert_eq!(span.with_start(ByteIndex(7)), Span::new(ByteIndex(6), ByteIndex(7)));
source

pub fn with_end(self, end: I) -> Span<I>

Return a new span with the high byte position replaced with the supplied byte position

use gluon_base::pos::{ByteIndex, Span};

let span = Span::new(ByteIndex(3), ByteIndex(6));
assert_eq!(span.with_end(ByteIndex(7)), Span::new(ByteIndex(3), ByteIndex(7)));
assert_eq!(span.with_end(ByteIndex(5)), Span::new(ByteIndex(3), ByteIndex(5)));
assert_eq!(span.with_end(ByteIndex(2)), Span::new(ByteIndex(2), ByteIndex(3)));
source

pub fn contains(self, other: Span<I>) -> bool

Return true if self fully encloses other.

use gluon_base::pos::{ByteIndex, Span};

let a = Span::new(ByteIndex(5), ByteIndex(8));

assert_eq!(a.contains(a), true);
assert_eq!(a.contains(Span::new(ByteIndex(6), ByteIndex(7))), true);
assert_eq!(a.contains(Span::new(ByteIndex(6), ByteIndex(10))), false);
assert_eq!(a.contains(Span::new(ByteIndex(3), ByteIndex(6))), false);
source

pub fn contains_pos(self, other: I) -> bool

source

pub fn containment(self, pos: I) -> Ordering

Return Equal if self contains pos, otherwise it returns Less if pos is before start or Greater if pos is after or at end.

use gluon_base::pos::{ByteIndex, Span};
use std::cmp::Ordering::*;

let a = Span::new(ByteIndex(5), ByteIndex(8));

assert_eq!(a.containment(ByteIndex(4)), Less);
assert_eq!(a.containment(ByteIndex(5)), Equal);
assert_eq!(a.containment(ByteIndex(6)), Equal);
assert_eq!(a.containment(ByteIndex(8)), Equal);
assert_eq!(a.containment(ByteIndex(9)), Greater);
source

pub fn containment_exclusive(self, pos: I) -> Ordering

Return Equal if self contains pos, otherwise it returns Less if pos is before start or Greater if pos is strictly after end.

use gluon_base::pos::{ByteIndex, Span};
use std::cmp::Ordering::*;

let a = Span::new(ByteIndex(5), ByteIndex(8));

assert_eq!(a.containment_exclusive(ByteIndex(4)), Less);
assert_eq!(a.containment_exclusive(ByteIndex(5)), Equal);
assert_eq!(a.containment_exclusive(ByteIndex(6)), Equal);
assert_eq!(a.containment_exclusive(ByteIndex(8)), Greater);
assert_eq!(a.containment_exclusive(ByteIndex(9)), Greater);
source

pub fn to(self, end: Span<I>) -> Span<I>

Return a Span that would enclose both self and end.

self     ~~~~~~~
end                     ~~~~~~~~
returns  ~~~~~~~~~~~~~~~~~~~~~~~
use gluon_base::pos::{ByteIndex, Span};

let a = Span::new(ByteIndex(2), ByteIndex(5));
let b = Span::new(ByteIndex(10), ByteIndex(14));

assert_eq!(a.to(b), Span::new(ByteIndex(2), ByteIndex(14)));
source

pub fn between(self, end: Span<I>) -> Span<I>

Return a Span between the end of self to the beginning of end.

self     ~~~~~~~
end                     ~~~~~~~~
returns         ~~~~~~~~~
use gluon_base::pos::{ByteIndex, Span};

let a = Span::new(ByteIndex(2), ByteIndex(5));
let b = Span::new(ByteIndex(10), ByteIndex(14));

assert_eq!(a.between(b), Span::new(ByteIndex(5), ByteIndex(10)));
source

pub fn until(self, end: Span<I>) -> Span<I>

Return a Span between the beginning of self to the beginning of end.

self     ~~~~~~~
end                     ~~~~~~~~
returns  ~~~~~~~~~~~~~~~~
use gluon_base::pos::{ByteIndex, Span};

let a = Span::new(ByteIndex(2), ByteIndex(5));
let b = Span::new(ByteIndex(10), ByteIndex(14));

assert_eq!(a.until(b), Span::new(ByteIndex(2), ByteIndex(10)));
source§

impl Span<BytePos>

source

pub fn to_range(self, source: &CodeMap) -> Option<Range<usize>>

Trait Implementations§

source§

impl<I: Clone> Clone for Span<I>

source§

fn clone(&self) -> Span<I>

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<I: Debug> Debug for Span<I>

source§

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

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

impl<I: Default> Default for Span<I>

source§

fn default() -> Span<I>

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

impl<'de, I> Deserialize<'de> for Span<I>
where I: Deserialize<'de>,

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<I: Display> Display for Span<I>

source§

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

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

impl<I: Ord> Ord for Span<I>

source§

fn cmp(&self, other: &Span<I>) -> 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<I: PartialEq> PartialEq for Span<I>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<I: PartialOrd> PartialOrd for Span<I>

source§

fn partial_cmp(&self, other: &Span<I>) -> 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

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

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

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<I> Serialize for Span<I>
where I: Serialize,

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<I: Copy> Copy for Span<I>

source§

impl<I: Eq> Eq for Span<I>

source§

impl<I> StructuralEq for Span<I>

source§

impl<I> StructuralPartialEq for Span<I>

Auto Trait Implementations§

§

impl<I> RefUnwindSafe for Span<I>
where I: RefUnwindSafe,

§

impl<I> Send for Span<I>
where I: Send,

§

impl<I> Sync for Span<I>
where I: Sync,

§

impl<I> Unpin for Span<I>
where I: Unpin,

§

impl<I> UnwindSafe for Span<I>
where I: UnwindSafe,

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<Id> AsId<Id> for Id
where Id: ?Sized,

source§

fn as_id(&self) -> &Id

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

§

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§

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

§

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

impl<T> Any for T
where T: Any,

§

impl<T> CloneAny for T
where T: Any + Clone,

source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,