How to correctly use a 2bit enum in a struct? #645
Answered
by
amboar
DidingasLushis
asked this question in
Q&A
-
|
Hello! First time asking questions here so I hope that this is the right place. I am trying to do this: use deku::prelude::*;
use super::Id;
#[derive(DekuRead, DekuWrite, Debug, PartialEq)]
pub struct Header {
pub version: u8,
pub port_num: u16,
#[deku(bits = "2")]
pub direction: Direction,
pub id: Id,
}
#[derive(DekuRead, DekuWrite, Debug, PartialEq)]
#[deku(id_type = "u8")]
pub enum Direction {
#[deku(id = "0")]
Inbound,
#[deku(id = "1")]
Outbound,
#[deku(id = "2")]
Bidirectional,
#[deku(id = "2")]
Reserved,
}However, I get this error: $ cargo check
Checking my_crate v0.1.0 (/path/to/project)
error[E0308]: mismatched types
--> src/frame/header.rs:4:10
|
4 | #[derive(DekuRead, DekuWrite, Debug, PartialEq)]
| ^^^^^^^^
| |
| expected `()`, found `BitSize`
| arguments to this function are incorrect
|
note: associated function defined here
--> <cargo-registry>/deku-0.19.1/src/lib.rs:423:8
|
423| fn from_reader_with_ctx<R: no_std_io::Read + no_std_io::Seek>(
| ^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the derive macro `DekuRead` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0308]: mismatched types
--> src/frame/header.rs:4:20
|
4 | #[derive(DekuRead, DekuWrite, Debug, PartialEq)]
| ^^^^^^^^^
| |
| expected `()`, found `BitSize`
| arguments to this function are incorrect
|
note: method defined here
--> <cargo-registry>/deku-0.19.1/src/lib.rs:489:8
|
489| fn to_writer<W: no_std_io::Write + no_std_io::Seek>(
| ^^^^^^^^^
= note: this error originates in the derive macro `DekuWrite` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0308`.
error: could not compile `my_crate` (lib) due to 2 previous errorsI have tried a few different re-writes and none seem to work, am I forced to do this? #[derive(DekuRead, DekuWrite, Debug, PartialEq)]
#[deku(id_type = "u8", bits = "2")]
pub enum Direction {
#[deku(id = "0")]
Inbound,
#[deku(id = "1")]
Outbound,
#[deku(id = "2")]
Bidirectional,
#[deku(id = "2")]
Reserved,
} |
Beta Was this translation helpful? Give feedback.
Answered by
amboar
Jan 15, 2026
Replies: 1 comment 1 reply
-
|
The trick is to define a context attribute on the enum. Elsewhere I've done: // MI v2.0, 5.1.1, Figure 77, SFREQ
#[derive(Debug, Clone, Copy, DekuRead, DekuWrite, Eq, PartialEq, PartialOrd)]
#[deku(
bits = "bits.0",
id_type = "u8",
ctx = "endian: Endian, bits: BitSize",
endian = "endian"
)]
#[repr(u8)]
enum SmbusFrequency {
Reserved = 0x00,
Freq100Khz = 0x01,
Freq400Khz = 0x02,
Freq1Mhz = 0x03,
}
// MI v2.0, 5.1.1, Figure 77
#[derive(Debug, DekuWrite, PartialEq)]
#[deku(endian = "little")]
struct GetSmbusI2cFrequencyResponse {
status: ResponseStatus,
#[deku(bits = "4", pad_bits_before = "4", pad_bytes_after = "2")]
sfreq: SmbusFrequency,
}So you could try: use deku::prelude::*;
use super::Id;
#[derive(DekuRead, DekuWrite, Debug, PartialEq)]
pub struct Header {
pub version: u8,
pub port_num: u16,
#[deku(bits = "2")]
pub direction: Direction,
pub id: Id,
}
#[derive(DekuRead, DekuWrite, Debug, PartialEq)]
#[deku(bits = "bits.0", ctx = "bits: BitSize", id_type = "u8")]
#[deku(id_type = "u8")]
pub enum Direction {
#[deku(id = "0")]
Inbound,
#[deku(id = "1")]
Outbound,
#[deku(id = "2")]
Bidirectional,
#[deku(id = "2")]
Reserved,
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
DidingasLushis
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The trick is to define a context attribute on the enum. Elsewhere I've done:
So y…