Skip to content

Commit

Permalink
feat: implement Decode for non-zero types
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaka91 committed Oct 14, 2023
1 parent 3d64436 commit 8f7e85e
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions crates/stef/src/buf/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub enum Error {
NonUtf8(std::string::FromUtf8Error),
MissingField { id: u32, name: Option<&'static str> },
UnknownVariant(u32),
Zero,
}

impl From<varint::DecodeIntError> for Error {
Expand Down Expand Up @@ -273,9 +274,24 @@ where
}
}

// -----------------------------
// TODO: NonZero
// -----------------------------
macro_rules! non_zero {
($ty:ty) => {
paste::paste! {
impl Decode for std::num::[<NonZero $ty:upper>] {
#[inline(always)]
fn decode(r: &mut impl Buf) -> Result<Self> {
Self::new([<decode_ $ty>](r)?).ok_or_else(|| Error::Zero)
}
}
}
};
($($ty:ty),+ $(,)?) => {
$(non_zero!($ty);)+
};
}

non_zero!(u8, u16, u32, u64, u128);
non_zero!(i8, i16, i32, i64, i128);

impl<'a, T> Decode for std::borrow::Cow<'a, T>
where
Expand Down

0 comments on commit 8f7e85e

Please sign in to comment.