Skip to content

Commit ce26caf

Browse files
authored
Merge pull request #76 from hashmismatch/nested_example
Nested struct example
2 parents 330795e + 76f2c15 commit ce26caf

File tree

7 files changed

+70
-8
lines changed

7 files changed

+70
-8
lines changed

README.md

+32-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ provide safe packing, unpacking and runtime debugging formatters with per-field
3535

3636
```toml
3737
[dependencies]
38-
packed_struct = "0.5"
38+
packed_struct = "0.6"
3939
```
4040
## Importing the library with the the most common traits and the derive macros
4141

@@ -190,6 +190,37 @@ fn main() -> Result<(), PackingError> {
190190
}
191191
```
192192

193+
## Nested packed types
194+
195+
```rust
196+
use packed_struct::prelude::*;
197+
#[derive(PackedStruct, Debug, PartialEq)]
198+
#[packed_struct(endian="lsb")]
199+
pub struct Duration {
200+
minutes: u8,
201+
seconds: u8,
202+
}
203+
#[derive(PackedStruct, Debug, PartialEq)]
204+
pub struct Record {
205+
#[packed_field(element_size_bytes="2")]
206+
span: Duration,
207+
events: u8,
208+
}
209+
fn main() -> Result<(), PackingError> {
210+
let example = Record {
211+
span: Duration {
212+
minutes: 10,
213+
seconds: 34,
214+
},
215+
events: 3,
216+
};
217+
let packed = example.pack()?;
218+
let unpacked = Record::unpack(&packed)?;
219+
assert_eq!(example, unpacked);
220+
Ok(())
221+
}
222+
```
223+
193224
## Nested packed types within arrays
194225

195226
```rust

packed_struct/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "packed_struct"
33
description = "Binary-level structure packing and unpacking generator"
44
repository = "https://github.com/hashmismatch/packed_struct.rs"
55
homepage = "http://www.hashmismatch.net/libraries/packed-struct/"
6-
version = "0.5.0"
6+
version = "0.6.0"
77
authors = ["Rudi Benkovic <[email protected]>"]
88
build = "build.rs"
99
license = "MIT OR Apache-2.0"
@@ -13,7 +13,7 @@ readme = "../README.md"
1313
edition = "2018"
1414

1515
[dependencies]
16-
packed_struct_codegen = { path = "../packed_struct_codegen/", version = "0.5.0" }
16+
packed_struct_codegen = { path = "../packed_struct_codegen/", version = "0.6.0" }
1717
serde = { version = "1.0", optional = true, default-features = false }
1818
serde_derive = { version = "1.0", optional = true }
1919

packed_struct/src/lib.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
//!
3636
//! ```toml
3737
//! [dependencies]
38-
//! packed_struct = "0.5"
38+
//! packed_struct = "0.6"
3939
//! ```
4040
//! ## Importing the library with the the most common traits and the derive macros
4141
//!
@@ -194,6 +194,37 @@
194194
//! }
195195
//! ```
196196
//!
197+
//! ## Nested packed types
198+
//!
199+
//! ```rust
200+
//! use packed_struct::prelude::*;
201+
//! #[derive(PackedStruct, Debug, PartialEq)]
202+
//! #[packed_struct(endian="lsb")]
203+
//! pub struct Duration {
204+
//! minutes: u8,
205+
//! seconds: u8,
206+
//! }
207+
//! #[derive(PackedStruct, Debug, PartialEq)]
208+
//! pub struct Record {
209+
//! #[packed_field(element_size_bytes="2")]
210+
//! span: Duration,
211+
//! events: u8,
212+
//! }
213+
//! fn main() -> Result<(), PackingError> {
214+
//! let example = Record {
215+
//! span: Duration {
216+
//! minutes: 10,
217+
//! seconds: 34,
218+
//! },
219+
//! events: 3,
220+
//! };
221+
//! let packed = example.pack()?;
222+
//! let unpacked = Record::unpack(&packed)?;
223+
//! assert_eq!(example, unpacked);
224+
//! Ok(())
225+
//! }
226+
//! ```
227+
//!
197228
//! ## Nested packed types within arrays
198229
//!
199230
//! ```rust

packed_struct_codegen/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "packed_struct_codegen"
33
description = "This crate implements the code generation for the packed_struct library."
44
repository = "https://github.com/hashmismatch/packed_struct.rs"
5-
version = "0.5.0"
5+
version = "0.6.0"
66
license = "MIT OR Apache-2.0"
77
authors = ["Rudi Benkovic <[email protected]>"]
88
edition = "2018"

packed_struct_examples/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ publish = false
66
edition = "2018"
77

88
[dependencies]
9-
packed_struct = { path = "../packed_struct/", version = "0.5", features = ["byte_types_64"] }
9+
packed_struct = { path = "../packed_struct/", version = "0.6", features = ["byte_types_64"] }

packed_struct_nostd_tests/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ version = "0.1.0"
44
authors = ["Rudi Benkovic <[email protected]>"]
55

66
[dependencies]
7-
packed_struct = { path = "../packed_struct/", version = "0.5", default-features = false }
7+
packed_struct = { path = "../packed_struct/", version = "0.6", default-features = false }

packed_struct_tests/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ publish = false
66
edition = "2018"
77

88
[dependencies]
9-
packed_struct = { path = "../packed_struct/", version = "0.5", features = ["byte_types_64", "use_serde"] }
9+
packed_struct = { path = "../packed_struct/", version = "0.6", features = ["byte_types_64", "use_serde"] }
1010
error-chain = "0.12.0"
1111
serde = "1.0"

0 commit comments

Comments
 (0)