Skip to content

Commit 51f779d

Browse files
authored
Merge pull request #67 from hashmismatch/rust_2018
Rust 2018 updates, readme update
2 parents c845cd8 + a886032 commit 51f779d

File tree

20 files changed

+84
-103
lines changed

20 files changed

+84
-103
lines changed

README.md

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ 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-
## Introduction
8+
# Introduction
99

1010
Packing and unpacking bit-level structures is usually a programming tasks that needlessly reinvents the wheel. This library provides
1111
a meta-programming approach, using attributes to define fields and how they should be packed. The resulting trait implementations
1212
provide safe packing, unpacking and runtime debugging formatters with per-field documentation generated for each structure.
1313

14-
## Features
14+
# Features
1515

1616
* Plain Rust structures, decorated with attributes
1717
* MSB or LSB integers of user-defined bit widths
@@ -23,29 +23,32 @@ provide safe packing, unpacking and runtime debugging formatters with per-field
2323
* Arrays of packed structures as fields
2424
* Reserved fields, their bits are always 0 or 1
2525

26-
## Sample usage
26+
# Crate-level feature flags
27+
* `std`: use the Rust standard library. Default.
28+
* `alloc`: use the `alloc` crate for `no_std` + `alloc` scenarios. Requires nightly Rust.
29+
* `use_serde`: add serialization support to the built-in helper types.
30+
* `byte_types_64`, `byte_types_256`: enlarge the size of the generated array, byte and bit width types.
2731

28-
### Cargo.toml
32+
# Sample usage
33+
34+
## Cargo.toml
2935

3036
```toml
3137
[dependencies]
32-
packed_struct = "0.4"
33-
packed_struct_codegen = "0.4"
38+
packed_struct = "0.5"
3439
```
35-
### Including the library and the code generator
40+
## Importing the library with the the most common traits and the derive macros
3641

3742
```rust
38-
extern crate packed_struct;
39-
#[macro_use]
40-
extern crate packed_struct_codegen;
43+
// This is only needed for pre Rust 2018
44+
#[macro_use] extern crate packed_struct;
45+
// Prelude import with the common imports
46+
use packed_struct::prelude::*;
4147
```
4248

43-
### Example of a single-byte structure, with a 3 bit integer, primitive enum and a bool field.
49+
## Example of a single-byte structure, with a 3 bit integer, primitive enum and a bool field.
4450

4551
```rust
46-
extern crate packed_struct;
47-
#[macro_use] extern crate packed_struct_codegen;
48-
4952
use packed_struct::prelude::*;
5053

5154
#[derive(PackedStruct)]
@@ -91,13 +94,12 @@ fn main() -> Result<(), PackingError> {
9194
}
9295
```
9396

94-
## Packing attributes
97+
# Packing attributes
9598

96-
### Syntax
99+
## Syntax
97100

98101
```rust
99-
extern crate packed_struct;
100-
#[macro_use] extern crate packed_struct_codegen;
102+
use packed_struct::prelude::*;
101103

102104
#[derive(PackedStruct)]
103105
#[packed_struct(attr1="val", attr2="val")]
@@ -107,15 +109,15 @@ pub struct Structure {
107109
}
108110
```
109111

110-
### Per-structure attributes
112+
## Per-structure attributes
111113

112114
Attribute | Values | Comment
113115
:--|:--|:--
114116
```size_bytes``` | ```1``` ... n | Size of the packed byte stream
115117
```bit_numbering``` | ```msb0``` or ```lsb0``` | Bit numbering for bit positioning of fields. Required if the bits attribute field is used.
116118
```endian``` | ```msb``` or ```lsb``` | Default integer endianness
117119

118-
### Per-field attributes
120+
## Per-field attributes
119121

120122
Attribute | Values | Comment
121123
:--|:--|:--
@@ -128,7 +130,7 @@ Attribute | Values | Comment
128130
```ty``` | ```enum``` | Packing helper for primitive enums.
129131
```endian``` | ```msb``` or ```lsb``` | Integer endianness. Applies to u16/i16 and larger types.
130132

131-
### Bit and byte positioning
133+
## Bit and byte positioning
132134

133135
Used for either ```bits``` or ```bytes``` on fields. The examples are for MSB0 positioning.
134136

@@ -139,14 +141,11 @@ Value | Comment
139141
```0..2``` | Exclusive range, bits zero and one
140142
```0:1```, ```0..=1``` | Inclusive range, bits zero and one
141143

142-
## More examples
144+
# More examples
143145

144-
### Mixed endian integers
146+
## Mixed endian integers
145147

146148
```rust
147-
extern crate packed_struct;
148-
#[macro_use] extern crate packed_struct_codegen;
149-
150149
use packed_struct::prelude::*;
151150

152151
#[derive(PackedStruct)]
@@ -169,12 +168,9 @@ fn main() -> Result<(), PackingError> {
169168
}
170169
```
171170

172-
### 24 bit LSB integers
171+
## 24 bit LSB integers
173172

