Skip to main content

Index

Struct Index 

Source
pub struct Index { /* private fields */ }
Expand description

An index built for fast convertion between byte offsets and line-column locations.

NOTE: the last element of line_offsets should be considered as a fake offset (sentinel):

self.line_offsets: [line0, line1, ..., EOF]

By the word ‘fake’, we means that if some offset >= the last offset, it should be seen as exceding the index since the last element only marks ending.

§Invariants

  • index is never empty.
  • index[0] == 0.
  • index is monotonically increasing.
  • index is append-only (no removals or mutations).
  • A sentinel EOF offset is always present as the last element.

Therefore:

  • Valid logical line indices are 0..self.count(), where count() = index.len() - 1.
  • For any valid line i, the byte range is: [index[i], index[i + 1]).

Implementations§

Source§

impl Index

Source

pub fn new() -> Self

An index with the first line starting at offset 0, which is the most common usage.

The zero is safe here since it just means an ending, which also means empty.

Source

pub fn count(&self) -> usize

length of index The API has guaranteed that self.line_offsets.len() > 0

Source

pub fn end(&self) -> Option<Offset>

ending offset of the source

Source

pub fn into_offsets(self) -> Vec<Offset>

into vector of offsets

Source§

impl Index

Source

pub fn query(&self) -> Query<'_>

Get the query and freeze index when querying

Source

pub fn get_line_offset_mut(&mut self, line_no: usize) -> Option<&mut Offset>

Source

pub fn add_next_line(&mut self, offset: Offset)

Add next line offset to index

Source

pub fn clear(&mut self)

Reset the index

Trait Implementations§

Source§

impl Debug for Index

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Index

§

impl RefUnwindSafe for Index

§

impl Send for Index

§

impl Sync for Index

§

impl Unpin for Index

§

impl UnsafeUnpin for Index

§

impl UnwindSafe for Index

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