wright::source_tracking::source

Struct Source

Source
pub struct Source {
    pub id: SourceId,
    /* private fields */
}
Available on crate feature source-tracking only.
Expand description

A full source. This is usually a file, but may also be passed in the form of a string for testing.

Fields§

§id: SourceId

Globally (process-wide) unique Source ID.

It is fequently useful to have a consistient way to sort sources and check for equality between sources. This cannot be done with the Source::name since that can be FileName::None, and checking for equality of content can be an expensive process.

The id of a Source is an identifier that’s globally unique for the runtime of the program, and is assigned to the Source when it is instantiated.

Implementations§

Source§

impl Source

Source

pub fn new_from_string(name: FileName, source: String) -> Self

Create a Source using a heap allocated String.

Source

pub fn new_from_static_str(name: FileName, source: &'static str) -> Self

Create a Source from a &'static str.

Source

pub fn new_mapped_from_disk(path: PathBuf) -> Result<Self>

Available on crate feature file_memmap only.

Attempt to memory map a file from the disk into a Source. This will likely be faster than reading the file in some cases, and almost always more memory efficient.

This requires the “file_memmap” feature.

Source

pub fn new_read_from_disk(path: PathBuf) -> Result<Self>

Read a file from the disk into a source. This reads the file, which may take longer than memory mapping it as done in Self::new_mapped_from_disk. This does not require the same features and dependencies as memory mapped operations though. This stores the whole file in memory, rather than mapping virtual memory to the disk. That makes this less memory efficient than Self::new_mapped_from_disk, which may be important on systems where ram is constrained.

Use this if the “file_memmap” is not available for some reason.

Source

pub fn line_starts(&self) -> &[usize]

Get byte indices of where lines start in this Source.

Source

pub fn count_lines(&self) -> usize

Get the number of lines in this Source. This is identical to Self::line_starts length.

Source

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

Get the line index that a byte index is on in this Source.

If the byte index is greater than the length of the Source then the highest possible index will be returned.

Source

pub fn get_line(self: Arc<Source>, line_index: usize) -> Fragment

Get a line of this Source as a Fragment. The returned Fragment will contain the line terminating characters at the end of it. If you don’t want those, use Fragment::trim_end.

Note that this uses line_index which is considered 0-indexed – when displaying line numbers to the user, remember to add 1.

§Panics
  • This will panic if you ask for a line index that’s higher than or equal to the number returned by Self::count_lines.
Source

pub fn lines(self: SourceRef) -> impl Iterator<Item = Fragment>

Get an iterator over all the lines of this Source. This calls Source::get_line for each element of the returned iterator.

The returned Fragments will contain the line terminating characters at the end of them. If you don’t want those, use Iterator::map and Fragment::trim_end.

Source

pub const fn source(&self) -> &ImmutableString

Get the the source code stored.

Source

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

Get the name of this Source.

Source

pub fn as_fragment(self: SourceRef) -> Fragment

Get the entire content of this Source as a Fragment.

Trait Implementations§

Source§

impl Debug for Source

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Source

§

impl RefUnwindSafe for Source

§

impl Send for Source

§

impl Sync for Source

§

impl Unpin for Source

§

impl UnwindSafe for Source

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize = _

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.