Struct Linearized

Source
pub struct Linearized<L>
where L: ?Sized,
{ /* private fields */ }
Expand description

Pre-computed output of Linearize::linearize.

StaticMap and StaticCopyMap can be index directly with a Linearize type. That operation computes the output of linearize ad-hoc. While the linearize function is zero cost for many types, you might be using types for which the cost is non-zero or unknown.

In such situations, the Linearized type allows you to cache the output of linearize.

It is guaranteed that the value cached by this type is the output of the linearize function. In particular, the value is less than LENGTH.

§Example

fn add_one<L: Linearize>(map: &mut StaticMap<L, u8>, key: L) {
    let key = key.linearized();
    let v = map[key];
    map[key] = v + 1;
}

§Trait Implementations

This type implements traits such as Debug, Hash, etc. These implementations operate on the pre-computed usize. In particular, the Debug implementation does not print the name of the original value used to create this object.

Implementations§

Source§

impl<L> Linearized<L>
where L: ?Sized,

Source

pub fn new(l: &L) -> Self
where L: Linearize,

Pre-computes the linearized value.

This function pre-computes the output of linearize.

The LinearizeExt extension trait provides the linearized function which does the same.

§Example
fn get_value<L: Linearize>(map: &StaticMap<L, u8>, key: L) -> u8 {
    map[Linearized::new(&key)]
    // Or: map[key.linearized()]
}
Source

pub const unsafe fn new_unchecked(index: usize) -> Self

Wraps an already computed values.

§Safety

The index must be less than L::LENGTH.

Source

pub fn get(self) -> usize

Returns the linearized value.

This function returns the output of linearize that was computed when this object was created.

§Example
fn get<L: Linearize>(key: L) {
    assert_eq!(key.linearized().get(), key.linearize());
}
Source

pub fn delinearize(self) -> L
where L: Linearize + Sized,

Returns the value that was used to create this object.

This function returns the output of from_linear_unchecked. This value is required to be the same that was used to create this object.

§Example
fn get<L: Linearize + Eq + Debug>(key: L) {
    assert_eq!(key.linearized().delinearize(), key);
}

Trait Implementations§

Source§

impl<L> Clone for Linearized<L>
where L: ?Sized,

Source§

fn clone(&self) -> Self

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<L> Debug for Linearized<L>
where L: ?Sized,

Source§

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

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

impl<L> Hash for Linearized<L>
where L: ?Sized,

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<L, T> Index<Linearized<L>> for StaticCopyMap<L, T>
where L: Linearize + ?Sized, T: Copy,

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: Linearized<L>) -> &Self::Output

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

impl<L, T> Index<Linearized<L>> for StaticMap<L, T>
where L: Linearize + ?Sized,

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: Linearized<L>) -> &Self::Output

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

impl<L, T> IndexMut<Linearized<L>> for StaticCopyMap<L, T>
where L: Linearize + ?Sized, T: Copy,

Source§

fn index_mut(&mut self, index: Linearized<L>) -> &mut Self::Output

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

impl<L, T> IndexMut<Linearized<L>> for StaticMap<L, T>
where L: Linearize + ?Sized,

Source§

fn index_mut(&mut self, index: Linearized<L>) -> &mut Self::Output

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

impl<L> Ord for Linearized<L>
where L: ?Sized,

Source§

fn cmp(&self, other: &Self) -> 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<L> PartialEq<usize> for Linearized<L>
where L: ?Sized,

Source§

fn eq(&self, other: &usize) -> 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<L> PartialEq for Linearized<L>
where L: ?Sized,

Source§

fn eq(&self, other: &Self) -> 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<L> PartialOrd<usize> for Linearized<L>
where L: ?Sized,

Source§

fn partial_cmp(&self, other: &usize) -> 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<L> PartialOrd for Linearized<L>
where L: ?Sized,

Source§

fn partial_cmp(&self, other: &Self) -> 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<L> Copy for Linearized<L>
where L: ?Sized,

Source§

impl<L> Eq for Linearized<L>
where L: ?Sized,

Auto Trait Implementations§

§

impl<L> Freeze for Linearized<L>
where L: ?Sized,

§

impl<L> RefUnwindSafe for Linearized<L>
where L: RefUnwindSafe + ?Sized,

§

impl<L> Send for Linearized<L>
where L: Send + ?Sized,

§

impl<L> Sync for Linearized<L>
where L: Sync + ?Sized,

§

impl<L> Unpin for Linearized<L>
where L: Unpin + ?Sized,

§

impl<L> UnwindSafe for Linearized<L>
where L: UnwindSafe + ?Sized,

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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.