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
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// License: see LICENSE file at root directory of `master` branch

//! # Read-only zeros

use alloc::{
    string::ToString,
    vec::Vec,
};

use core::{
    convert::TryFrom,
    fmt::{self, Display, Formatter},
    ops::Deref,
};

use crate::{Error, Z128, Z224, Z256, Z384, Z512, Zeros};

/// # Read-only zeros
///
/// This struct is simply a read-only wrapper of [`Zeros`][::Zeros].
///
/// ## Examples
///
/// ```
/// use std::convert::TryFrom;
/// use zeros::{ReadOnlyZeros, Zeros};
///
/// const SOME_HASH: ReadOnlyZeros = ReadOnlyZeros::new(Zeros::Z128([
///     0xb2, 0xc2, 0x5d, 0x4b, 0x26, 0xd9, 0xfc, 0xb0,
///     0xb3, 0x6f, 0xc9, 0xdf, 0x33, 0xb2, 0x8b, 0x46,
/// ]));
///
/// // Zeros implements AsRef<[u8]> and TryFrom<&[u8]>
/// let another_hash = Zeros::try_from(SOME_HASH.as_ref())?;
/// assert_eq!(SOME_HASH, another_hash);
/// assert_eq!(SOME_HASH.to_string(), another_hash.to_string());
/// assert_eq!(SOME_HASH, ReadOnlyZeros::from(another_hash));
///
/// # Ok::<_, zeros::Error>(())
/// ```
///
/// [::new()]: #method.new
/// [::Zeros]: enum.Zeros.html
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub struct ReadOnlyZeros {

    /// # Zeros
    zeros: Zeros,

}

impl ReadOnlyZeros {

    /// # Makes new instance
    pub const fn new(zeros: Zeros) -> Self {
        Self {
            zeros,
        }
    }

}

impl Deref for ReadOnlyZeros {

    type Target = Zeros;

    fn deref(&self) -> &Self::Target {
        &self.zeros
    }

}

impl PartialEq<Zeros> for ReadOnlyZeros {

    fn eq(&self, other: &Zeros) -> bool {
        self.zeros.eq(other)
    }

}

impl PartialEq<ReadOnlyZeros> for Zeros {

    fn eq(&self, other: &ReadOnlyZeros) -> bool {
        self.eq(&other.zeros)
    }

}

impl Display for ReadOnlyZeros {

    fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
        f.write_str(&self.zeros.to_string())
    }

}

impl From<Zeros> for ReadOnlyZeros {

    fn from(zeros: Zeros) -> Self {
        Self::new(zeros)
    }

}

impl TryFrom<&[u8]> for ReadOnlyZeros {

    type Error = Error;

    fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
        Ok(Self::new(Zeros::try_from(bytes)?))
    }

}

impl TryFrom<Vec<u8>> for ReadOnlyZeros {

    type Error = Error;

    fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
        Self::try_from(bytes.as_slice())
    }

}

macro_rules! impl_from_slices_for_read_only_zeros { ($($ty: ty, $code: tt,)+) => {
    $(
        impl From<$ty> for ReadOnlyZeros {

            fn from(slice: $ty) -> Self {
                Self::new($code(slice))
            }

        }
    )+
}}

impl_from_slices_for_read_only_zeros! {
    Z128, (Zeros::Z128),
    Z224, (Zeros::Z224),
    Z256, (Zeros::Z256),
    Z384, (Zeros::Z384),
    Z512, (Zeros::Z512),
}