use bytemuck_derive::{Pod, Zeroable};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Pod, Zeroable)]
#[repr(transparent)]
pub struct PodU16([u8; 2]);
impl From<u16> for PodU16 {
fn from(n: u16) -> Self {
Self(n.to_le_bytes())
}
}
impl From<PodU16> for u16 {
fn from(pod: PodU16) -> Self {
Self::from_le_bytes(pod.0)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Pod, Zeroable)]
#[repr(transparent)]
pub struct PodU64([u8; 8]);
impl From<u64> for PodU64 {
fn from(n: u64) -> Self {
Self(n.to_le_bytes())
}
}
impl From<PodU64> for u64 {
fn from(pod: PodU64) -> Self {
Self::from_le_bytes(pod.0)
}
}
macro_rules! impl_from_str {
(TYPE = $type:ident, BYTES_LEN = $bytes_len:expr, BASE64_LEN = $base64_len:expr) => {
impl std::str::FromStr for $type {
type Err = crate::errors::ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() > $base64_len {
return Err(Self::Err::WrongSize);
}
let mut bytes = [0u8; $bytes_len];
let decoded_len = BASE64_STANDARD
.decode_slice(s, &mut bytes)
.map_err(|_| Self::Err::Invalid)?;
if decoded_len != $bytes_len {
Err(Self::Err::WrongSize)
} else {
Ok($type(bytes))
}
}
}
};
}
pub(crate) use impl_from_str;
macro_rules! impl_from_bytes {
(TYPE = $type:ident, BYTES_LEN = $bytes_len:expr) => {
impl std::convert::From<[u8; $bytes_len]> for $type {
fn from(bytes: [u8; $bytes_len]) -> Self {
Self(bytes)
}
}
};
}
pub(crate) use impl_from_bytes;