pub struct HashValue { /* private fields */ }
Expand description
Output value of our hash function. Intentionally opaque for safety and modularity.
Implementations§
§impl HashValue
impl HashValue
pub const LENGTH_IN_BITS: usize = 256usize
pub const LENGTH_IN_BITS: usize = 256usize
The length of the hash in bits.
pub fn from_slice<T>(bytes: T) -> Result<HashValue, HashValueParseError>where
T: AsRef<[u8]>,
pub fn from_slice<T>(bytes: T) -> Result<HashValue, HashValueParseError>where T: AsRef<[u8]>,
Create from a slice (e.g. retrieved from storage).
pub fn random_with_rng<R>(rng: &mut R) -> HashValuewhere
R: Rng,
pub fn random_with_rng<R>(rng: &mut R) -> HashValuewhere R: Rng,
Creates a random instance with given rng. Useful in unit tests.
pub fn sha3_256_of(buffer: &[u8]) -> HashValue
pub fn sha3_256_of(buffer: &[u8]) -> HashValue
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.
pub fn iter_bits(&self) -> HashValueBitIterator<'_> ⓘ
pub fn iter_bits(&self) -> HashValueBitIterator<'_> ⓘ
Returns a HashValueBitIterator
over all the bits that represent this HashValue
.
pub fn from_bit_iter(
iter: impl ExactSizeIterator<Item = bool>
) -> Result<HashValue, HashValueParseError>
pub fn from_bit_iter( iter: impl ExactSizeIterator<Item = bool> ) -> Result<HashValue, HashValueParseError>
Constructs a HashValue
from an iterator of bits.
pub 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§
§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
.§fn arbitrary_with(
_top: <HashValue as Arbitrary>::Parameters
) -> <HashValue as Arbitrary>::Strategy
fn arbitrary_with( _top: <HashValue as Arbitrary>::Parameters ) -> <HashValue as Arbitrary>::Strategy
§impl<'de> Deserialize<'de> for HashValue
impl<'de> Deserialize<'de> for HashValue
§fn deserialize<D>(
deserializer: D
) -> Result<HashValue, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>( deserializer: D ) -> Result<HashValue, <D as Deserializer<'de>>::Error>where D: Deserializer<'de>,
§impl FromStr for HashValue
impl FromStr for HashValue
§type Err = HashValueParseError
type Err = HashValueParseError
§impl Ord for HashValue
impl Ord for HashValue
§impl PartialOrd<HashValue> for HashValue
impl PartialOrd<HashValue> for HashValue
§fn partial_cmp(&self, other: &HashValue) -> Option<Ordering>
fn partial_cmp(&self, other: &HashValue) -> Option<Ordering>
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§impl Serialize for HashValue
impl Serialize for HashValue
§fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>( &self, serializer: S ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where S: Serializer,
impl Copy for HashValue
impl Eq for HashValue
impl StructuralEq for HashValue
impl StructuralPartialEq for HashValue
Auto Trait Implementations§
impl RefUnwindSafe for HashValue
impl Send for HashValue
impl Sync for HashValue
impl Unpin for HashValue
impl UnwindSafe for HashValue
Blanket Implementations§
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.