174173
```rust
175-
extern crate packed_struct;
176-
#[macro_use] extern crate packed_struct_codegen;
177-
178174
use packed_struct::prelude::*;
179175

180176
#[derive(PackedStruct)]
@@ -194,12 +190,9 @@ fn main() -> Result<(), PackingError> {
194190
}
195191
```
196192

197-
### Nested packed types within arrays
193+
## Nested packed types within arrays
198194

199195
```rust
200-
extern crate packed_struct;
201-
#[macro_use] extern crate packed_struct_codegen;
202-
203196
use packed_struct::prelude::*;
204197

205198
#[derive(PackedStruct, Default, Debug, PartialEq)]
@@ -235,15 +228,14 @@ fn main() -> Result<(), PackingError> {
235228
}
236229
```
237230

238-
## Primitive enums with simple discriminants
231+
# Primitive enums with simple discriminants
239232

240233
Supported backing integer types: ```u8```, ```u16```, ```u32```, ```u64```, ```i8```, ```i16```, ```i32```, ```i64```.
241234

242235
Explicit or implicit backing type:
243236

244237
```rust
245-
extern crate packed_struct;
246-
#[macro_use] extern crate packed_struct_codegen;
238+
use packed_struct::prelude::*;
247239

248240
#[derive(PrimitiveEnum, Clone, Copy, PartialEq, Debug)]
249241
pub enum ImplicitType {
@@ -269,11 +261,10 @@ fn main() {
269261
}
270262
```
271263

272-
## Primitive enum packing with support for catch-all unknown values
264+
# Primitive enum packing with support for catch-all unknown values
273265

274266
```rust
275-
extern crate packed_struct;
276-
#[macro_use] extern crate packed_struct_codegen;
267+
use packed_struct::prelude::*;
277268

278269
#[derive(PrimitiveEnum_u8, Debug, Clone, Copy)]
279270
pub enum Field {

packed_struct/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ license = "MIT OR Apache-2.0"
1010
keywords = ["enum", "packing", "serialization"]
1111
categories = ["encoding"]
1212
readme = "../README.md"
13+
edition = "2018"
1314

1415
[dependencies]
1516
packed_struct_codegen = { path = "../packed_struct_codegen/", version = "0.5.0" }

packed_struct/src/debug_fmt.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Helper structures for runtime packing visualization.
22
3-
use internal_prelude::v1::*;
3+
use crate::internal_prelude::v1::*;
44

55
#[cfg(any(feature="alloc", feature="std"))]
66
pub trait PackedStructDebug {
@@ -101,8 +101,7 @@ impl<'a, P> PackedStructDisplay<'a, P> {
101101
}
102102
}
103103

104-
use packing::{PackedStruct};
105-
104+
use crate::packing::PackedStruct;
106105
use crate::types_bits::ByteArray;
107106

108107
impl<'a, P> fmt::Display for PackedStructDisplay<'a, P> where P: PackedStruct + PackedStructDebug {

packed_struct/src/lib.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
//! * Arrays of packed structures as fields
2424
//! * Reserved fields, their bits are always 0 or 1
2525
//!
26+
//! # Crate-level feature flags
27+
//! * `std`: use the Rust standard library. Default.
28+
//! * `alloc`: use the `alloc` crate for `no_std` + `alloc` scenarios. Requires nightly Rust.
29+
//! * `use_serde`: add serialization support to the built-in helper types.
30+
//! * `byte_types_64`, `byte_types_256`: enlarge the size of the generated array, byte and bit width types.
31+
//!
2632
//! # Sample usage
2733
//!
2834
//! ## Cargo.toml
@@ -31,20 +37,20 @@
3137
//! [dependencies]
3238
//! packed_struct = "0.5"
3339
//! ```
34-
//! ## Importing the library with the derive macros
40+
//! ## Importing the library with the the most common traits and the derive macros
3541
//!
3642
//! ```rust
37-
//! #[macro_use]
38-
//! extern crate packed_struct;
43+
//! // This is only needed for pre Rust 2018
44+
//! #[macro_use] extern crate packed_struct;
45+
//! // Prelude import with the common imports
46+
//! use packed_struct::prelude::*;
3947
//! # fn main() {
4048
//! # }
4149
//! ```
4250
//!
4351
//! ## Example of a single-byte structure, with a 3 bit integer, primitive enum and a bool field.
4452
//!
4553
//! ```rust
46-
//! #[macro_use] extern crate packed_struct;
47-
//!
4854
//! use packed_struct::prelude::*;
4955
//!
5056
//! #[derive(PackedStruct)]
@@ -95,7 +101,6 @@
95101
//! ## Syntax
96102
//!
97103
//! ```rust
98-
//! #[macro_use] extern crate packed_struct;
99104
//! use packed_struct::prelude::*;
100105
//!
101106
//! #[derive(PackedStruct)]
@@ -145,8 +150,6 @@
145150
//! ## Mixed endian integers
146151
//!
147152
//! ```rust
148-
//! #[macro_use] extern crate packed_struct;
149-
//!
150153
//! use packed_struct::prelude::*;
151154
//!
152155
//! #[derive(PackedStruct)]
@@ -172,8 +175,6 @@
172175
//! ## 24 bit LSB integers
173176
//!
174177
//! ```rust
175-
//! #[macro_use] extern crate packed_struct;
176-
//!
177178
//! use packed_struct::prelude::*;
178179
//!
179180
//! #[derive(PackedStruct)]
@@ -196,8 +197,6 @@
196197
//! ## Nested packed types within arrays
197198
//!
198199
//! ```rust
199-
//! #[macro_use] extern crate packed_struct;
200-
//!
201200
//! use packed_struct::prelude::*;
202201
//!
203202
//! #[derive(PackedStruct, Default, Debug, PartialEq)]
@@ -240,7 +239,6 @@
240239
//! Explicit or implicit backing type:
241240
//!
242241
//! ```rust
243-
//! #[macro_use] extern crate packed_struct;
244242
//! use packed_struct::prelude::*;
245243
//!
246244
//! #[derive(PrimitiveEnum, Clone, Copy, PartialEq, Debug)]
@@ -270,7 +268,6 @@
270268
//! # Primitive enum packing with support for catch-all unknown values
271269
//!
272270
//! ```rust
273-
//! #[macro_use] extern crate packed_struct;
274271
//! use packed_struct::prelude::*;
275272
//!
276273
//! #[derive(PrimitiveEnum_u8, Debug, Clone, Copy)]
@@ -363,22 +360,20 @@ pub mod prelude {
363360
364361
pub use super::derive::*;
365362

366-
pub use PackedStruct;
367-
pub use PackedStructSlice;
368-
pub use PackingError;
363+
pub use crate::{PackedStruct, PackedStructSlice, PackingError};
369364

370-
pub use PrimitiveEnum;
365+
pub use crate::PrimitiveEnum;
371366
#[cfg(any(feature="alloc", feature="std"))]
372-
pub use PrimitiveEnumDynamicStr;
367+
pub use crate::PrimitiveEnumDynamicStr;
373368

374369
#[cfg(not(any(feature="alloc", feature="std")))]
375-
pub use PrimitiveEnumStaticStr;
370+
pub use crate::PrimitiveEnumStaticStr;
376371

377372

378-
pub use EnumCatchAll;
373+
pub use crate::EnumCatchAll;
379374

380-
pub use types::*;
381-
pub use types::bits as packed_bits;
375+
pub use crate::types::*;
376+
pub use crate::types::bits as packed_bits;
382377
}
383378

