Struct aptos_crypto::hash::HashValue
source · [−]pub struct HashValue { /* private fields */ }
Expand description
Output value of our hash function. Intentionally opaque for safety and modularity.
Implementations
sourceimpl 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 to_vec(&self) -> Vec<u8>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
pub fn to_vec(&self) -> Vec<u8>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
A: Allocator,
Dumps into a vector.
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<'_>ⓘNotable traits for HashValueBitIterator<'a>impl<'a> Iterator for HashValueBitIterator<'a> type Item = bool;
pub fn iter_bits(&self) -> HashValueBitIterator<'_>ⓘNotable traits for HashValueBitIterator<'a>impl<'a> Iterator for HashValueBitIterator<'a> type Item = bool;
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.
sourcepub fn to_hex_literal(&self) -> String
pub fn to_hex_literal(&self) -> String
Full hex representation of a given hash value with 0x
prefix.
Methods from Deref<Target = [u8; 32]>
1.57.0 · sourcepub fn as_slice(&self) -> &[T]ⓘNotable traits for &mut [u8]impl Write for &mut [u8]impl Read for &[u8]
pub fn as_slice(&self) -> &[T]ⓘNotable traits for &mut [u8]impl Write for &mut [u8]impl Read for &[u8]
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
sourceimpl<'de> Deserialize<'de> for HashValue
impl<'de> Deserialize<'de> for HashValue
sourcefn 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>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl FromStr for HashValue
impl FromStr for HashValue
type Err = HashValueParseError
type Err = HashValueParseError
The associated error which can be returned from parsing.
sourceimpl Ord for HashValue
impl Ord for HashValue
sourceimpl PartialOrd<HashValue> for HashValue
impl PartialOrd<HashValue> for HashValue
sourcefn partial_cmp(&self, other: &HashValue) -> Option<Ordering>
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 · sourcefn lt(&self, other: &Rhs) -> bool
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 · sourcefn le(&self, other: &Rhs) -> bool
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
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> TestOnlyHash for T where
T: Serialize + ?Sized,
impl<T> TestOnlyHash for T where
T: Serialize + ?Sized,
sourcefn test_only_hash(&self) -> HashValue
fn test_only_hash(&self) -> HashValue
Generates a hash used only for tests.