Skip to main content

TextRangeExt

Trait TextRangeExt 

Source
pub trait TextRangeExt {
    // Required methods
    fn as_byte_range(&self) -> Range<usize>;
    fn as_u32_range(&self) -> Range<u32>;
    fn intersects(&self, other: TextRange) -> bool;
}
Expand description

Convenience conversions on top of TextRange.

TextRange already exposes start() and end() returning TextSize, plus Into<u32> / Into<usize> on TextSize. This trait just bundles the two-line adapter that consumers kept duplicating into a single call site.

Required Methods§

Source

fn as_byte_range(&self) -> Range<usize>

Convert to a half-open byte range suitable for slicing &str or passing to codespan_reporting. Equivalent to usize::from(self.start())..usize::from(self.end()).

Source

fn as_u32_range(&self) -> Range<u32>

Convert to a half-open u32 range. Useful for FFI / wire formats that prefer u32 offsets over usize.

Source

fn intersects(&self, other: TextRange) -> bool

Returns true if the two ranges share at least one byte position under the half-open convention. Zero-width ranges are handled correctly — a caret at 10..10 intersects 10..15 but not 5..9.

Implementors§