384379
use internal_prelude::v1::*;

packed_struct/src/packing.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use internal_prelude::v1::*;
1+
use crate::internal_prelude::v1::*;
22

33
use crate::types_bits::ByteArray;
44

@@ -57,8 +57,8 @@ pub enum PackingError {
5757
InternalError
5858
}
5959

60-
impl Display for PackingError {
61-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
60+
impl crate::Display for PackingError {
61+
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
6262
write!(f, "{:?}", self)
6363
}
6464
}
@@ -81,7 +81,7 @@ impl ::std::error::Error for PackingError {
8181
}
8282
}
8383

84-
impl From<PackingError> for fmt::Error {
84+
impl From<PackingError> for crate::fmt::Error {
8585
fn from(_: PackingError) -> Self {
8686
Self
8787
}

packed_struct/src/primitive_enum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use internal_prelude::v1::*;
1+
use crate::internal_prelude::v1::*;
22

33
/// An enum type that can be packed or unpacked from a simple primitive integer.
44
pub trait PrimitiveEnum where Self: Sized + Copy {

packed_struct/src/types_bits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Unit bit sizes, used as a type parameter to concrete types to signify their
22
//! intended size.
33
4-
use internal_prelude::v1::*;
4+
use crate::internal_prelude::v1::*;
55

66
/// Number of bits that the generic type should occupy.
77
pub trait NumberOfBits: Copy + Clone + Debug + Default {

packed_struct/src/types_num.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! as a native type, packing and unpacking into byte arrays, with MSB/LSB
33
//! support.
44
5-
use internal_prelude::v1::*;
5+
use crate::internal_prelude::v1::*;
66
use crate::{PackingResult, lib_get_slice, lib_get_mut_slice};
77

88
use super::types_bits::*;
@@ -719,7 +719,7 @@ fn test_struct_info() {
719719

720720
#[test]
721721
fn test_slice_packing() {
722-
use packing::PackedStructSlice;
722+
use crate::packing::PackedStructSlice;
723723

724724
let mut data = vec![0xAA, 0xBB, 0xCC, 0xDD];
725725
let unpacked = <MsbInteger<_, _, Integer<u32, Bits32>>>::unpack_from_slice(&data).unwrap();
@@ -740,7 +740,7 @@ fn test_packed_int_lsb_sub() {
740740

741741
#[test]
742742
fn test_big_slice_unpacking() {
743-
use packing::PackedStructSlice;
743+
use crate::packing::PackedStructSlice;
744744

745745
let data = vec![0xAA, 0xBB, 0xCC, 0xDD];
746746
let unpacked = <MsbInteger<_, _, Integer<u32, Bits32>>>::unpack_from_slice(&data).unwrap();

0 commit comments

Comments
 (0)