@@ -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
@@ -252,4 +259,28 @@ pub enum ExplicitType {
252
259
253
260
```
254
261
262
+ ## Primitive enum packing with support for catch-all unknown values
263
+
264
+ ``` rust
265
+ extern crate packed_struct;
266
+ #[macro_use] extern crate packed_struct_codegen;
267
+
268
+ #[derive(PrimitiveEnum_u8 , Debug , Clone , Copy )]
269
+ pub enum Field {
270
+ A = 1 ,
271
+ B = 2 ,
272
+ C = 3
273
+ }
274
+
275
+ #[derive(PackedStruct , Debug , PartialEq )]
276
+ #[packed_struct(bit_numbering= " msb0" )]
277
+ pub struct Register {
278
+ #[packed_field(bits= " 0..4" , ty= " enum" )]
279
+ field : EnumCatchAll <Field >
280
+ }
281
+
282
+ ```
283
+ [ crates-badge ] : https://img.shields.io/crates/v/packed_struct.svg
284
+ [ crates-url ] : https://crates.io/crates/packed_struct
285
+
255
286
License: MIT OR Apache-2.0
0 commit comments