Struct SmallIndex
struct SmallIndex(_)
A type that represents a "small" index.
The main idea of this type is to provide something that can index memory,
but uses less memory than usize on 64-bit systems. Specifically, its
representation is always a u32 and has repr(transparent) enabled. (So
it is safe to transmute between a u32 and a SmallIndex.)
A small index is typically useful in cases where there is no practical way
that the index will overflow a 32-bit integer. A good example of this is
an NFA state. If you could somehow build an NFA with 2^30 states, its
memory usage would be exorbitant and its runtime execution would be so
slow as to be completely worthless. Therefore, this crate generally deems
it acceptable to return an error if it would otherwise build an NFA that
requires a slice longer than what a 32-bit integer can index. In exchange,
we can use 32-bit indices instead of 64-bit indices in various places.
This type ensures this by providing a constructor that will return an error if its argument cannot fit into the type. This makes it much easier to handle these sorts of boundary cases that are otherwise extremely subtle.
On all targets, this type guarantees that its value will fit in a u32,
i32, usize and an isize. This means that on 16-bit targets, for
example, this type's maximum value will never overflow an isize,
which means it will never overflow a i16 even though its internal
representation is still a u32.
The purpose for making the type fit into even signed integer types like
isize is to guarantee that the difference between any two small indices
is itself also a small index. This is useful in certain contexts, e.g.,
for delta encoding.
Other types
The following types wrap SmallIndex to provide a more focused use case:
PatternIDis for representing the identifiers of patterns.StateIDis for representing the identifiers of states in finite automata. It is used for both NFAs and DFAs.
Representation
This type is always represented internally by a u32 and is marked as
repr(transparent). Thus, this type always has the same representation as
a u32. It is thus safe to transmute between a u32 and a SmallIndex.
Indexing
For convenience, callers may use a SmallIndex to index slices.
Safety
While a SmallIndex is meant to guarantee that its value fits into usize
without using as much space as a usize on all targets, callers must
not rely on this property for safety. Callers may choose to rely on this
property for correctness however. For example, creating a SmallIndex with
an invalid value can be done in entirely safe code. This may in turn result
in panics or silent logical errors.
Implementations
impl SmallIndex
fn new(index: usize) -> Result<SmallIndex, SmallIndexError>Create a new small index.
If the given index exceeds
SmallIndex::MAX, then this returns an error.const fn new_unchecked(index: usize) -> SmallIndexCreate a new small index without checking whether the given value exceeds
SmallIndex::MAX.Using this routine with an invalid index value will result in unspecified behavior, but not undefined behavior. In particular, an invalid index value is likely to cause panics or possibly even silent logical errors.
Callers must never rely on a
SmallIndexto be within a certain range for memory safety.fn must(index: usize) -> SmallIndexLike
SmallIndex::new, but panics if the given index is not valid.const fn as_usize(self: &Self) -> usizeReturn this small index as a
usize. This is guaranteed to never overflowusize.const fn as_u64(self: &Self) -> u64Return this small index as a
u64. This is guaranteed to never overflow.const fn as_u32(self: &Self) -> u32Return the internal
u32of this small index. This is guaranteed to never overflowu32.const fn as_i32(self: &Self) -> i32Return the internal
u32of this small index represented as ani32. This is guaranteed to never overflow ani32.fn one_more(self: &Self) -> usizeReturns one more than this small index as a usize.
Since a small index has constraints on its maximum value, adding
1to it will always fit in ausize,u32and ai32.fn from_ne_bytes(bytes: [u8; 4]) -> Result<SmallIndex, SmallIndexError>Decode this small index from the bytes given using the native endian byte order for the current target.
If the decoded integer is not representable as a small index for the current target, then this returns an error.
fn from_ne_bytes_unchecked(bytes: [u8; 4]) -> SmallIndexDecode this small index from the bytes given using the native endian byte order for the current target.
This is analogous to
SmallIndex::new_uncheckedin that is does not check whether the decoded integer is representable as a small index.fn to_ne_bytes(self: &Self) -> [u8; 4]Return the underlying small index integer as raw bytes in native endian format.
impl Clone for SmallIndex
fn clone(self: &Self) -> SmallIndex
impl Copy for SmallIndex
impl Debug for SmallIndex
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl Default for SmallIndex
fn default() -> SmallIndex
impl Eq for SmallIndex
impl Freeze for SmallIndex
impl From for SmallIndex
fn from(index: u8) -> SmallIndex
impl Hash for SmallIndex
fn hash<__H: $crate::hash::Hasher>(self: &Self, state: &mut __H)
impl Ord for SmallIndex
fn cmp(self: &Self, other: &SmallIndex) -> Ordering
impl PartialEq for SmallIndex
fn eq(self: &Self, other: &SmallIndex) -> bool
impl PartialOrd for SmallIndex
fn partial_cmp(self: &Self, other: &SmallIndex) -> Option<Ordering>
impl RefUnwindSafe for SmallIndex
impl Send for SmallIndex
impl StructuralPartialEq for SmallIndex
impl Sync for SmallIndex
impl TryFrom for SmallIndex
fn try_from(index: u16) -> Result<SmallIndex, SmallIndexError>
impl TryFrom for SmallIndex
fn try_from(index: u32) -> Result<SmallIndex, SmallIndexError>
impl TryFrom for SmallIndex
fn try_from(index: u64) -> Result<SmallIndex, SmallIndexError>
impl TryFrom for SmallIndex
fn try_from(index: usize) -> Result<SmallIndex, SmallIndexError>
impl Unpin for SmallIndex
impl UnsafeUnpin for SmallIndex
impl UnwindSafe for SmallIndex
impl<T> Any for SmallIndex
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for SmallIndex
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for SmallIndex
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for SmallIndex
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for SmallIndex
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for SmallIndex
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T, U> Into for SmallIndex
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for SmallIndex
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for SmallIndex
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>