Merge

Trait Merge 

Source
pub trait Merge<T>
where T: AsRef<[u8]> + TryFrom<Vec<u8>>,
{ // Required method fn merge(left: &T, right: &T) -> Result<T, T::Error>; }
Expand description

Trait that merges two values of any type into a single one of the same type.

§How is merge thought to be implemented by?

This trait is thought to hash two values A and B, of any type, into a single one C of the same type. The basic idea is that C then must be able to represent the union of A and B.

§How can I implement Merge?

Merge only requires the merge method to be implemented. This trait must be implemented for a data type and a merging object (any hashing algorithm, for example). An example implementation for Blake2b hashing algorithm and H256 is given:

use enchainte_merge::traits::Merge;
use blake2b_simd::Params as HashAlgorithm;
use std::convert::{TryFrom, TryInto};

impl <T> Merge<T> for HashAlgorithm
    where
        T: AsRef<[u8]> + TryFrom<Vec<u8>>,
        <T as TryFrom<Vec<u8>>>::Error: std::fmt::Debug,
{
    fn merge(left: &T, right: &T) -> Result<T, T::Error> {
        HashAlgorithm::new()
            .hash_length(left.as_ref().len())
            .to_state()
            .update(left.as_ref())
            .update(right.as_ref())
            .finalize()
            .as_bytes()
            .to_vec()
            .try_into()
    }
}

Required Methods§

Source

fn merge(left: &T, right: &T) -> Result<T, T::Error>

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§