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: usizeImplementations§
Source§impl SourceFile
impl SourceFile
Sourcepub fn filename(&self) -> Option<&str>
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
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of this SourceFile
This is the same as self.contents.len()
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if this SourceFile is empty
This is the same as self.contents.is_empty()
Sourcepub fn into_parts(&self) -> (Rc<str>, usize)
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());Sourcepub const fn offset(&self) -> usize
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());pub fn contains_span(&self, span: &Span) -> bool
Sourcepub fn slice(&self, span: &Span) -> &str
pub fn slice(&self, span: &Span) -> &str
Slices the given span
NOTE: span must be contained in this SourceFile
Sourcepub fn file_position(&self, span: &Span) -> FilePosition
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
pub const fn id(&self) -> FileId
pub fn path(&self) -> Option<&Path>
Trait Implementations§
Source§impl Clone for SourceFile
impl Clone for SourceFile
Source§fn clone(&self) -> SourceFile
fn clone(&self) -> SourceFile
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for SourceFile
impl RefUnwindSafe for SourceFile
impl !Send for SourceFile
impl !Sync for SourceFile
impl Unpin for SourceFile
impl UnwindSafe for SourceFile
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more