bloock_merge/traits.rs
1use std::convert::TryFrom;
2
3/// Trait that merges two values of any type into a single one of the same type.
4///
5/// # How is merge thought to be implemented by?
6/// This trait is thought to hash two values A and B, of any type, into a single one C of the
7/// same type. The basic idea is that C then must be able to represent the union of A and B.
8///
9/// # How can I implement Merge?
10/// Merge only requires the *merge* method to be implemented. This trait must be implemented
11/// for a data type and a merging object (any hashing algorithm, for example). An example
12/// implementation for Blake2b hashing algorithm and H256 is given:
13/// ```ignore
14/// use enchainte_merge::traits::Merge;
15/// use blake2b_simd::Params as HashAlgorithm;
16/// use std::convert::{TryFrom, TryInto};
17///
18/// impl <T> Merge<T> for HashAlgorithm
19/// where
20/// T: AsRef<[u8]> + TryFrom<Vec<u8>>,
21/// <T as TryFrom<Vec<u8>>>::Error: std::fmt::Debug,
22/// {
23/// fn merge(left: &T, right: &T) -> Result<T, T::Error> {
24/// HashAlgorithm::new()
25/// .hash_length(left.as_ref().len())
26/// .to_state()
27/// .update(left.as_ref())
28/// .update(right.as_ref())
29/// .finalize()
30/// .as_bytes()
31/// .to_vec()
32/// .try_into()
33/// }
34/// }
35/// ```
36pub trait Merge<T>
37where
38 T: AsRef<[u8]> + TryFrom<Vec<u8>>,
39{
40 fn merge(left: &T, right: &T) -> Result<T, T::Error>;
41}