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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//! Macro for defining a new hash type.

/// Instantiate a new marked digest. Wraps the output of some type that implemented `digest::Digest`
macro_rules! marked_digest {
    (
        $(#[$outer:meta])*
        $marked_name:ident, $digest:ty
    ) => {
        $(#[$outer])*
        #[derive(Copy, Clone, Debug, Eq, PartialEq, Default, Hash, PartialOrd, Ord)]
        pub struct $marked_name($crate::hashes::DigestOutput<$digest>);

        impl $marked_name {
            /// Unwrap the marked digest, returning the underlying `GenericArray`
            pub fn to_internal(self) -> $crate::hashes::DigestOutput<$digest> {
                self.0
            }
        }

        impl $crate::hashes::MarkedDigestOutput for $marked_name {
            fn size(&self) -> usize {
                self.0.len()
            }
        }

        impl<T> From<T> for $marked_name
        where
            T: Into<$crate::hashes::DigestOutput<$digest>>
        {
            fn from(t: T) -> Self {
                $marked_name(t.into())
            }
        }

        impl $crate::hashes::MarkedDigest<$marked_name> for $digest {
            fn finalize_marked(self) -> $marked_name {
                $marked_name($crate::hashes::Digest::finalize(self))
            }

            fn digest_marked(data: &[u8]) -> $marked_name {
                $marked_name(<$digest as $crate::hashes::Digest>::digest(data))
            }
        }

        impl AsRef<$crate::hashes::DigestOutput<$digest>> for $marked_name {
            fn as_ref(&self) -> &$crate::hashes::DigestOutput<$digest> {
                &self.0
            }
        }

        impl AsMut<$crate::hashes::DigestOutput<$digest>> for $marked_name {
            fn as_mut(&mut self) -> &mut $crate::hashes::DigestOutput<$digest> {
                &mut self.0
            }
        }

        impl AsRef<[u8]> for $marked_name {
            fn as_ref(&self) -> &[u8] {
                self.0.as_ref()
            }
        }

        impl AsMut<[u8]> for $marked_name {
            fn as_mut(&mut self) -> &mut [u8] {
                self.0.as_mut()
            }
        }

        impl $crate::ser::ByteFormat for $marked_name {
            type Error = $crate::ser::SerError;

            fn serialized_length(&self) -> usize {
                $crate::hashes::MarkedDigestOutput::size(self)
            }

            fn read_from<R>(reader: &mut R) -> $crate::ser::SerResult<Self>
            where
                R: std::io::Read,
                Self: std::marker::Sized,
            {
                let mut buf = Self::default();
                reader.read_exact(buf.as_mut())?;
                Ok(buf)
            }

            fn write_to<W>(&self, writer: &mut W) -> $crate::ser::SerResult<usize>
            where
                W: std::io::Write,
            {
                Ok(writer.write(self.as_ref())?)
            }
        }
    };
}