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
impl HashValue
sourcepub const LENGTH_IN_BITS: usize = 256usize
pub const LENGTH_IN_BITS: usize = 256usize
The length of the hash in bits.
sourcepub fn from_slice<T: AsRef<[u8]>>(bytes: T) -> Result<Self, HashValueParseError>
pub fn from_slice<T: AsRef<[u8]>>(bytes: T) -> Result<Self, HashValueParseError>
Create from a slice (e.g. retrieved from storage).
sourcepub fn random_with_rng<R: Rng>(rng: &mut R) -> Self
pub fn random_with_rng<R: Rng>(rng: &mut R) -> Self
Creates a random instance with given rng. Useful in unit tests.
sourcepub fn sha3_256_of(buffer: &[u8]) -> Self
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.
sourcepub fn iter_bits(&self) -> HashValueBitIterator<'_> ⓘ
pub fn iter_bits(&self) -> HashValueBitIterator<'_> ⓘ
Returns a HashValueBitIterator
over all the bits that represent this HashValue
.
sourcepub fn from_bit_iter(
iter: impl ExactSizeIterator<Item = bool>
) -> Result<Self, HashValueParseError>
pub fn from_bit_iter( iter: impl ExactSizeIterator<Item = bool> ) -> Result<Self, HashValueParseError>
Constructs a HashValue
from an iterator of bits.
sourcepub fn common_prefix_bits_len(&self, other: HashValue) -> usize
pub fn common_prefix_bits_len(&self, other: HashValue) -> usize
Returns the length of common prefix of self
and other
in bits.
Methods from Deref<Target = [u8; 32]>§
1.57.0 · sourcepub fn as_slice(&self) -> &[T] ⓘ
pub fn as_slice(&self) -> &[T] ⓘ
Returns a slice containing the entire array. Equivalent to &s[..]
.
sourcepub fn each_ref(&self) -> [&T; N]
🔬This is a nightly-only experimental API. (array_methods
)
pub fn each_ref(&self) -> [&T; N]
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);
sourcepub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])
🔬This is a nightly-only experimental API. (split_array
)
pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])
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, &[]);
}
sourcepub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])
🔬This is a nightly-only experimental API. (split_array
)
pub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])
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
impl Arbitrary for HashValue
§type Parameters = <[u8; 32] as Arbitrary>::Parameters
type Parameters = <[u8; 32] as Arbitrary>::Parameters
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>
type Strategy = Map<<[u8; 32] as Arbitrary>::Strategy, fn(_: [u8; 32]) -> HashValue>
Strategy
used to generate values of type Self
.source§fn arbitrary_with(_top: Self::Parameters) -> Self::Strategy
fn arbitrary_with(_top: Self::Parameters) -> Self::Strategy
source§impl<'de> Deserialize<'de> for HashValue
impl<'de> Deserialize<'de> for HashValue
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where D: Deserializer<'de>,
source§impl FromStr for HashValue
impl FromStr for HashValue
§type Err = HashValueParseError
type Err = HashValueParseError
source§impl Ord for HashValue
impl Ord for HashValue
source§impl PartialEq<HashValue> for HashValue
impl PartialEq<HashValue> for HashValue
source§impl PartialOrd<HashValue> for HashValue
impl PartialOrd<HashValue> for HashValue
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more