Skip to main content

SourceFile

Struct SourceFile 

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

One source file, with the line index a caret needs.

Line starts are computed once at construction. Every later question about a position is a binary search over that vector rather than a rescan, which matters because a diagnostic-heavy run asks the question many times per file.

Implementations§

Source§

impl SourceFile

Source

pub const fn id(&self) -> FileId

Returns this file’s identity within its map.

Source

pub const fn name(&self) -> &SourceName

Returns where this file came from.

Source

pub fn text(&self) -> &str

Returns the whole source text.

Source

pub fn len(&self) -> u32

Returns the source length in bytes.

§Panics

Never for a file a SourceMap holds: SourceMap::add refuses text whose length does not fit in a u32 offset.

Source

pub fn is_empty(&self) -> bool

Returns whether the source is empty.

Source

pub fn line_count(&self) -> usize

Returns how many lines the file has.

A file always has at least one line, and a trailing newline does not open an extra one.

Source

pub fn line_col(&self, offset: u32) -> LineCol

Returns the one-based line and column of offset.

§Panics

Panics when offset lies past the end of the file, or inside a multi-byte character. Both are defects in whatever produced the offset: spans come from a lexer whose token extents partition the file.

Source

pub fn offset(&self, at: LineCol) -> Option<u32>

Returns the offset at names, the inverse of SourceFile::line_col: None when the file has no such line, or the line no such column. A line’s columns end at its line break, or one past the file’s last character on its last line.

Source

pub fn line_index(&self, offset: u32) -> usize

Returns the zero-based index of the line containing offset.

Source

pub fn line_text(&self, index: usize) -> &str

Returns the text of the zero-based line index, without its newline.

§Panics

Panics when index names no line.

Source

pub fn snippet(&self, span: Span) -> &str

Returns the text span covers.

§Panics

Panics when the span names another file, lies past the end, or does not land on character boundaries.

Trait Implementations§

Source§

impl Clone for SourceFile

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SourceFile

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Forget for SourceFile

§

impl Freeze for SourceFile

§

impl Move for SourceFile

§

impl RefUnwindSafe for SourceFile

§

impl Send for SourceFile
where RawVecInner: Send,

§

impl Sync for SourceFile
where RawVecInner: Sync,

§

impl Unpin for SourceFile
where *const u8 is !null: Unpin,

§

impl UnsafeUnpin for SourceFile

§

impl UnwindSafe for SourceFile
where RawVecInner: 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<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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.