Skip to main content

SourceFile

Struct SourceFile 

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

A single source held by a SourceMap: a display name, the owned source text, and the half-open Span the text occupies in the map’s global position space.

The map owns the text so that everything above it can borrow &str for the life of the map without re-reading or copying — a line index, a lexer, a diagnostic renderer all read through this borrow. The span is the file’s footprint in the shared coordinate space: its start is the global offset of the first byte, and a local offset within the file plus that start is the corresponding global position.

Construction is internal; a SourceFile only ever comes from SourceMap::add or SourceMap::source, so its span is always consistent with the map that produced it.

§Examples

use source_lang::SourceMap;

let mut map = SourceMap::new();
let id = map.add("greeting.txt", "hello").expect("fits");
let file = map.source(id).expect("just added");

assert_eq!(file.name(), "greeting.txt");
assert_eq!(file.text(), "hello");
assert_eq!(file.span().len(), 5);

Implementations§

Source§

impl SourceFile

Source

pub fn name(&self) -> &str

Returns the source’s display name — the path or label it was added under.

This is purely a label for diagnostics; the map does not interpret it, so two sources may share a name and still be distinct entries.

§Examples
use source_lang::SourceMap;

let mut map = SourceMap::new();
let id = map.add("src/main.rs", "fn main() {}").expect("fits");
assert_eq!(map.source(id).unwrap().name(), "src/main.rs");
Source

pub fn text(&self) -> &str

Returns the source text, borrowed for the life of the map.

§Examples
use source_lang::SourceMap;

let mut map = SourceMap::new();
let id = map.add("note.txt", "first line\nsecond line").expect("fits");
let text = map.source(id).unwrap().text();
assert_eq!(text.lines().count(), 2);
Source

pub const fn span(&self) -> Span

Returns the file’s half-open range in the map’s global position space.

span().start() is the global offset of the file’s first byte; the file covers start..start + text().len(). Subtracting start from a global position that falls in this range gives the local offset into text — which is exactly what SourceMap::locate returns.

§Examples
use source_lang::SourceMap;

let mut map = SourceMap::new();
let first = map.add("a.txt", "abc").expect("fits");   // global 0..3
let second = map.add("b.txt", "de").expect("fits");   // global 3..5

assert_eq!(map.source(first).unwrap().span().start().to_u32(), 0);
assert_eq!(map.source(second).unwrap().span().start().to_u32(), 3);
Source

pub fn line_index(&self) -> LineIndex<'_>

Builds a reusable line index over this source’s text.

The returned LineIndex borrows the source for as long as the SourceFile is borrowed, so it can be kept and queried many times without re-scanning. Building it is the only O(text len) step; each line_col / offset lookup on it is sub-linear.

Prefer this over SourceMap::line_col when resolving several positions within one source — that convenience method builds a fresh index per call, whereas this builds it once.

§Examples
use source_lang::{BytePos, LineCol, SourceMap};

let mut map = SourceMap::new();
let id = map.add("m.rs", "let x = 1;\nlet y = 2;").expect("fits");
let index = map.source(id).unwrap().line_index();

// Resolve as many local positions as needed against the one index.
assert_eq!(index.line_col(BytePos::new(0)), LineCol::new(1, 1));
assert_eq!(index.line_col(BytePos::new(11)), LineCol::new(2, 1));
assert_eq!(index.line_count(), 2);

Trait Implementations§

Source§

impl Clone for SourceFile

Source§

fn clone(&self) -> SourceFile

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
Source§

impl Eq for SourceFile

Source§

impl PartialEq for SourceFile

Source§

fn eq(&self, other: &SourceFile) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for SourceFile

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.