Skip to content

Commit 52f8b43

Browse files
Improve error message for id_type and missing repr (#596)
* This piece of code will only fire if you are using id_type with repr(inttype). Thus, print out what is actually required to fix this error. * Add to docs
1 parent 3dd5369 commit 52f8b43

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

deku-derive/src/macros/deku_read.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
264264
let Some(repr) = input.repr else {
265265
return Err(syn::Error::new(
266266
variant.ident.span(),
267-
"DekuRead: `id_type` must be specified on non-unit variants",
267+
"DekuRead: `id_type` with non-unit variants requires primitive representation i.e. `repr(inttype)`",
268268
));
269269
};
270270
if let Some(id_type) = id_type {

deku-derive/src/macros/deku_write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> {
278278
None => {
279279
return Err(syn::Error::new(
280280
variant.ident.span(),
281-
"DekuWrite: `id_type` must be specified on non-unit variants",
281+
"DekuWrite: `id_type` with non-unit variants requires primitive representation i.e. `repr(inttype)`",
282282
));
283283
}
284284
Some(repr) => {

src/attributes.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,6 +2054,25 @@ assert_eq!(data, value);
20542054
# fn main() {}
20552055
```
20562056
2057+
2058+
When using `id_type` with non-unit variants, Deku requires [primitive representation](https://doc.rust-lang.org/reference/type-layout.html#primitive-representations).
2059+
Deku uses this for calculating the discriminant when reading and writing.
2060+
```rust
2061+
# use core::convert::{TryInto, TryFrom};
2062+
# use deku::prelude::*;
2063+
# #[cfg(feature = "std")]
2064+
# use std::io::Cursor;
2065+
#[derive(Copy, Clone, Debug, DekuRead, DekuWrite)]
2066+
#[deku(endian = "endian", ctx = "endian: deku::ctx::Endian", id_type = "u8")]
2067+
#[repr(u8)]
2068+
enum OpKind {
2069+
Id = 0,
2070+
Opcode = 255,
2071+
}
2072+
```
2073+
2074+
2075+
20572076
# bytes
20582077
20592078
Set the byte size of the enum variant `id`

tests/test_compile/cases/enum_validation.stderr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ error: DekuWrite: cannot determine write `id`. must provide storage for the id o
8888
132 | B,
8989
| ^
9090

91-
error: DekuWrite: `id_type` must be specified on non-unit variants
91+
error: DekuWrite: `id_type` with non-unit variants requires primitive representation i.e. `repr(inttype)`
9292
--> tests/test_compile/cases/enum_validation.rs:139:5
9393
|
9494
139 | A = 0,
9595
| ^
9696

97-
error: DekuRead: `id_type` must be specified on non-unit variants
97+
error: DekuRead: `id_type` with non-unit variants requires primitive representation i.e. `repr(inttype)`
9898
--> tests/test_compile/cases/enum_validation.rs:148:5
9999
|
100100
148 | Base = 0x00,
@@ -106,25 +106,25 @@ error: DekuRead: `repr` must match `id_type`
106106
156 | Base = 0x00,
107107
| ^^^^
108108

109-
error: DekuRead: `id_type` must be specified on non-unit variants
109+
error: DekuRead: `id_type` with non-unit variants requires primitive representation i.e. `repr(inttype)`
110110
--> tests/test_compile/cases/enum_validation.rs:163:5
111111
|
112112
163 | Base = 0x00,
113113
| ^^^^
114114

115-
error: DekuRead: `id_type` must be specified on non-unit variants
115+
error: DekuRead: `id_type` with non-unit variants requires primitive representation i.e. `repr(inttype)`
116116
--> tests/test_compile/cases/enum_validation.rs:170:5
117117
|
118118
170 | Base = 0x00,
119119
| ^^^^
120120

121-
error: DekuRead: `id_type` must be specified on non-unit variants
121+
error: DekuRead: `id_type` with non-unit variants requires primitive representation i.e. `repr(inttype)`
122122
--> tests/test_compile/cases/enum_validation.rs:177:5
123123
|
124124
177 | Base = 0x00,
125125
| ^^^^
126126

127-
error: DekuWrite: `id_type` must be specified on non-unit variants
127+
error: DekuWrite: `id_type` with non-unit variants requires primitive representation i.e. `repr(inttype)`
128128
--> tests/test_compile/cases/enum_validation.rs:184:5
129129
|
130130
184 | Base = 0x00,
@@ -136,19 +136,19 @@ error: DekuWrite: `repr` must match `id_type`
136136
192 | Base = 0x00,
137137
| ^^^^
138138

139-
error: DekuWrite: `id_type` must be specified on non-unit variants
139+
error: DekuWrite: `id_type` with non-unit variants requires primitive representation i.e. `repr(inttype)`
140140
--> tests/test_compile/cases/enum_validation.rs:199:5
141141
|
142142
199 | Base = 0x00,
143143
| ^^^^
144144

145-
error: DekuWrite: `id_type` must be specified on non-unit variants
145+
error: DekuWrite: `id_type` with non-unit variants requires primitive representation i.e. `repr(inttype)`
146146
--> tests/test_compile/cases/enum_validation.rs:206:5
147147
|
148148
206 | Base = 0x00,
149149
| ^^^^
150150

151-
error: DekuWrite: `id_type` must be specified on non-unit variants
151+
error: DekuWrite: `id_type` with non-unit variants requires primitive representation i.e. `repr(inttype)`
152152
--> tests/test_compile/cases/enum_validation.rs:213:5
153153
|
154154
213 | Base = 0x00,

0 commit comments

Comments
 (0)