sharksforarms / deku

Declarative binary reading and writing: bit-level, symmetric, serialization/deserialization
Apache License 2.0
1.05k stars 54 forks source link

Missing support for core::net::Ipv4Addr/etc. #376

Open yanshay opened 7 months ago

yanshay commented 7 months ago

Deku has support for no_std. It also supports std::net::Ipv4Addr (and other IP addresses structures). However, in no_std the std IP addresses structures are replaced with core::net:: IP addresses. I think it's worth supporting those structure as well for completeness ease of developing structures for both std/no_std targets.

Is there a way to implement this myself in my library? When I just try to copy the code from Deku and apply it on core::net::Ipv4Addr, rust doesn't allow it because DekuWrite is a foreign trait. Any way to overcome it without replacing the structure with my own Ipv4Addr structure (and then using standard serialization) which then cause complications elsewhere?

This is what I tried:

impl<Ctx> DekuWrite<Ctx> for core::net::Ipv4Addr
where
    u32: DekuWrite<Ctx>,
{
    fn write(&self, output: &mut BitVec<u8, Msb0>, ctx: Ctx) -> Result<(), DekuError> {
        let ip: u32 = (*self).into();
        ip.write(output, ctx)
    }
}

This is the error rust raised:

error[E0210]: type parameter `Ctx` must be used as the type parameter for some local type (e.g., `MyStruct<Ctx>`)
  --> src/lib.rs:24:6
   |
24 | impl<Ctx> DekuWrite<Ctx> for core::net::Ipv4Addr
   |      ^^^ type parameter `Ctx` must be used as the type parameter for some local type
   |
   = note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
   = note: only traits defined in the current crate can be implemented for a type parameter
wcampbell0x2a commented 7 months ago

At first glance, this is related to https://github.com/sharksforarms/deku/pull/226 and https://github.com/sharksforarms/deku/pull/226.

No reason we can't support no_std IpAddr