regex_automata::util::primitives

Struct SmallIndex

Source
pub struct SmallIndex(/* private fields */);
Expand description

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:

  • PatternID is for representing the identifiers of patterns.
  • StateID is 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§

Source§

impl SmallIndex

Source

pub const MAX: SmallIndex = _

Available on 32-bit or 64-bit only.

The maximum index value.

Source

pub const LIMIT: usize = 2_147_483_647usize

The total number of values that can be represented as a small index.

Source

pub const ZERO: SmallIndex = _

The zero index value.

Source

pub const SIZE: usize = 4usize

The number of bytes that a single small index uses in memory.

Source

pub fn new(index: usize) -> Result<SmallIndex, SmallIndexError>

Create a new small index.

If the given index exceeds SmallIndex::MAX, then this returns an error.

Source

pub const fn new_unchecked(index: usize) -> SmallIndex

Create 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 SmallIndex to be within a certain range for memory safety.

Source

pub fn must(index: usize) -> SmallIndex

Like SmallIndex::new, but panics if the given index is not valid.

Source

pub const fn as_usize(&self) -> usize

Return this small index as a usize. This is guaranteed to never overflow usize.

Source

pub const fn as_u64(&self) -> u64

Return this small index as a u64. This is guaranteed to never overflow.

Source

pub const fn as_u32(&self) -> u32

Return the internal u32 of this small index. This is guaranteed to never overflow u32.

Source

pub const fn as_i32(&self) -> i32

Return the internal u32 of this small index represented as an i32. This is guaranteed to never overflow an i32.

Source

pub fn one_more(&self) -> usize

Returns one more than this small index as a usize.

Since a small index has constraints on its maximum value, adding 1 to it will always fit in a usize, u32 and a i32.

Source

pub 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.

Source

pub fn from_ne_bytes_unchecked(bytes: [u8; 4]) -> SmallIndex

Decode this small index from the bytes given using the native endian byte order for the current target.

This is analogous to SmallIndex::new_unchecked in that is does not check whether the decoded integer is representable as a small index.

Source

pub fn to_ne_bytes(&self) -> [u8; 4]

Return the underlying small index integer as raw bytes in native endian format.

Trait Implementations§

Source§

impl Clone for SmallIndex

Source§

fn clone(&self) -> SmallIndex

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SmallIndex

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SmallIndex

Source§

fn default() -> SmallIndex

Returns the “default value” for a type. Read more
Source§

impl From<u8> for SmallIndex

Source§

fn from(index: u8) -> SmallIndex

Converts to this type from the input type.
Source§

impl Hash for SmallIndex

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> Index<SmallIndex> for [T]

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: SmallIndex) -> &T

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> Index<SmallIndex> for Vec<T>

Available on crate feature alloc only.
Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: SmallIndex) -> &T

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<SmallIndex> for [T]

Source§

fn index_mut(&mut self, index: SmallIndex) -> &mut T

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<SmallIndex> for Vec<T>

Available on crate feature alloc only.
Source§

fn index_mut(&mut self, index: SmallIndex) -> &mut T

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl Ord for SmallIndex

Source§

fn cmp(&self, other: &SmallIndex) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for SmallIndex

Source§

fn eq(&self, other: &SmallIndex) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for SmallIndex

Source§

fn partial_cmp(&self, other: &SmallIndex) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl TryFrom<u16> for SmallIndex

Source§

type Error = SmallIndexError

The type returned in the event of a conversion error.
Source§

fn try_from(index: u16) -> Result<SmallIndex, SmallIndexError>

Performs the conversion.
Source§

impl TryFrom<u32> for SmallIndex

Source§

type Error = SmallIndexError

The type returned in the event of a conversion error.
Source§

fn try_from(index: u32) -> Result<SmallIndex, SmallIndexError>

Performs the conversion.
Source§

impl TryFrom<u64> for SmallIndex

Source§

type Error = SmallIndexError

The type returned in the event of a conversion error.
Source§

fn try_from(index: u64) -> Result<SmallIndex, SmallIndexError>

Performs the conversion.
Source§

impl TryFrom<usize> for SmallIndex

Source§

type Error = SmallIndexError

The type returned in the event of a conversion error.
Source§

fn try_from(index: usize) -> Result<SmallIndex, SmallIndexError>

Performs the conversion.
Source§

impl Copy for SmallIndex

Source§

impl Eq for SmallIndex

Source§

impl StructuralPartialEq for SmallIndex

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.