sharksforarms / deku

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

Support `bool` type in `id` attribute in `Enum` #471

Closed wcampbell0x2a closed 2 months ago

wcampbell0x2a commented 2 months ago

This should work:

use deku::prelude::*;

#[derive(DekuRead, DekuWrite, Debug)]
pub struct A {
    bit: bool,
    #[deku(ctx = "*bit")]
    var: Var,
}

#[derive(DekuRead, DekuWrite, Debug)]
#[deku(id = "bit", ctx = "bit: bool")]
pub enum Var {
    #[deku(id = false)]
    False(u8),
    #[deku(id = true)]
    True(u32),
}

It doens't:

error: Unexpected type `bool`
  --> src/lib.rs:13:17
   |
13 |     #[deku(id = false)]
   |                 ^^^^^

error: Unexpected type `bool`
  --> src/lib.rs:15:17
   |
15 |     #[deku(id = true)]
   |                 ^^^^

Instead, you need to make the id a TokenStream:

use deku::prelude::*;

#[derive(DekuRead, DekuWrite, Debug)]
pub struct A {
    bit: bool,
    #[deku(ctx = "*bit")]
    var: Var,
}

#[derive(DekuRead, DekuWrite, Debug)]
#[deku(id = "bit", ctx = "bit: bool")]
pub enum Var {
    #[deku(id = "false")]
    False(u8),
    #[deku(id = "true")]
    True(u32),
}