Skip to main content

Offset

Trait Offset 

Source
pub trait Offset:
    Copy
    + Send
    + Sync
    + 'static {
    const TYPE_NAME: &'static str;

    // Required methods
    fn try_from_usize(value: usize) -> Option<Self>;
    fn to_usize(self) -> usize;
}
Expand description

Contract for integer types used as byte offsets.

Unlike crate::StringIndex (which bounds string count), Offset bounds total UTF-8 byte size. Builders and validators use Self::try_from_usize for checked growth, while lookup code uses Self::to_usize for infallible slicing.

§Implementing this trait

This trait is already implemented for primitive unsigned integers (u8, u16, u32, u64, usize). To implement it for custom wrapper types, use the impl_offset macro:

use lite_strtab::impl_offset;

#[derive(Clone, Copy)]
#[repr(transparent)]
struct ByteOffset(u32);

impl_offset!(ByteOffset: u32);

Required Associated Constants§

Source

const TYPE_NAME: &'static str

Human-readable type name.

Required Methods§

Source

fn try_from_usize(value: usize) -> Option<Self>

Converts a byte length/offset into this type.

Source

fn to_usize(self) -> usize

Converts this offset to usize.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Offset for u8

Source§

const TYPE_NAME: &'static str = "u8"

Source§

fn try_from_usize(value: usize) -> Option<Self>

Source§

fn to_usize(self) -> usize

Source§

impl Offset for u16

Source§

const TYPE_NAME: &'static str = "u16"

Source§

fn try_from_usize(value: usize) -> Option<Self>

Source§

fn to_usize(self) -> usize

Source§

impl Offset for u32

Source§

const TYPE_NAME: &'static str = "u32"

Source§

fn try_from_usize(value: usize) -> Option<Self>

Source§

fn to_usize(self) -> usize

Source§

impl Offset for u64

Source§

const TYPE_NAME: &'static str = "u64"

Source§

fn try_from_usize(value: usize) -> Option<Self>

Source§

fn to_usize(self) -> usize

Source§

impl Offset for usize

Source§

const TYPE_NAME: &'static str = "usize"

Source§

fn try_from_usize(value: usize) -> Option<Self>

Source§

fn to_usize(self) -> usize

Implementors§