1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::fmt;
use std::hash::Hasher;
use std::io::{self, Write};
use std::marker::PhantomData;

use byteorder::{LittleEndian, WriteBytesExt};

use {ByteHash, State};

/// Wrapping any `Hasher` in ByteHash
#[derive(Debug, Clone)]
pub struct Wrapped<H>(PhantomData<H>);

/// Wrapped state for computing hashes
#[derive(Default)]
pub struct WrappedState<H>
where
    H: Default,
{
    buf: [u8; 8],
    state: H,
}

impl<H: Hasher> Write for WrappedState<H>
where
    H: Default,
{
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.state.write(buf);
        Ok(buf.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

impl<H> ByteHash for Wrapped<H>
where
    H: 'static + Hasher + Default + Clone + fmt::Debug,
{
    type Digest = [u8; 8];
    type State = WrappedState<H>;

    fn state() -> Self::State {
        WrappedState::default()
    }
}

impl<H> State<[u8; 8]> for WrappedState<H>
where
    H: Hasher + Default,
{
    fn fin(self) -> [u8; 8] {
        let WrappedState { state, mut buf } = self;
        buf.as_mut()
            .write_u64::<LittleEndian>(state.finish())
            .expect("in-memory write");
        buf
    }
}