ckb_fixed_hash_core/
impls.rs

1use crate::{error::FromSliceError, H160, H256, H512, H520};
2
3macro_rules! impl_methods {
4    ($name:ident, $bytes_size:expr) => {
5        impl $name {
6            /// Converts `Self` to a byte slice.
7            #[inline]
8            pub fn as_bytes(&self) -> &[u8] {
9                &self.0[..]
10            }
11            /// To convert the byte slice back into `Self`.
12            #[inline]
13            pub fn from_slice(input: &[u8]) -> Result<Self, FromSliceError> {
14                if input.len() != $bytes_size {
15                    Err(FromSliceError::InvalidLength(input.len()))
16                } else {
17                    let mut ret = Self::default();
18                    ret.0[..].copy_from_slice(input);
19                    Ok(ret)
20                }
21            }
22        }
23    };
24}
25
26impl_methods!(H160, 20);
27impl_methods!(H256, 32);
28impl_methods!(H512, 64);
29impl_methods!(H520, 65);