Skip to content

Commit b3cc39c

Browse files
committed
readme sync with cargo-readme
1 parent 3cd745b commit b3cc39c

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

README.md

+44-13
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ Bit-level packing and unpacking for Rust
55
[![Documentation](https://docs.rs/packed_struct/badge.svg)](https://docs.rs/packed_struct)
66
![master](https://github.com/hashmismatch/packed_struct.rs/workflows/Rust/badge.svg)
77

8-
[crates-badge]: https://img.shields.io/crates/v/packed_struct.svg
9-
[crates-url]: https://crates.io/crates/packed_struct
10-
118
## Introduction
129

1310
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 {
7067
DebugMode = 3,
7168
}
7269

73-
fn main() {
70+
fn main() -> Result<(), PackingError> {
7471
let test = TestPack {
7572
tiny_int: 5.into(),
7673
mode: SelfTestMode::DebugMode,
7774
enabled: true
7875
};
7976

80-
let packed = test.pack();
77+
// pack into a byte array
78+
let packed: [u8; 1] = test.pack()?;
8179
assert_eq!([0b10111001], packed);
8280

83-
let unpacked = TestPack::unpack(&packed).unwrap();
81+
// unpack from a byte array
82+
let unpacked = TestPack::unpack(&packed)?;
8483
assert_eq!(*unpacked.tiny_int, 5);
8584
assert_eq!(unpacked.mode, SelfTestMode::DebugMode);
8685
assert_eq!(unpacked.enabled, true);
86+
87+
// or unpack from a slice
88+
let unpacked = TestPack::unpack_from_slice(&packed[..])?;
89+
90+
Ok(())
8791
}
8892
```
8993

@@ -153,14 +157,15 @@ pub struct EndianExample {
153157
int2: i32
154158
}
155159

156-
fn main() {
160+
fn main() -> Result<(), PackingError> {
157161
let example = EndianExample {
158162
int1: 0xBBAA,
159163
int2: 0x11223344
160164
};
161165

162-
let packed = example.pack();
166+
let packed = example.pack()?;
163167
assert_eq!([0xAA, 0xBB, 0x11, 0x22, 0x33, 0x44], packed);
168+
Ok(())
164169
}
165170
```
166171

@@ -178,13 +183,14 @@ pub struct LsbIntExample {
178183
int1: Integer<u32, packed_bits::Bits24>,
179184
}
180185

181-
fn main() {
186+
fn main() -> Result<(), PackingError> {
182187
let example = LsbIntExample {
183188
int1: 0xCCBBAA.into()
184189
};
185190

186-
let packed = example.pack();
191+
let packed = example.pack()?;
187192
assert_eq!([0xAA, 0xBB, 0xCC], packed);
193+
Ok(())
188194
}
189195
```
190196

@@ -211,7 +217,7 @@ pub struct Settings {
211217
values: [TinyFlags; 4]
212218
}
213219

214-
fn main() {
220+
fn main() -> Result<(), PackingError> {
215221
let example = Settings {
216222
values: [
217223
TinyFlags { flag1: true, val1: 1.into(), flag2: false, .. TinyFlags::default() },
@@ -221,10 +227,11 @@ fn main() {
221227
]
222228
};
223229

224-
let packed = example.pack();
225-
let unpacked = Settings::unpack(&packed).unwrap();
230+
let packed = example.pack()?;
231+
let unpacked = Settings::unpack(&packed)?;
226232

227233
assert_eq!(example, unpacked);
234+
Ok(())
228235
}
229236
```
230237

@@ -252,4 +259,28 @@ pub enum ExplicitType {
252259

253260
```
254261

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+
255286
License: MIT OR Apache-2.0

0 commit comments

Comments
 (0)