musli_core

Attribute Macro decoder

Source
#[decoder]
Expand description

This is an attribute macro that must be used when implementing a Decoder.

It is required to use because a Decoder implementation might introduce new associated types in the future, and this is 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::decoder(crate = musli_core)] attribute.

ยงExamples

use std::fmt;

use musli_core::Context;
use musli_core::de::{Decoder, Decode};

struct MyDecoder<'a, C: ?Sized> {
    cx: &'a C,
}

#[musli_core::decoder(crate = musli_core)]
impl<'de, C: ?Sized + Context> Decoder<'de> for MyDecoder<'_, C> {
    type Cx = C;

    fn cx(&self) -> &C {
        self.cx
    }

    fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "32-bit unsigned integers")
    }

    fn decode_u32(self) -> Result<u32, Self::Error> {
        Ok(42)
    }
}