Skip to content

Commit c29621a

Browse files
authored
Merge pull request #59 from hashmismatch/doc_slice
Main docs update
2 parents 949e2cd + 2194384 commit c29621a

File tree

2 files changed

+92
-30
lines changed

2 files changed

+92
-30
lines changed

README.md

+55-14
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

@@ -238,7 +245,7 @@ Explicit or implicit backing type:
238245
extern crate packed_struct;
239246
#[macro_use] extern crate packed_struct_codegen;
240247

241-
#[derive(PrimitiveEnum, Clone, Copy)]
248+
#[derive(PrimitiveEnum, Clone, Copy, PartialEq, Debug)]
242249
pub enum ImplicitType {
243250
VariantMin = 0,
244251
VariantMax = 255
@@ -250,6 +257,40 @@ pub enum ExplicitType {
250257
VariantMax = 32767
251258
}
252259

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+
253292
```
293+
[crates-badge]: https://img.shields.io/crates/v/packed_struct.svg
294+
[crates-url]: https://crates.io/crates/packed_struct
254295

255296
License: MIT OR Apache-2.0

packed_struct/src/lib.rs

+37-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! Bit-level packing and unpacking for Rust
22
//! ===========================================
33
//!
4-
//! [![Build Status](https://travis-ci.org/hashmismatch/packed_struct.rs.svg?branch=master)](https://travis-ci.org/hashmismatch/packed_struct.rs)
5-
//!
4+
//! [![Crates.io][crates-badge]][crates-url]
65
//! [![Documentation](https://docs.rs/packed_struct/badge.svg)](https://docs.rs/packed_struct)
6+
//! ![master](https://github.com/hashmismatch/packed_struct.rs/workflows/Rust/badge.svg)
77
//!
88
//! # Introduction
99
//!
@@ -29,8 +29,8 @@
2929
//!
3030
//! ```toml
3131
//! [dependencies]
32-
//! packed_struct = "0.3"
33-
//! packed_struct_codegen = "0.3"
32+
//! packed_struct = "0.4"
33+
//! packed_struct_codegen = "0.4"
3434
//! ```
3535
//! ## Including the library and the code generator
3636
//!
@@ -69,20 +69,27 @@
6969
//! DebugMode = 3,
7070
//! }
7171
//!
72-
//! fn main() {
72+
//! fn main() -> Result<(), PackingError> {
7373
//! let test = TestPack {
7474
//! tiny_int: 5.into(),
7575
//! mode: SelfTestMode::DebugMode,
7676
//! enabled: true
7777
//! };
7878
//!
79-
//! let packed = test.pack().unwrap();
79+
//! // pack into a byte array
80+
//! let packed: [u8; 1] = test.pack()?;
8081
//! assert_eq!([0b10111001], packed);
8182
//!
82-
//! let unpacked = TestPack::unpack(&packed).unwrap();
83+
//! // unpack from a byte array
84+
//! let unpacked = TestPack::unpack(&packed)?;
8385
//! assert_eq!(*unpacked.tiny_int, 5);
8486
//! assert_eq!(unpacked.mode, SelfTestMode::DebugMode);
8587
//! assert_eq!(unpacked.enabled, true);
88+
//!
89+
//! // or unpack from a slice
90+
//! let unpacked = TestPack::unpack_from_slice(&packed[..])?;
91+
//!
92+
//! Ok(())
8693
//! }
8794
//! ```
8895
//!
@@ -154,14 +161,15 @@
154161
//! int2: i32
155162
//! }
156163
//!
157-
//! fn main() {
164+
//! fn main() -> Result<(), PackingError> {
158165
//! let example = EndianExample {
159166
//! int1: 0xBBAA,
160167
//! int2: 0x11223344
161168
//! };
162169
//!
163-
//! let packed = example.pack().unwrap();
170+
//! let packed = example.pack()?;
164171
//! assert_eq!([0xAA, 0xBB, 0x11, 0x22, 0x33, 0x44], packed);
172+
//! Ok(())
165173
//! }
166174
//! ```
167175
//!
@@ -179,13 +187,14 @@
179187
//! int1: Integer<u32, packed_bits::Bits24>,
180188
//! }
181189
//!
182-
//! fn main() {
190+
//! fn main() -> Result<(), PackingError> {
183191
//! let example = LsbIntExample {
184192
//! int1: 0xCCBBAA.into()
185193
//! };
186194
//!
187-
//! let packed = example.pack().unwrap();
195+
//! let packed = example.pack()?;
188196
//! assert_eq!([0xAA, 0xBB, 0xCC], packed);
197+
//! Ok(())
189198
//! }
190199
//! ```
191200
//!
@@ -212,7 +221,7 @@
212221
//! values: [TinyFlags; 4]
213222
//! }
214223
//!
215-
//! fn main() {
224+
//! fn main() -> Result<(), PackingError> {
216225
//! let example = Settings {
217226
//! values: [
218227
//! TinyFlags { flag1: true, val1: 1.into(), flag2: false, .. TinyFlags::default() },
@@ -222,10 +231,11 @@
222231
//! ]
223232
//! };
224233
//!
225-
//! let packed = example.pack().unwrap();
226-
//! let unpacked = Settings::unpack(&packed).unwrap();
234+
//! let packed = example.pack()?;
235+
//! let unpacked = Settings::unpack(&packed)?;
227236
//!
228237
//! assert_eq!(example, unpacked);
238+
//! Ok(())
229239
//! }
230240
//! ```
231241
//!
@@ -239,7 +249,7 @@
239249
//! extern crate packed_struct;
240250
//! #[macro_use] extern crate packed_struct_codegen;
241251
//!
242-
//! #[derive(PrimitiveEnum, Clone, Copy)]
252+
//! #[derive(PrimitiveEnum, Clone, Copy, PartialEq, Debug)]
243253
//! pub enum ImplicitType {
244254
//! VariantMin = 0,
245255
//! VariantMax = 255
@@ -251,7 +261,16 @@
251261
//! VariantMax = 32767
252262
//! }
253263
//!
254-
//! # fn main() {}
264+
//! fn main() {
265+
//! use packed_struct::PrimitiveEnum;
266+
//!
267+
//! let t = ImplicitType::VariantMin;
268+
//! let tn: u8 = t.to_primitive();
269+
//! assert_eq!(0, tn);
270+
//!
271+
//! let t = ImplicitType::from_primitive(255).unwrap();
272+
//! assert_eq!(ImplicitType::VariantMax, t);
273+
//! }
255274
//! ```
256275
//!
257276
//! # Primitive enum packing with support for catch-all unknown values
@@ -277,6 +296,8 @@
277296
//!
278297
//! # fn main() {}
279298
//! ```
299+
//! [crates-badge]: https://img.shields.io/crates/v/packed_struct.svg
300+
//! [crates-url]: https://crates.io/crates/packed_struct
280301
281302
#![cfg_attr(not(feature = "std"), no_std)]
282303

0 commit comments

Comments
 (0)