Struct diem_crypto::hash::HashValue

source ·
pub struct HashValue { /* private fields */ }
Expand description

Output value of our hash function. Intentionally opaque for safety and modularity.

Implementations§

source§

impl HashValue

source

pub const LENGTH: usize = 32usize

The length of the hash in bytes.

source

pub const LENGTH_IN_BITS: usize = 256usize

The length of the hash in bits.

source

pub fn new(hash: [u8; 32]) -> Self

Create a new HashValue from a byte array.

source

pub fn from_slice<T: AsRef<[u8]>>(bytes: T) -> Result<Self, HashValueParseError>

Create from a slice (e.g. retrieved from storage).

source

pub fn to_vec(&self) -> Vec<u8>

Dumps into a vector.

source

pub const fn zero() -> Self

Creates a zero-initialized instance.

source

pub fn random() -> Self

Create a cryptographically random instance.

source

pub fn random_with_rng<R: Rng>(rng: &mut R) -> Self

Creates a random instance with given rng. Useful in unit tests.

source

pub fn sha3_256_of(buffer: &[u8]) -> Self

Convenience function that computes a HashValue internally equal to the sha3_256 of a byte buffer. It will handle hasher creation, data feeding and finalization.

Note this will not result in the <T as CryptoHash>::hash() for any reasonable struct T, as this computes a sha3 without any ornaments.

source

pub fn bit(&self, index: usize) -> bool

Returns the index-th bit in the bytes.

source

pub fn nibble(&self, index: usize) -> u8

Returns the index-th nibble in the bytes.

source

pub fn iter_bits(&self) -> HashValueBitIterator<'_>

Returns a HashValueBitIterator over all the bits that represent this HashValue.

source

pub fn from_bit_iter( iter: impl ExactSizeIterator<Item = bool> ) -> Result<Self, HashValueParseError>

Constructs a HashValue from an iterator of bits.

source

pub fn common_prefix_bits_len(&self, other: HashValue) -> usize

Returns the length of common prefix of self and other in bits.

source

pub fn to_hex(&self) -> String

Full hex representation of a given hash value.

source

pub fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, HashValueParseError>

Parse a given hex string to a hash value.

source

pub fn from_u64(v: u64) -> Self

Create a hash value whose contents are just the given integer. Useful for generating basic mock hash values.

Ex: HashValue::from_u64(0x1234) => HashValue([0, .., 0, 0x12, 0x34])

Methods from Deref<Target = [u8; 32]>§

1.57.0 · source

pub fn as_slice(&self) -> &[T]

Returns a slice containing the entire array. Equivalent to &s[..].

source

pub fn each_ref(&self) -> [&T; N]

🔬This is a nightly-only experimental API. (array_methods)

Borrows each element and returns an array of references with the same size as self.

Example
#![feature(array_methods)]

let floats = [3.1, 2.7, -1.0];
let float_refs: [&f64; 3] = floats.each_ref();
assert_eq!(float_refs, [&3.1, &2.7, &-1.0]);

This method is particularly useful if combined with other methods, like map. This way, you can avoid moving the original array if its elements are not Copy.

#![feature(array_methods)]

let strings = ["Ferris".to_string(), "♥".to_string(), "Rust".to_string()];
let is_ascii = strings.each_ref().map(|s| s.is_ascii());
assert_eq!(is_ascii, [true, false, true]);

// We can still access the original array: it has not been moved.
assert_eq!(strings.len(), 3);
source

pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])

🔬This is a nightly-only experimental API. (split_array)

Divides one array reference into two at an index.

The first will contain all indices from [0, M) (excluding the index M itself) and the second will contain all indices from [M, N) (excluding the index N itself).

Panics

Panics if M > N.

Examples
#![feature(split_array)]

let v = [1, 2, 3, 4, 5, 6];

{
   let (left, right) = v.split_array_ref::<0>();
   assert_eq!(left, &[]);
   assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}

{
    let (left, right) = v.split_array_ref::<2>();
    assert_eq!(left, &[1, 2]);
    assert_eq!(right, &[3, 4, 5, 6]);
}

{
    let (left, right) = v.split_array_ref::<6>();
    assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
    assert_eq!(right, &[]);
}
source

pub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])

🔬This is a nightly-only experimental API. (split_array)

Divides one array reference into two at an index from the end.

The first will contain all indices from [0, N - M) (excluding the index N - M itself) and the second will contain all indices from [N - M, N) (excluding the index N itself).

Panics

Panics if M > N.

Examples
#![feature(split_array)]

let v = [1, 2, 3, 4, 5, 6];

{
   let (left, right) = v.rsplit_array_ref::<0>();
   assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
   assert_eq!(right, &[]);
}

{
    let (left, right) = v.rsplit_array_ref::<2>();
    assert_eq!(left, &[1, 2, 3, 4]);
    assert_eq!(right, &[5, 6]);
}

{
    let (left, right) = v.rsplit_array_ref::<6>();
    assert_eq!(left, &[]);
    assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}

Trait Implementations§

source§

impl Arbitrary for HashValue

§

type Parameters = <[u8; 32] as Arbitrary>::Parameters

The type of parameters that arbitrary_with accepts for configuration of the generated Strategy. Parameters must implement Default.
§

type Strategy = Map<<[u8; 32] as Arbitrary>::Strategy, fn(_: [u8; 32]) -> HashValue>

The type of Strategy used to generate values of type Self.
source§

fn arbitrary_with(_top: Self::Parameters) -> Self::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). The strategy is passed the arguments given in args. Read more
§

fn arbitrary() -> Self::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). Read more
source§

impl AsRef<[u8; 32]> for HashValue

source§

fn as_ref(&self) -> &[u8; 32]

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Binary for HashValue

source§

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

Formats the value using the given formatter.
source§

impl Clone for HashValue

source§

fn clone(&self) -> HashValue

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 HashValue

source§

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

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

impl Default for HashValue

source§

fn default() -> Self

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

impl Deref for HashValue

§

type Target = [u8; 32]

The resulting type after dereferencing.
source§

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

Dereferences the value.
source§

impl<'de> Deserialize<'de> for HashValue

source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for HashValue

Will print shortened (4 bytes) hash

source§

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

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

impl From<HashValue> for Bytes

source§

fn from(value: HashValue) -> Bytes

Converts to this type from the input type.
source§

impl FromStr for HashValue

§

type Err = HashValueParseError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, HashValueParseError>

Parses a string s to return a value of this type. Read more
source§

impl Hash for HashValue

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 Index<usize> for HashValue

§

type Output = u8

The returned type after indexing.
source§

fn index(&self, s: usize) -> &u8

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

impl LowerHex for HashValue

source§

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

Formats the value using the given formatter.
source§

impl Ord for HashValue

source§

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

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

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

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

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

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

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

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

impl PartialEq<HashValue> for HashValue

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<HashValue> for HashValue

source§

fn partial_cmp(&self, other: &HashValue) -> 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

This method 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

This method 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

This method 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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for HashValue

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Copy for HashValue

source§

impl Eq for HashValue

source§

impl StructuralEq for HashValue

source§

impl StructuralPartialEq for HashValue

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> TestOnlyHash for Twhere T: Serialize + ?Sized,

source§

fn test_only_hash(&self) -> HashValue

Generates a hash used only for tests.
source§

impl<T> ToOwned for Twhere T: Clone,

§

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> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,