namada_core/
borsh.rs

1//! Borsh binary encoding (re-exported) from official crate with custom
2//! ext.
3
4pub use borsh::*;
5
6/// Extensions to types implementing [`BorshSerialize`].
7pub trait BorshSerializeExt {
8    /// Serialize a value to a [`Vec`] of bytes.
9    fn serialize_to_vec(&self) -> Vec<u8>;
10}
11
12impl<T: BorshSerialize> BorshSerializeExt for T {
13    fn serialize_to_vec(&self) -> Vec<u8> {
14        let Ok(vec) = borsh::to_vec(self) else {
15            unreachable!("Serializing to a Vec should be infallible");
16        };
17        vec
18    }
19}
20
21#[cfg(test)]
22mod tests {
23    use super::*;
24
25    #[test]
26    fn test_serialize_equal() {
27        let to_seriailze =
28            "this is some cool shizzle I can guarantee that much - t. knower";
29        let serialized_this_lib = to_seriailze.serialize_to_vec();
30        let serialized_borsh_native = borsh::to_vec(&to_seriailze).unwrap();
31        assert_eq!(serialized_this_lib, serialized_borsh_native);
32    }
33}