typst_utils

Struct LazyHash

source
pub struct LazyHash<T: ?Sized> { /* private fields */ }
Expand description

A wrapper type with lazily-computed hash.

This is useful if you want to pass large values of T to memoized functions. Especially recursive structures like trees benefit from intermediate prehashed nodes.

Note that for a value v of type T, hash(v) is not necessarily equal to hash(LazyHash::new(v)). Writing the precomputed hash into a hasher’s state produces different output than writing the value’s parts directly. However, that seldom matters as you are typically either dealing with values of type T or with values of type LazyHash<T>, not a mix of both.

§Equality

Because Typst uses high-quality 128 bit hashes in all places, the risk of a hash collision is reduced to an absolute minimum. Therefore, this type additionally provides PartialEq and Eq implementations that compare by hash instead of by value. For this to be correct, your hash implementation must feed all information relevant to the PartialEq impl to the hasher.

§Usage

If the value is expected to be cloned, it is best used inside of an Arc or Rc to best re-use the hash once it has been computed.

Implementations§

source§

impl<T> LazyHash<T>

source

pub fn new(value: T) -> Self

Wraps an item without pre-computed hash.

source

pub fn reuse<U: ?Sized>(value: T, existing: &LazyHash<U>) -> Self

Wrap an item with a pre-computed hash.

Important: The hash must be correct for the value. This cannot be enforced at compile time, so use with caution.

source

pub fn into_inner(self) -> T

Returns the wrapped value.

Trait Implementations§

source§

impl<T: Hash + Clone + 'static> Clone for LazyHash<T>

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<T: Debug> Debug for LazyHash<T>

source§

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

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

impl<T: Default> Default for LazyHash<T>

source§

fn default() -> Self

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

impl<T: ?Sized> Deref for LazyHash<T>

source§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T: Hash + ?Sized + 'static> DerefMut for LazyHash<T>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl<T> From<T> for LazyHash<T>

source§

fn from(value: T) -> Self

Converts to this type from the input type.
source§

impl<T: Hash + ?Sized + 'static> Hash for LazyHash<T>

source§

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

Feeds this value into the given Hasher. Read more
source§

impl<T: Hash + ?Sized + 'static> PartialEq for LazyHash<T>

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<T: Hash + ?Sized + 'static> Eq for LazyHash<T>

Auto Trait Implementations§

§

impl<T> !Freeze for LazyHash<T>

§

impl<T> RefUnwindSafe for LazyHash<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> Send for LazyHash<T>
where T: Send + ?Sized,

§

impl<T> Sync for LazyHash<T>
where T: Sync + ?Sized,

§

impl<T> Unpin for LazyHash<T>
where T: Unpin + ?Sized,

§

impl<T> UnwindSafe for LazyHash<T>
where T: 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, 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<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
source§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.