cache_any/
cacheable.rs

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use std::fmt::Debug;
use std::io::Cursor;
use std::sync::Arc;
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};

/// used to convert [`Cacheable`] to bytes and vice versa.
pub trait Cacheable: Debug {
    /// Convert [`Cacheable`] to bytes.
    fn to_bytes(&self) -> Vec<u8>;

    /// Convert bytes to [`Cacheable`].
    fn from_bytes(bytes: &[u8]) -> anyhow::Result<Self>
    where
        Self: Sized;

    fn to_hex(&self) -> String {
        hex::encode(self.to_bytes())
    }

    fn from_hex(hex: &str) -> anyhow::Result<Self>
    where
        Self: Sized,
    {
        let bytes = hex::decode(hex)?;
        Self::from_bytes(&bytes)
    }
}

impl Cacheable for () {
    fn to_bytes(&self) -> Vec<u8> {
        vec![]
    }

    fn from_bytes(_bytes: &[u8]) -> anyhow::Result<Self> {
        Ok(())
    }
}

impl<T: Cacheable> Cacheable for Arc<T> {
    fn to_bytes(&self) -> Vec<u8> {
        self.as_ref().to_bytes()
    }

    fn from_bytes(bytes: &[u8]) -> anyhow::Result<Self>
    where
        Self: Sized,
    {
        Ok(Arc::new(T::from_bytes(bytes)?))
    }
}

impl Cacheable for Vec<u8> {
    fn to_bytes(&self) -> Vec<u8> {
        self.clone()
    }

    fn from_bytes(bytes: &[u8]) -> anyhow::Result<Self>
    where
        Self: Sized,
    {
        Ok(bytes.to_vec())
    }
}

impl Cacheable for String {
    fn to_bytes(&self) -> Vec<u8> {
        self.as_bytes().to_vec()
    }

    fn from_bytes(bytes: &[u8]) -> anyhow::Result<Self>
    where
        Self: Sized,
    {
        Ok(Self::from_utf8(bytes.to_vec())?)
    }
}

macro_rules! impl_numeric {
    ($ty: ty) => {
        impl Cacheable for $ty {
            fn to_bytes(&self) -> Vec<u8> {
                let num = *self as u128;
                let mut wtr = Vec::with_capacity(16);
                wtr.write_u128::<BigEndian>(num).unwrap();

                wtr
            }

            fn from_bytes(bytes: &[u8]) -> anyhow::Result<Self>
            where
                Self: Sized
            {
                let mut rdr = Cursor::new(bytes);
                let num = rdr.read_u128::<BigEndian>().unwrap();

                Ok(num as $ty)
            }
        }
    };
    ($($ty: ty),+ $(,)?) => {
        $(
            impl_numeric!($ty);
        )*
    };
}

impl_numeric!(
    u128, i128,
    u64, i64,
    u32, i32,
    u16, i16,
    u8, i8,
    usize, isize,
);

impl Cacheable for bool {
    fn to_bytes(&self) -> Vec<u8> {
        if *self {
            Cacheable::to_bytes(&1)
        } else {
            Cacheable::to_bytes(&0)
        }
    }

    fn from_bytes(bytes: &[u8]) -> anyhow::Result<Self>
    where
        Self: Sized,
    {
        Ok(!bytes.iter().all(|byte| *byte == 0))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rand::{random, Rng, thread_rng};
    use rand::distributions::Alphanumeric;

    #[tokio::test]
    async fn it_works() -> anyhow::Result<()> { Ok(()) }

    #[test]
    fn test_string() -> anyhow::Result<()> {
        for _ in 0..1024 {
            let t: String = (0..32)
                .map(|_| thread_rng().sample(Alphanumeric) as char)
                .collect();

            let v = Cacheable::to_bytes(&t);
            let d: String = Cacheable::from_bytes(&v).unwrap();

            assert_eq!(t, d);
        }

        Ok(())
    }

    #[test]
    fn test_numeric() -> anyhow::Result<()> {
        macro_rules! test {
            ($ty: ty) => {
                for _ in 0..1024 {
                    let num: $ty = random();
                    let v = Cacheable::to_bytes(&num);
                    let d: $ty = Cacheable::from_bytes(&v).unwrap();
                    assert_eq!(num, d);
                }
            };
            ($($ty: ty),+ $(,)?) => {
                $(test!($ty);)+
            };
        }

        test!(
            u128, i128,
            u64, i64,
            u32, i32,
            u16, i16,
            u8, i8,
            usize, isize,
        );

        macro_rules! test_arc {
            ($ty: ty) => {
                for _ in 0..1024 {
                    let num: $ty = rand::thread_rng().gen();
                    let num = Arc::new(num);
                    let v = Cacheable::to_bytes(&num);
                    let arc_d: Arc<$ty> = Cacheable::from_bytes(&v).unwrap();
                    let d: $ty = Cacheable::from_bytes(&v).unwrap();
                    assert_eq!(num, arc_d);
                    assert_eq!(d, *arc_d);
                }
            };
            ($($ty: ty),+ $(,)?) => {
                $(test_arc!($ty);)+
            };
        }

        test_arc!(
            u128, i128,
            u64, i64,
            u32, i32,
            u16, i16,
            u8, i8,
            usize, isize,
        );

        Ok(())
    }

    #[test]
    fn test_boolean() -> anyhow::Result<()> {
        for _ in 0..1024 {
            let b: bool = random();
            let v = b.to_bytes();
            let d: bool = Cacheable::from_bytes(&v).unwrap();

            assert_eq!(b, d);
        }

        Ok(())
    }
}