I am trying to initalize a struct H256 and passing in data as the parameter. Here is the trait and struct definition:
/// An object that can be meaningfully hashed.
pub trait Hashable {
/// Hash the object using SHA256.
fn hash(&self) -> H256;
}
/// A SHA256 hash.
#[derive(Eq, PartialEq, Serialize, Deserialize, Clone, Hash, Default, Copy)]
pub struct H256(pub [u8; 32]); // big endian u256
impl Hashable for H256 {
fn hash(&self) -> H256 {
ring::digest::digest(&ring::digest::SHA256, &self.0).into()
}
}
Here is another struct's method where I initalize the struct H256:
impl MerkleTree {
pub fn new<T>(data: &[T]) -> Self
where
T: Hashable,
{
let a = H256(data);
}
...
}
Here is the error :mismatched types expected array [u8; 32]found reference&[T]`
What is the problem?
u8s but you pass slice ofTs&[T]and[u8; 32]are not the same type. For one, the former takes an arbitrary typeT, whereas the latter only has concrete types. Even correcting that (that is, considering&[u8]instead), the former is a pointer to an array, not an array. Finally, even considering[u8]instead of[u8; 32], the latter exactly has32elements, whereas for the second one this is a runtime information.