Update tests; Update docs;

This commit is contained in:
Gabriel Bjørnager Jensen 2025-04-04 13:11:54 +02:00
parent 677c1ebcbc
commit 40ff018433
10 changed files with 191 additions and 38 deletions

View file

@ -3,6 +3,11 @@
This is the changelog of [Oct](https://crates.io/crates/oct/).
See `README.md` for more information.
## 0.21.2
* Update tests
* Update docs
## 0.21.1
* Fix `GenericDecodeError` and `GenericEncodeError` not implementing `Eq`

View file

@ -9,7 +9,7 @@ members = ["oct", "oct-benchmarks", "oct-macros"]
resolver = "2"
[workspace.package]
version = "0.21.1"
version = "0.21.2"
authors = ["Gabriel Bjørnager Jensen"]
readme = "README.md"
repository = "https://mandelbrot.dk/bjoernager/oct/"

View file

@ -25,7 +25,7 @@ pub fn decode_enum(data: DataEnum, repr: Repr, error: Type) -> TokenStream {
let commands = iter::repeat_n(
quote! {
::oct::decode::Decode::decode(stream)
::oct::decode::Decode::decode(input)
.map_err(::core::convert::Into::<#error>::into)
.map_err(::oct::error::EnumDecodeError::BadField)?
},
@ -52,10 +52,10 @@ pub fn decode_enum(data: DataEnum, repr: Repr, error: Type) -> TokenStream {
type Error = ::oct::error::EnumDecodeError<#repr, <#repr as ::oct::decode::Decode>::Error, #error>;
#[inline]
fn decode(stream: &mut ::oct::decode::Input) -> ::core::result::Result<Self, Self::Error> {
fn decode(input: &mut ::oct::decode::Input) -> ::core::result::Result<Self, Self::Error> {
use ::core::result::Result;
let discriminant = <#repr as ::oct::decode::Decode>::decode(stream)
let discriminant = <#repr as ::oct::decode::Decode>::decode(input)
.map_err(::core::convert::Into::<::core::convert::Infallible>::into)
.map_err(::oct::error::EnumDecodeError::InvalidDiscriminant)?;

View file

@ -61,17 +61,17 @@ pub fn encode_enum(data: DataEnum, repr: Repr, error: Type) -> TokenStream {
quote! {
type Error = ::oct::error::EnumEncodeError<<#repr as ::oct::encode::Encode>::Error, #error>;
#[allow(unreachable_patterns)]
#[expect(unreachable_patterns)]
#[inline]
fn encode(&self, stream: &mut ::oct::encode::Output) -> ::core::result::Result<(), Self::Error> {
fn encode(&self, output: &mut ::oct::encode::Output) -> ::core::result::Result<(), Self::Error> {
match *self {
#(
#patterns => {
<#repr as ::oct::encode::Encode>::encode(&#discriminants, stream)
<#repr as ::oct::encode::Encode>::encode(&#discriminants, output)
.map_err(::oct::error::EnumEncodeError::BadDiscriminant)?;
#(
::oct::encode::Encode::encode(#captures, stream)
::oct::encode::Encode::encode(#captures, output)
.map_err(::core::convert::Into::<#error>::into)
.map_err(::oct::error::EnumEncodeError::BadField)?;
)*

View file

@ -27,7 +27,7 @@ pub fn encode_struct(data: DataStruct, error: Type) -> TokenStream {
);
quote! {
::oct::encode::Encode::encode(&self.#name, stream)
::oct::encode::Encode::encode(&self.#name, output)
.map_err(::core::convert::Into::<#error>::into)?;
}
});
@ -36,7 +36,7 @@ pub fn encode_struct(data: DataStruct, error: Type) -> TokenStream {
type Error = #error;
#[inline]
fn encode(&self, stream: &mut ::oct::encode::Output) -> ::core::result::Result<(), Self::Error> {
fn encode(&self, output: &mut ::oct::encode::Output) -> ::core::result::Result<(), Self::Error> {
#(#commands)*
::core::result::Result::Ok(())

View file

@ -42,13 +42,13 @@ pub fn derive_decode(input: TokenStream) -> TokenStream {
for attr in &input.attrs {
if attr.meta.path().is_ident("oct") {
attr.parse_nested_meta(|meta| {
let _ = attr.parse_nested_meta(|meta| {
if meta.path.is_ident("decode_error") {
error = Parse::parse(meta.value()?)?;
}
Ok(())
}).unwrap();
});
}
}
@ -88,13 +88,13 @@ pub fn derive_encode(input: TokenStream) -> TokenStream {
for attr in &input.attrs {
if attr.meta.path().is_ident("oct") {
attr.parse_nested_meta(|meta| {
let _ = attr.parse_nested_meta(|meta| {
if meta.path.is_ident("encode_error") {
error = Parse::parse(meta.value()?)?;
}
Ok(())
}).unwrap();
});
}
}

View file

@ -95,6 +95,7 @@ pub trait Decode: Sized {
/// # Panics
///
/// If `input` unexpectedly terminates before a full encoding was read, then this method should panic.
/// #[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error>;
}
@ -104,6 +105,7 @@ impl<T: Decode> Decode for (T, ) {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let this = (Decode::decode(input)?, );
Ok(this)
@ -114,6 +116,7 @@ impl<T: Decode, const N: usize> Decode for [T; N] {
type Error = CollectionDecodeError<Infallible, ItemDecodeError<usize, T::Error>>;
#[inline]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
// Initialise the array incrementally.
@ -150,6 +153,7 @@ impl<T: Decode> Decode for Arc<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let value = Decode::decode(input)?;
@ -164,6 +168,7 @@ impl<T: Decode + Ord> Decode for BinaryHeap<T> {
type Error = <alloc::vec::Vec<T> as Decode>::Error;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let v = alloc::vec::Vec::decode(input)?;
@ -179,6 +184,7 @@ impl Decode for bool {
///
/// Whilst <code>[Encode](crate::encode::Encode)::[encode](crate::encode::Encode::encode)</code> will only yield the values `0` and `1`, this method clamps all values above `1`.
#[inline]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let Ok(value) = u8::decode(input);
@ -191,6 +197,7 @@ impl<T: Decode> Decode for Bound<T> {
type Error = EnumDecodeError<u8, <u8 as Decode>::Error, T::Error>;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let Ok(discriminant) = u8::decode(input);
@ -224,6 +231,7 @@ impl<T: Decode> Decode for Box<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let value = Decode::decode(input)?;
@ -242,6 +250,7 @@ impl Decode for CString {
/// This implementation will always allocate one more byte than specified by the slice for the null terminator.
/// Note that any null value already in the data will truncate the final string.
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let Ok(len) = usize::decode(input);
@ -260,6 +269,7 @@ impl<T: Decode> Decode for Cell<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let value = Decode::decode(input)?;
@ -272,6 +282,7 @@ impl Decode for c_void {
type Error = Infallible;
#[inline(always)]
#[track_caller]
fn decode(_input: &mut Input) -> Result<Self, Self::Error> {
panic!("cannot deserialise `c_void` as it cannot be constructed to begin with")
}
@ -281,6 +292,7 @@ impl Decode for char {
type Error = CharDecodeError;
#[inline]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let Ok(code_point) = u32::decode(input);
@ -308,6 +320,7 @@ where
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let value = Decode::decode(input)?;
@ -320,6 +333,7 @@ impl Decode for Duration {
type Error = Infallible;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let Ok(secs) = Decode::decode(input);
let Ok(nanos) = Decode::decode(input);
@ -335,6 +349,7 @@ impl Decode for f128 {
type Error = Infallible;
#[inline]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let mut data = [Default::default(); <Self as SizedEncode>::MAX_ENCODED_SIZE];
input.read_into(&mut data).unwrap();
@ -350,6 +365,7 @@ impl Decode for f16 {
type Error = Infallible;
#[inline]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let mut data = [Default::default(); <Self as SizedEncode>::MAX_ENCODED_SIZE];
input.read_into(&mut data).unwrap();
@ -370,6 +386,7 @@ where
type Error = CollectionDecodeError<Infallible, ItemDecodeError<usize, E>>;
#[inline]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let Ok(len) = Decode::decode(input);
@ -400,6 +417,7 @@ where
type Error = CollectionDecodeError<Infallible, ItemDecodeError<usize, K::Error>>;
#[inline]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let Ok(len) = Decode::decode(input);
@ -420,6 +438,7 @@ impl Decode for Infallible {
type Error = Self;
#[inline(always)]
#[track_caller]
fn decode(_input: &mut Input) -> Result<Self, Self::Error> {
panic!("cannot deserialise `Infallible` as it cannot be constructed to begin with")
}
@ -429,6 +448,7 @@ impl Decode for IpAddr {
type Error = EnumDecodeError<u8, <u8 as Decode>::Error, Infallible>;
#[inline]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let Ok(discriminant) = u8::decode(input);
@ -447,6 +467,7 @@ impl Decode for Ipv4Addr {
type Error = Infallible;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let Ok(value) = Decode::decode(input);
@ -459,6 +480,7 @@ impl Decode for Ipv6Addr {
type Error = Infallible;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let Ok(value) = Decode::decode(input);
@ -471,6 +493,7 @@ impl Decode for isize {
type Error = Infallible;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let Ok(value) = i16::decode(input);
@ -484,6 +507,7 @@ impl<T: Decode> Decode for LinkedList<T> {
type Error = CollectionDecodeError<Infallible, ItemDecodeError<usize, T::Error>>;
#[inline]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let Ok(len) = usize::decode(input);
@ -506,6 +530,7 @@ impl<T: Decode> Decode for Mutex<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let value = Decode::decode(input)?;
@ -519,6 +544,7 @@ impl<T: Decode> Decode for Option<T> {
#[expect(clippy::if_then_some_else_none)] // ???
#[inline]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let Ok(sign) = bool::decode(input);
@ -538,6 +564,7 @@ impl Decode for OsString {
type Error = <alloc::string::String as Decode>::Error;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let s: alloc::string::String = Decode::decode(input)?;
@ -550,6 +577,7 @@ impl<T> Decode for PhantomData<T> {
type Error = Infallible;
#[inline(always)]
#[track_caller]
fn decode(_input: &mut Input) -> Result<Self, Self::Error> {
Ok(Self)
}
@ -559,6 +587,7 @@ impl Decode for PhantomPinned {
type Error = Infallible;
#[inline(always)]
#[track_caller]
fn decode(_input: &mut Input) -> Result<Self, Self::Error> {
Ok(Self)
}
@ -568,6 +597,7 @@ impl<T: Decode> Decode for Range<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let start = Decode::decode(input)?;
let end = Decode::decode(input)?;
@ -580,6 +610,7 @@ impl<T: Decode> Decode for RangeFrom<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let start = Decode::decode(input)?;
@ -591,6 +622,7 @@ impl Decode for RangeFull {
type Error = Infallible;
#[inline(always)]
#[track_caller]
fn decode(_input: &mut Input) -> Result<Self, Self::Error> {
Ok(..)
}
@ -600,6 +632,7 @@ impl<T: Decode> Decode for RangeInclusive<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let start = Decode::decode(input)?;
let end = Decode::decode(input)?;
@ -612,6 +645,7 @@ impl<T: Decode> Decode for RangeTo<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let end = Decode::decode(input)?;
@ -623,6 +657,7 @@ impl<T: Decode> Decode for RangeToInclusive<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let end = Decode::decode(input)?;
@ -636,6 +671,7 @@ impl<T: Decode> Decode for Rc<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
Ok(Self::new(Decode::decode(input)?))
}
@ -645,6 +681,7 @@ impl<T: Decode> Decode for RefCell<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let value = Decode::decode(input)?;
@ -661,6 +698,7 @@ where
type Error = EnumDecodeError<bool, <bool as Decode>::Error, Err>;
#[inline]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let Ok(sign) = bool::decode(input);
@ -687,6 +725,7 @@ impl<T: Decode> Decode for RwLock<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let value = Decode::decode(input)?;
@ -699,6 +738,7 @@ impl<T: Decode> Decode for Saturating<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let value = Decode::decode(input)?;
@ -711,6 +751,7 @@ impl Decode for SocketAddr {
type Error = EnumDecodeError<u8, <u8 as Decode>::Error, Infallible>;
#[inline]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let Ok(discriminant) = u8::decode(input);
@ -730,6 +771,7 @@ impl Decode for SocketAddrV4 {
type Error = Infallible;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let ip = Decode::decode(input)?;
let port = Decode::decode(input)?;
@ -743,6 +785,7 @@ impl Decode for SocketAddrV6 {
type Error = Infallible;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let ip = Decode::decode(input)?;
let port = Decode::decode(input)?;
@ -760,6 +803,7 @@ impl Decode for alloc::string::String {
type Error = CollectionDecodeError<Infallible, Utf8Error>;
#[inline]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let Ok(len) = Decode::decode(input);
@ -789,6 +833,7 @@ impl Decode for SystemTime {
type Error = SystemTimeDecodeError;
#[inline]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let Ok(time) = i64::decode(input);
@ -810,6 +855,7 @@ impl Decode for () {
type Error = Infallible;
#[inline(always)]
#[track_caller]
fn decode(_input: &mut Input) -> Result<Self, Self::Error> {
Ok(())
}
@ -819,6 +865,7 @@ impl<T: Decode> Decode for UnsafeCell<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let value = Decode::decode(input)?;
@ -831,6 +878,7 @@ impl Decode for usize {
type Error = Infallible;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let Ok(value) = u16::decode(input);
Ok(value as Self)
@ -843,6 +891,7 @@ impl<T: Decode> Decode for alloc::vec::Vec<T> {
type Error = CollectionDecodeError<Infallible, ItemDecodeError<usize, T::Error>>;
#[inline]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let Ok(len) = Decode::decode(input);
@ -875,6 +924,7 @@ impl<T: Decode> Decode for Wrapping<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let value = Decode::decode(input)?;
@ -889,6 +939,7 @@ macro_rules! impl_numeric {
type Error = ::core::convert::Infallible;
#[inline]
#[track_caller]
fn decode(input: &mut ::oct::decode::Input) -> ::core::result::Result<Self, Self::Error> {
let mut data = [::core::default::Default::default(); <Self as ::oct::encode::SizedEncode>::MAX_ENCODED_SIZE];
input.read_into(&mut data).unwrap();
@ -914,6 +965,7 @@ macro_rules! impl_tuple {
type Error = E;
#[inline(always)]
#[track_caller]
fn decode(input: &mut ::oct::decode::Input) -> ::core::result::Result<Self, Self::Error> {
let this = (
<$ty as ::oct::decode::Decode>::decode(input)?,
@ -936,6 +988,7 @@ macro_rules! impl_non_zero {
type Error = ::oct::error::NonZeroDecodeError;
#[inline]
#[track_caller]
fn decode(input: &mut ::oct::decode::Input) -> ::core::result::Result<Self, Self::Error> {
use ::core::result::Result::{Err, Ok};
@ -969,6 +1022,7 @@ macro_rules! impl_atomic {
type Error = <$ty as ::oct::decode::Decode>::Error;
#[inline(always)]
#[track_caller]
fn decode(input: &mut ::oct::decode::Input) -> ::core::result::Result<Self, Self::Error> {
let value = ::oct::decode::Decode::decode(input)?;

View file

@ -179,42 +179,71 @@ fn test_decode_derive() {
#[test]
fn test_decode_derive_custom_error() {
#[derive(Debug, Eq, PartialEq)]
struct Error;
struct FooError;
#[derive(Debug, Eq, PartialEq)]
struct Foo(pub u8);
#[derive(Debug, Encode, Eq, PartialEq)]
struct Foo;
impl Decode for Foo {
type Error = Error;
type Error = FooError;
fn decode(input: &mut Input) -> Result<Self, Self::Error> {
let Ok(value) = u8::decode(input);
if value % 0x2 != 0x0 {
return Err(Error);
}
let this = Self(value);
Ok(this)
fn decode(_input: &mut Input) -> Result<Self, Self::Error> {
Err(FooError)
}
}
#[derive(Debug, Decode, Eq, PartialEq)]
#[oct(decode_error = Error)]
struct Bar(Foo);
#[derive(Debug, Eq, PartialEq)]
struct BarError;
#[derive(Debug, Encode, Eq, PartialEq)]
struct Bar;
impl Decode for Bar {
type Error = BarError;
fn decode(_input: &mut Input) -> Result<Self, Self::Error> {
Err(BarError)
}
}
#[derive(Debug, Eq, PartialEq)]
enum FooBarError {
Foo(FooError),
Bar(BarError),
}
impl From<FooError> for FooBarError {
fn from(value: FooError) -> Self {
Self::Foo(value)
}
}
impl From<BarError> for FooBarError {
fn from(value: BarError) -> Self {
Self::Bar(value)
}
}
#[repr(u8)]
#[derive(Debug, Decode, Encode, Eq, PartialEq)]
#[oct(decode_error = FooBarError)]
enum FooBar {
Foo(Foo),
Bar(Bar),
}
test! {
Foo {
[0x00] => Ok(Foo(0x00)),
[0x02] => Ok(Foo(0x02)),
[0x80] => Ok(Foo(0x80)),
[0x7F] => Err(Error),
[] => Err(FooError),
}
Bar {
[0x00] => Ok(Bar(Foo(0x00))),
[0xFE] => Ok(Bar(Foo(0xFE))),
[0xFF] => Err(Error),
[] => Err(BarError),
}
FooBar {
[0x00] => Err(EnumDecodeError::BadField(FooBarError::Foo(FooError))),
[0x01] => Err(EnumDecodeError::BadField(FooBarError::Bar(BarError))),
}
}
}

View file

@ -97,6 +97,7 @@ use {
///
/// type Error = Infallible;
///
/// #[track_caller] // (1)
/// fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
/// // Encode fields using chaining.
///
@ -107,6 +108,8 @@ use {
/// }
/// }
/// ```
///
/// <sub>(1): In cases where `output` is empty, `encode` is expected to panic. Adding `track_caller` makes this "easier" to debug without analysing the stack trace.</sub>
#[doc(alias("Serialise", "Serialize"))]
pub trait Encode {
/// The type returned in case of error.
@ -123,6 +126,7 @@ pub trait Encode {
/// # Panics
///
/// If `output` cannot contain the entirety of the resulting encoding, then this method should panic.
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error>;
}
@ -130,6 +134,7 @@ impl<T: Encode + ?Sized> Encode for &T {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
T::encode(self, output)
}
@ -139,6 +144,7 @@ impl<T: Encode + ?Sized> Encode for &mut T {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
T::encode(self, output)
}
@ -150,6 +156,7 @@ impl<T: Encode> Encode for (T, ) {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self.0.encode(output)
}
@ -162,6 +169,7 @@ impl<T: Encode, const N: usize> Encode for [T; N] {
///
/// The length `N ` is hard-coded into the type and is therefore not encoded.
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
for (i, v) in self.iter().enumerate() {
v
@ -178,6 +186,7 @@ impl<T: Encode> Encode for [T] {
/// Encodes each element sequentially with an extra length specifier (of type [`usize`]) prepended first.
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self
.len()
@ -200,6 +209,7 @@ impl<T: Encode + ?Sized> Encode for Arc<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
T::encode(self, output)
}
@ -211,6 +221,7 @@ impl<T: Encode> Encode for BinaryHeap<T> {
type Error = <[T] as Encode>::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self.as_slice().encode(output)
}
@ -221,6 +232,7 @@ impl Encode for bool {
/// Encodes the raw representationf of the boolean.
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
u8::from(*self).encode(output)
}
@ -230,6 +242,7 @@ impl<T: Encode> Encode for Bound<T> {
type Error = EnumEncodeError<u8, T::Error>;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
match *self {
Self::Included(ref bound) => {
@ -257,6 +270,7 @@ impl<T: Encode + ?Sized> Encode for Box<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
T::encode(self, output)
}
@ -267,6 +281,7 @@ impl Encode for CStr {
/// Encodes the string identically to [a byte slice](slice) containing the string's byte values **excluding** the null terminator.
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self.to_bytes().encode(output)
}
@ -279,6 +294,7 @@ impl Encode for CString {
/// See the the implementation of [`CStr`].
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self.as_c_str().encode(output)
}
@ -288,6 +304,7 @@ impl Encode for c_void {
type Error = Infallible;
#[inline(always)]
#[track_caller]
fn encode(&self, _output: &mut Output) -> Result<(), Self::Error> {
// NOTE: Contrary to `Infallible` and/or `!`,
// `c_void` *can* actually be constructed (although
@ -300,6 +317,7 @@ impl<T: Copy + Encode> Encode for Cell<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self.get().encode(output)
}
@ -309,6 +327,7 @@ impl Encode for char {
type Error = <u32 as Encode>::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
u32::from(*self).encode(output)
}
@ -320,6 +339,7 @@ impl<T: Encode + ToOwned + ?Sized> Encode for Cow<'_, T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
T::encode(self, output)
}
@ -330,6 +350,7 @@ impl Encode for Duration {
/// Encodes the duration's seconds and nanoseconds counters sequentially.
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self.as_secs().encode(output)?;
self.subsec_nanos().encode(output)?;
@ -344,6 +365,7 @@ impl Encode for f128 {
type Error = Infallible;
#[inline]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
output.write(&self.to_le_bytes()).unwrap();
@ -357,6 +379,7 @@ impl Encode for f16 {
type Error = Infallible;
#[inline]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
output.write(&self.to_le_bytes()).unwrap();
@ -375,6 +398,7 @@ where
type Error = E;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
for (key, value) in self {
key.encode(output)?;
@ -398,6 +422,7 @@ where
type Error = K::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
for key in self {
key.encode(output)?;
@ -413,6 +438,7 @@ impl Encode for Infallible {
type Error = Self;
#[inline(always)]
#[track_caller]
fn encode(&self, _output: &mut Output) -> Result<(), Self::Error> {
unreachable!()
}
@ -425,6 +451,7 @@ impl Encode for IpAddr {
///
/// See also the implementations of [`Ipv4Addr`] and [`Ipv6Addr`].
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
// The discriminant here is the IP version.
@ -449,6 +476,7 @@ impl Encode for Ipv4Addr {
/// Encodes the address's bits in big-endian.
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
let value = self.to_bits();
value.encode(output)
@ -460,6 +488,7 @@ impl Encode for Ipv6Addr {
/// Encodes the address's bits in big-endian.
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
let value = self.to_bits();
value.encode(output)
@ -471,6 +500,7 @@ impl Encode for isize {
/// Casts `self` to [`i16`] and encodes the result.
#[inline]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
let value = i16::try_from(*self)
.map_err(|_| IsizeEncodeError(*self))?;
@ -484,6 +514,7 @@ impl<T: Encode> Encode for LazyCell<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
T::encode(self, output)
}
@ -495,6 +526,7 @@ impl<T: Encode> Encode for LazyLock<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
T::encode(self, output)
}
@ -506,6 +538,7 @@ impl<T: Encode> Encode for LinkedList<T> {
type Error = CollectionEncodeError<UsizeEncodeError, ItemEncodeError<usize, T::Error>>;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self
.len()
@ -528,6 +561,7 @@ impl<T: Encode + ?Sized> Encode for Mutex<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self
.lock()
@ -543,6 +577,7 @@ impl<T: Encode> Encode for Option<T> {
/// This is `false` for `None` instances and `true` for `Some` instances.
///
/// If `Some`, then the contained value is encoded after this sign..
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
match *self {
None => {
@ -578,6 +613,7 @@ impl Encode for OsStr {
///
/// This implementation will yield an error if the string `self` contains any non-UTF-8 octets.
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
let data = self.as_encoded_bytes();
@ -613,6 +649,7 @@ impl Encode for OsString {
///
/// See [`OsStr`]'s implementation of `Encode` for more information.
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self.as_os_str().encode(output)
}
@ -622,6 +659,7 @@ impl<T: ?Sized> Encode for PhantomData<T> {
type Error = Infallible;
#[inline(always)]
#[track_caller]
fn encode(&self, _output: &mut Output) -> Result<(), Self::Error> {
Ok(())
}
@ -631,6 +669,7 @@ impl Encode for PhantomPinned {
type Error = Infallible;
#[inline(always)]
#[track_caller]
fn encode(&self, _output: &mut Output) -> Result<(), Self::Error> {
Ok(())
}
@ -640,6 +679,7 @@ impl<T: Encode> Encode for Range<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self.start.encode(output)?;
self.end.encode(output)?;
@ -652,6 +692,7 @@ impl<T: Encode> Encode for RangeFrom<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self.start.encode(output)
}
@ -661,6 +702,7 @@ impl Encode for RangeFull {
type Error = Infallible;
#[inline(always)]
#[track_caller]
fn encode(&self, _output: &mut Output) -> Result<(), Self::Error> {
Ok(())
}
@ -670,6 +712,7 @@ impl<T: Encode> Encode for RangeInclusive<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self.start().encode(output)?;
self.end().encode(output)?;
@ -682,6 +725,7 @@ impl<T: Encode> Encode for RangeTo<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self.end.encode(output)
}
@ -691,6 +735,7 @@ impl<T: Encode> Encode for RangeToInclusive<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self.end.encode(output)
}
@ -702,6 +747,7 @@ impl<T: Encode + ?Sized> Encode for Rc<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
T::encode(self, output)
}
@ -711,6 +757,7 @@ impl<T: Encode + ?Sized> Encode for RefCell<T> {
type Error = RefCellEncodeError<T::Error>;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
let value = self
.try_borrow()
@ -735,6 +782,7 @@ where
///
/// If `Ok`, then the contained value is encoded after this sign.
#[inline]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
// The sign here is `false` for `Ok` objects and
// `true` for `Err` objects.
@ -763,6 +811,7 @@ impl<T: Encode + ?Sized> Encode for RwLock<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self
.read()
@ -775,6 +824,7 @@ impl<T: Encode> Encode for Saturating<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self.0.encode(output)
}
@ -786,6 +836,7 @@ impl Encode for SocketAddr {
/// This implementation encoded as discriminant denoting the IP version of the address (i.e. `4` for IPv4 and `6` for IPv6).
/// This is then followed by the respective address' own encoding (either [`SocketAddrV4`] or [`SocketAddrV6`]).
#[inline]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
// The discriminant here is the IP version.
@ -810,6 +861,7 @@ impl Encode for SocketAddrV4 {
/// Encodes the address's bits followed by the port number, both of which in big-endian.
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self.ip().encode(output)?;
self.port().encode(output)?;
@ -823,6 +875,7 @@ impl Encode for SocketAddrV6 {
/// Encodes the address's bits followed by the port number, flow information, and scope identifier -- all of which in big-endian.
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self.ip().encode(output)?;
self.port().encode(output)?;
@ -838,6 +891,7 @@ impl Encode for str {
/// Encodes the string identically to [a byte slice](slice) containing the string's byte values.
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self.as_bytes().encode(output)
}
@ -850,6 +904,7 @@ impl Encode for alloc::string::String {
/// See [`prim@str`].
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self.as_str().encode(output)
}
@ -872,6 +927,7 @@ impl Encode for SystemTime {
/// | `1945-05-04T18:30:00+02:00` | -778231800 |
#[expect(clippy::cast_possible_wrap)]
#[inline]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
let time = if *self >= UNIX_EPOCH {
let duration = self
@ -896,6 +952,7 @@ impl Encode for () {
type Error = Infallible;
#[inline(always)]
#[track_caller]
fn encode(&self, _output: &mut Output) -> Result<(), Self::Error> {
Ok(())
}
@ -905,6 +962,7 @@ impl<T: Copy + Encode> Encode for UnsafeCell<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
// SAFETY: The pointer returned by `Self::get` is
// valid for reading for the lifetime of `self`.
@ -919,6 +977,7 @@ impl Encode for usize {
/// Casts `self` to [`u16`] and encodes the result.
#[inline]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
let value = u16::try_from(*self)
.map_err(|_| UsizeEncodeError(*self))?;
@ -934,6 +993,7 @@ impl<T: Encode> Encode for alloc::vec::Vec<T> {
type Error = <[T] as Encode>::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self.as_slice().encode(output)
}
@ -943,6 +1003,7 @@ impl<T: Encode> Encode for Wrapping<T> {
type Error = T::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut Output) -> Result<(), Self::Error> {
self.0.encode(output)
}
@ -954,6 +1015,7 @@ macro_rules! impl_numeric {
type Error = ::core::convert::Infallible;
#[inline]
#[track_caller]
fn encode(&self, output: &mut ::oct::encode::Output) -> ::core::result::Result<(), Self::Error> {
output.write(&self.to_le_bytes()).unwrap();
@ -977,6 +1039,7 @@ macro_rules! impl_tuple {
type Error = E;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut ::oct::encode::Output) -> ::core::result::Result<(), Self::Error> {
let (ref $capture, $(ref $extra_captures, )*) = *self;
@ -999,6 +1062,7 @@ macro_rules! impl_non_zero {
type Error = <$ty as ::oct::encode::Encode>::Error;
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut ::oct::encode::Output) -> ::core::result::Result<(), Self::Error> {
self.get().encode(output)
}
@ -1021,6 +1085,7 @@ macro_rules! impl_atomic {
///
/// The atomic object itself is read with the [`Relaxed`](core::sync::atomic::Ordering) ordering scheme.
#[inline(always)]
#[track_caller]
fn encode(&self, output: &mut ::oct::encode::Output) -> ::core::result::Result<(), Self::Error> {
use ::core::sync::atomic::Ordering;

View file

@ -115,8 +115,8 @@ fn test_encode_derive() {
#[repr(transparent)]
struct Foo(char);
#[derive(Encode, SizedEncode)]
#[repr(u8)]
#[derive(Encode, SizedEncode)]
enum Bar {
Unit = 0x45,