Skip to content

encode::write::Write::Error should probably be 'static #42

@Abrahamh08

Description

@Abrahamh08

Consider this example of mixing serde Serialize Deserialize structs into minicbor Encode Decode structs:

use minicbor::{Decode, Encode};
use serde::{Deserialize, Serialize};

#[derive(Debug, Encode, Decode, Eq, PartialEq)]
pub struct Madness {
    #[n(0)] pub cfg1: Kind,
    #[n(1)] pub cfg2: Kind,
    #[n(2)] pub cfg3: Kind,
    #[n(3)] pub cfg4: Kind,
}

#[derive(Debug, Encode, Decode, Eq, PartialEq)]
pub enum Kind {
    #[n(0)] Foo(#[n(0)] FooConfig),
    #[n(1)] Bar(#[n(0)] BarConfig),
}

#[derive(Debug, Encode, Decode, Eq, PartialEq)]
pub struct FooConfig {
    #[n(0)] pub size: u32,
    #[n(1)] pub name: [u8; 16],
}

#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct BarConfig {
    pub mode: u8,
}

impl<C> minicbor::Encode<C> for BarConfig {
    fn encode<W>(
        &self,
        e: &mut minicbor::Encoder<W>,
        _ctx: &mut C,
    ) -> Result<(), minicbor::encode::Error<W::Error>>
    where
        W: minicbor::encode::Write,
    {
        let mut ser = minicbor_serde::Serializer::new(e.writer_mut());
        self.serialize(&mut ser).map_err(|_| minicbor::encode::Error::message("serde encode error"))
    }
}

impl<'b, C> minicbor::Decode<'b, C> for BarConfig {
    fn decode(d: &mut minicbor::Decoder<'b>, _ctx: &mut C)
        -> Result<Self, minicbor::decode::Error>
    {
        let start = d.position();

        let rest = &d.input()[start..];
        let mut de = minicbor_serde::Deserializer::new(rest);
        let v: BarConfig = serde::Deserialize::deserialize(&mut de)
            .map_err(|_| minicbor::decode::Error::message("serde decode error"))?;

        let consumed = de.decoder().position();

        d.set_position(start + consumed);

        Ok(v)
    }
}

fn main() {
    let madness = Madness {
        cfg1: Kind::Foo(FooConfig {
            size: 42,
            name: *b"example_foo_name",
        }),
        cfg2: Kind::Bar(BarConfig {
            mode: 7,
        }),
        cfg3: Kind::Foo(FooConfig {
            size: 42,
            name: *b"example_foo_name",
        }),
        cfg4: Kind::Bar(BarConfig {
            mode: 7,
        }),
    };
    let mut buffer = [0u8; 256];
    minicbor::encode(&madness, &mut buffer[..]).unwrap();
    let decoded_madness: Madness = minicbor::decode(&buffer).unwrap();
    assert_eq!(madness, decoded_madness);
}

In order to make this work, other than my into_inner impl and core::error::Error bound (we can talk about in another issue), encode::write::Write::Error needs to have 'static lifetime bound.

I could make this work with a wrapper that would have to erase the part of the error that the bound required, but it may be better to just lifetime bind Error to 'static.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions