Skip to main content

SourceFile

Struct SourceFile 

Source
pub struct SourceFile {
    pub id: FileId,
    pub name: String,
    pub start: BytePos,
    pub end: BytePos,
    pub included_from: Option<Span>,
    /* private fields */
}
Expand description

The bytes of one file, plus where they sit in the flat space.

Contents are held behind a trait object rather than as a Vec, so that the memory mapped input in spec/05-preprocessor.md section 5.2 can be handed over as it is instead of being copied into one. Reading the bytes goes through one virtual call, which is fine because it happens once per file in the lexer and once per rendered diagnostic, never in a loop.

Fields§

§id: FileId

This file’s own id, so that anything holding a &SourceFile can name it.

§name: String

The name to print in a diagnostic, which is the path as the user wrote it rather than a canonical one. Somebody who typed -I../include wants to read ../include/foo.h.

§start: BytePos

First byte of this file in the flat space.

§end: BytePos

One past this file’s last byte.

§included_from: Option<Span>

The #include that pulled this file in, or None for a file named on the command line. This is what “in file included from” is printed from.

Implementations§

Source§

impl SourceFile

Source

pub fn bytes(&self) -> &[u8]

The file’s contents.

Source

pub fn shared_bytes(&self) -> SourceBytes

The file’s contents, shared.

Source

pub fn len(&self) -> u32

Length in bytes.

Source

pub fn is_empty(&self) -> bool

Whether the file is empty.

Source

pub fn contains(&self, pos: BytePos) -> bool

Whether pos falls in this file.

The end position is included, so a diagnostic about something missing at the end of a file still names the file rather than falling into the gap after it.

Source

pub fn line_count(&self) -> u32

How many lines the file has, counting a trailing newline as ending the last line rather than starting another. An empty file has one line, which is empty.

Source

pub fn line_bytes(&self, line: u32) -> Option<&[u8]>

The bytes of line line, counting from one, without its line terminator.

None if the file has no such line.

Source

pub fn position(&self, pos: BytePos) -> Option<Loc>

The line and column of pos, or None if pos is not in this file.

Source

pub fn presumed_position(&self, pos: BytePos) -> Option<PresumedLoc<'_>>

Where pos is presented as being, once the #line directives in front of it are taken into account. The same as SourceFile::position for a file that has none.

Source

pub fn line_span(&self, pos: BytePos) -> Option<Span>

The span covering the line pos is on, including its terminator.

Trait Implementations§

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§

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

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.