solana_zk_sdk/
pod.rs

1use bytemuck_derive::{Pod, Zeroable};
2
3#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Pod, Zeroable)]
4#[repr(transparent)]
5pub struct PodU16([u8; 2]);
6impl From<u16> for PodU16 {
7    fn from(n: u16) -> Self {
8        Self(n.to_le_bytes())
9    }
10}
11impl From<PodU16> for u16 {
12    fn from(pod: PodU16) -> Self {
13        Self::from_le_bytes(pod.0)
14    }
15}
16
17#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Pod, Zeroable)]
18#[repr(transparent)]
19pub struct PodU64([u8; 8]);
20impl From<u64> for PodU64 {
21    fn from(n: u64) -> Self {
22        Self(n.to_le_bytes())
23    }
24}
25impl From<PodU64> for u64 {
26    fn from(pod: PodU64) -> Self {
27        Self::from_le_bytes(pod.0)
28    }
29}
30
31macro_rules! impl_from_str {
32    (TYPE = $type:ident, BYTES_LEN = $bytes_len:expr, BASE64_LEN = $base64_len:expr) => {
33        impl std::str::FromStr for $type {
34            type Err = crate::errors::ParseError;
35
36            fn from_str(s: &str) -> Result<Self, Self::Err> {
37                if s.len() > $base64_len {
38                    return Err(Self::Err::WrongSize);
39                }
40                let mut bytes = [0u8; $bytes_len];
41                let decoded_len = BASE64_STANDARD
42                    .decode_slice(s, &mut bytes)
43                    .map_err(|_| Self::Err::Invalid)?;
44                if decoded_len != $bytes_len {
45                    Err(Self::Err::WrongSize)
46                } else {
47                    Ok($type(bytes))
48                }
49            }
50        }
51    };
52}
53pub(crate) use impl_from_str;
54
55macro_rules! impl_from_bytes {
56    (TYPE = $type:ident, BYTES_LEN = $bytes_len:expr) => {
57        impl std::convert::From<[u8; $bytes_len]> for $type {
58            fn from(bytes: [u8; $bytes_len]) -> Self {
59                Self(bytes)
60            }
61        }
62    };
63}
64pub(crate) use impl_from_bytes;