Struct SourceFile

Source
pub struct SourceFile {
    pub fname: FileName,
    pub contents: Rc<str>,
    pub offset: usize,
    /* private fields */
}
Expand description

A source file for the compiler

Fields§

§fname: FileName§contents: Rc<str>§offset: usize

Implementations§

Source§

impl SourceFile

Source

pub fn filename(&self) -> Option<&str>

Gets a string slice with the filename of this SourceFile

This method returns an Option because the FileName::Path variant stores a PathBuf, which can contain non-utf8 filenames.

It the conversion to string fails, returns None

Source

pub fn len(&self) -> usize

Returns the length of this SourceFile

This is the same as self.contents.len()

Source

pub fn is_empty(&self) -> bool

Returns true if this SourceFile is empty

This is the same as self.contents.is_empty()

Source

pub fn into_parts(&self) -> (Rc<str>, usize)

Returns a tuple with the contents and offset of this file

§Example
use span::source::{SourceMap, SourceFile};

let mut sm = SourceMap::new();

let input1 = "Hello world!";
let (contents, offset) = sm.add_file_annon(input1.into()).into_parts();
assert_eq!(&*contents, input1);
assert_eq!(offset, 0);

let input2 = "Second file";
let (contents, offset) = sm.add_file_annon(input2.into()).into_parts();
assert_eq!(&*contents, input2);
assert_eq!(offset, input1.len());
Source

pub const fn offset(&self) -> usize

Returns the absolute offset of this file inside it’s SourceMap

§Example
use span::source::{SourceMap, SourceFile};

let mut sm = SourceMap::new();

let input1 = "Hello world!";
let offset1 = sm.add_file_annon(input1.into()).offset();
assert_eq!(offset1, 0);

let input2 = "Second file";
let offset2 = sm.add_file_annon(input2.into()).offset();
assert_eq!(offset2, input1.len());
Source

pub fn contains_span(&self, span: &Span) -> bool

Source

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

Slices the given span

NOTE: span must be contained in this SourceFile

Source

pub fn file_position(&self, span: &Span) -> FilePosition

Returns the file position of this span inside self

NOTE: span must be contained in this SourceFile

Source

pub const fn id(&self) -> FileId

Source

pub fn path(&self) -> Option<&Path>

Trait Implementations§

Source§

impl Clone for SourceFile

Source§

fn clone(&self) -> SourceFile

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

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