ps_hash/methods/
compact.rs

1use crate::{Hash, HASH_SIZE_COMPACT};
2
3impl Hash {
4    /// Produces a compact binary form of this [`Hash`].
5    ///
6    /// To turn the binary form back into a [`Hash`], use [`Hash::validate`].
7    #[inline]
8    #[must_use]
9    pub fn compact(&self) -> &[u8] {
10        &self.inner[..HASH_SIZE_COMPACT]
11    }
12}
13
14#[cfg(test)]
15mod tests {
16    use std::error::Error;
17
18    use crate::{hash, Hash};
19
20    #[test]
21    fn roundtrip() -> Result<(), Box<dyn Error>> {
22        for i in 0..1000 {
23            let input = "X".repeat(i);
24            let h = hash(&input)?;
25            let mut c = h.compact();
26
27            let r1 = Hash::validate(&c)?;
28
29            assert_eq!(r1, h, "validated should equal original");
30
31            let r2 = Hash::validate(&mut c)?;
32
33            assert_eq!(r1, r2, "validated hashes should be equal");
34        }
35
36        Ok(())
37    }
38}