pub struct BytePos(/* private fields */);Expand description
A zero-based byte offset into a single source buffer.
BytePos is a Copy newtype over a u32, so it is eight-times cheaper to
move than a usize pair and fits two-to-a-cache-line inside a
Span. The 32-bit width bounds a single source to 4 GiB, which
is the addressing envelope language front-ends use; a larger source belongs in
a multi-file mapping above this crate, not in a wider offset here.
The offset is a byte index, not a character index — it may only legally fall
on a UTF-8 character boundary. Resolving an offset that lands inside a
multi-byte sequence is defined (it rounds down) rather than undefined; see
LineIndex::line_col.
§Examples
use span_lang::BytePos;
let p = BytePos::new(42);
assert_eq!(p.to_u32(), 42);
assert_eq!(p.to_usize(), 42);
// Ordered, so positions sort and compare naturally.
assert!(BytePos::new(1) < BytePos::new(2));Implementations§
Source§impl BytePos
impl BytePos
Sourcepub const fn new(offset: u32) -> Self
pub const fn new(offset: u32) -> Self
Constructs a position from a raw byte offset.
§Examples
use span_lang::BytePos;
const START: BytePos = BytePos::new(0);
assert_eq!(START.to_u32(), 0);Trait Implementations§
impl Copy for BytePos
Source§impl<'de> Deserialize<'de> for BytePos
impl<'de> Deserialize<'de> for BytePos
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl Eq for BytePos
Source§impl Ord for BytePos
impl Ord for BytePos
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl PartialOrd for BytePos
impl PartialOrd for BytePos
impl StructuralPartialEq for BytePos
Auto Trait Implementations§
impl Freeze for BytePos
impl RefUnwindSafe for BytePos
impl Send for BytePos
impl Sync for BytePos
impl Unpin for BytePos
impl UnsafeUnpin for BytePos
impl UnwindSafe for BytePos
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