@@ -5,9 +5,6 @@ Bit-level packing and unpacking for Rust
5
5
[ ![ Documentation] ( https://docs.rs/packed_struct/badge.svg )] ( https://docs.rs/packed_struct )
6
6
![ master] ( https://github.com/hashmismatch/packed_struct.rs/workflows/Rust/badge.svg )
7
7
8
- [ crates-badge ] : https://img.shields.io/crates/v/packed_struct.svg
9
- [ crates-url ] : https://crates.io/crates/packed_struct
10
-
11
8
## Introduction
12
9
13
10
Packing and unpacking bit-level structures is usually a programming tasks that needlessly reinvents the wheel. This library provides
@@ -70,20 +67,27 @@ pub enum SelfTestMode {
70
67
DebugMode = 3 ,
71
68
}
72
69
73
- fn main () {
70
+ fn main () -> Result <(), PackingError > {
74
71
let test = TestPack {
75
72
tiny_int : 5. into (),
76
73
mode : SelfTestMode :: DebugMode ,
77
74
enabled : true
78
75
};
79
76
80
- let packed = test . pack ();
77
+ // pack into a byte array
78
+ let packed : [u8 ; 1 ] = test . pack ()? ;
81
79
assert_eq! ([0b10111001 ], packed );
82
80
83
- let unpacked = TestPack :: unpack (& packed ). unwrap ();
81
+ // unpack from a byte array
82
+ let unpacked = TestPack :: unpack (& packed )? ;
84
83
assert_eq! (* unpacked . tiny_int, 5 );
85
84
assert_eq! (unpacked . mode, SelfTestMode :: DebugMode );
86
85
assert_eq! (unpacked . enabled, true );
86
+
87
+ // or unpack from a slice
88
+ let unpacked = TestPack :: unpack_from_slice (& packed [.. ])? ;
89
+
90
+ Ok (())
87
91
}
88
92
```
89
93
@@ -153,14 +157,15 @@ pub struct EndianExample {
153
157
int2 : i32
154
158
}
155
159
156
- fn main () {
160
+ fn main () -> Result <(), PackingError > {
157
161
let example = EndianExample {
158
162
int1 : 0xBBAA ,
159
163
int2 : 0x11223344
160
164
};
161
165
162
- let packed = example . pack ();
166
+ let packed = example . pack ()? ;
163
167
assert_eq! ([0xAA , 0xBB , 0x11 , 0x22 , 0x33 , 0x44 ], packed );
168
+ Ok (())
164
169
}
165
170
```
166
171
@@ -178,13 +183,14 @@ pub struct LsbIntExample {
178
183
int1 : Integer <u32 , packed_bits :: Bits24 >,
179
184
}
180
185
181
- fn main () {
186
+ fn main () -> Result <(), PackingError > {
182
187
let example = LsbIntExample {
183
188
int1 : 0xCCBBAA . into ()
184
189
};
185
190
186
- let packed = example . pack ();
191
+ let packed = example . pack ()? ;
187
192
assert_eq! ([0xAA , 0xBB , 0xCC ], packed );
193
+ Ok (())
188
194
}
189
195
```
190
196
@@ -211,7 +217,7 @@ pub struct Settings {
211
217
values : [TinyFlags ; 4 ]
212
218
}
213
219
214
- fn main () {
220
+ fn main () -> Result <(), PackingError > {
215
221
let example = Settings {
216
222
values : [
217
223
TinyFlags { flag1 : true , val1 : 1. into (), flag2 : false , .. TinyFlags :: default () },
@@ -221,10 +227,11 @@ fn main() {
221
227
]
222
228
};
223
229
224
- let packed = example . pack ();
225
- let unpacked = Settings :: unpack (& packed ). unwrap () ;
230
+ let packed = example . pack ()? ;
231
+ let unpacked = Settings :: unpack (& packed )? ;
226
232
227
233
assert_eq! (example , unpacked );
234
+ Ok (())
228
235
}
229
236
```
230
237
@@ -238,7 +245,7 @@ Explicit or implicit backing type:
238
245
extern crate packed_struct;
239
246
#[macro_use] extern crate packed_struct_codegen;
240
247
241
- #[derive(PrimitiveEnum , Clone , Copy )]
248
+ #[derive(PrimitiveEnum , Clone , Copy , PartialEq , Debug )]
242
249
pub enum ImplicitType {
243
250
VariantMin = 0 ,
244
251
VariantMax = 255
@@ -250,6 +257,40 @@ pub enum ExplicitType {
250
257
VariantMax = 32767
251
258
}
252
259
260
+ fn main () {
261
+ use packed_struct :: PrimitiveEnum ;
262
+
263
+ let t = ImplicitType :: VariantMin ;
264
+ let tn : u8 = t . to_primitive ();
265
+ assert_eq! (0 , tn );
266
+
267
+ let t = ImplicitType :: from_primitive (255 ). unwrap ();
268
+ assert_eq! (ImplicitType :: VariantMax , t );
269
+ }
270
+ ```
271
+
272
+ ## Primitive enum packing with support for catch-all unknown values
273
+
274
+ ``` rust
275
+ extern crate packed_struct;
276
+ #[macro_use] extern crate packed_struct_codegen;
277
+
278
+ #[derive(PrimitiveEnum_u8 , Debug , Clone , Copy )]
279
+ pub enum Field {
280
+ A = 1 ,
281
+ B = 2 ,
282
+ C = 3
283
+ }
284
+
285
+ #[derive(PackedStruct , Debug , PartialEq )]
286
+ #[packed_struct(bit_numbering= " msb0" )]
287
+ pub struct Register {
288
+ #[packed_field(bits= " 0..4" , ty= " enum" )]
289
+ field : EnumCatchAll <Field >
290
+ }
291
+
253
292
```
293
+ [ crates-badge ] : https://img.shields.io/crates/v/packed_struct.svg
294
+ [ crates-url ] : https://crates.io/crates/packed_struct
254
295
255
296
License: MIT OR Apache-2.0
0 commit comments