#[encoder]
Expand description
This is an attribute macro that must be used when implementing a
Encoder
.
It is required to use because a Encoder
implementation might introduce
new associated types in the future, and this not yet supported on a
language level in Rust. So this attribute macro polyfills any missing types
automatically.
Note that using derives directly from musli_core
requires you to use the
#[musli_core::encoder(crate = musli_core)]
attribute.
ยงExamples
use std::fmt;
use musli_core::Context;
use musli_core::en::{Encoder, Encode};
struct MyEncoder<'a, C: ?Sized> {
value: &'a mut Option<u32>,
cx: &'a C,
}
#[musli_core::encoder(crate = musli_core)]
impl<C: ?Sized + Context> Encoder for MyEncoder<'_, C> {
type Cx = C;
type Ok = ();
fn cx(&self) -> &C {
self.cx
}
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "32-bit unsigned integers")
}
fn encode<T>(self, value: T) -> Result<Self::Ok, C::Error>
where
T: Encode<Self::Mode>,
{
value.encode(self.cx, self)
}
fn encode_u32(self, value: u32) -> Result<(), Self::Error> {
*self.value = Some(value);
Ok(())
}
}