pub struct Span {
pub file_id: FileId,
pub start: u32,
pub end: u32,
}Expand description
A span representing a range in the source code.
Spans use byte offsets into the source string and include a file identifier for multi-file compilation. They are designed to be small (12 bytes) and cheap to copy.
Fields§
§file_id: FileIdThe file this span belongs to
start: u32Start byte offset (inclusive)
end: u32End byte offset (exclusive)
Implementations§
Source§impl Span
impl Span
Sourcepub const fn new(start: u32, end: u32) -> Self
pub const fn new(start: u32, end: u32) -> Self
Create a new span from start and end byte offsets.
Uses the default file ID. For multi-file compilation, use with_file.
Sourcepub const fn with_file(file_id: FileId, start: u32, end: u32) -> Self
pub const fn with_file(file_id: FileId, start: u32, end: u32) -> Self
Create a new span with a specific file ID.
Sourcepub const fn point(pos: u32) -> Self
pub const fn point(pos: u32) -> Self
Create an empty span at a single position.
Uses the default file ID. For multi-file compilation, use point_in_file.
Sourcepub const fn point_in_file(file_id: FileId, pos: u32) -> Self
pub const fn point_in_file(file_id: FileId, pos: u32) -> Self
Create an empty span at a single position in a specific file.
Sourcepub const fn cover(a: Span, b: Span) -> Self
pub const fn cover(a: Span, b: Span) -> Self
Create a span covering two spans (from start of first to end of second).
Uses the file ID from span a. Both spans should be from the same file.
Sourcepub const fn extend_to(&self, end: u32) -> Self
pub const fn extend_to(&self, end: u32) -> Self
Extend this span to a new end position, preserving the file ID.
Creates a span from self.start to end with the same file ID.
Sourcepub const fn contains(&self, other: Span) -> bool
pub const fn contains(&self, other: Span) -> bool
Returns true if other is entirely contained within this span.
A span a contains span b if a.start <= b.start and b.end <= a.end.
An empty span at a boundary is considered contained.
§Example
use gruel_span::Span;
let outer = Span::new(5, 20);
let inner = Span::new(10, 15);
let overlapping = Span::new(15, 25);
assert!(outer.contains(inner));
assert!(!outer.contains(overlapping));
assert!(outer.contains(Span::point(10)));Sourcepub const fn contains_pos(&self, pos: u32) -> bool
pub const fn contains_pos(&self, pos: u32) -> bool
Returns true if this span contains the given byte position.
The position is contained if self.start <= pos < self.end.
Note: the end position is exclusive, so pos == self.end returns false.
§Example
use gruel_span::Span;
let span = Span::new(5, 10);
assert!(!span.contains_pos(4)); // before span
assert!(span.contains_pos(5)); // at start (inclusive)
assert!(span.contains_pos(7)); // in middle
assert!(!span.contains_pos(10)); // at end (exclusive)Sourcepub fn line_number(&self, source: &str) -> usize
pub fn line_number(&self, source: &str) -> usize
Compute the 1-based line number for this span’s start position.
Returns the line number (1-indexed) where this span begins.
§Panics
In debug builds, panics if self.start exceeds source.len().
In release builds, out-of-bounds offsets are clamped to source.len().
Sourcepub fn line_col(&self, source: &str) -> (usize, usize)
pub fn line_col(&self, source: &str) -> (usize, usize)
Compute the 1-based line and column numbers for this span’s start position.
Returns (line, column) where both are 1-indexed. The column is the
number of bytes from the start of the line, plus 1.
§Panics
In debug builds, panics if self.start exceeds source.len().
In release builds, out-of-bounds offsets are clamped to source.len().