Skip to content

Commit 2194384

Browse files
committed
primitive enum example
1 parent b3cc39c commit 2194384

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ Explicit or implicit backing type:
245245
extern crate packed_struct;
246246
#[macro_use] extern crate packed_struct_codegen;
247247

248-
#[derive(PrimitiveEnum, Clone, Copy)]
248+
#[derive(PrimitiveEnum, Clone, Copy, PartialEq, Debug)]
249249
pub enum ImplicitType {
250250
VariantMin = 0,
251251
VariantMax = 255
@@ -257,6 +257,16 @@ pub enum ExplicitType {
257257
VariantMax = 32767
258258
}
259259

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+
}
260270
```
261271

262272
## Primitive enum packing with support for catch-all unknown values

packed_struct/src/lib.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@
249249
//! extern crate packed_struct;
250250
//! #[macro_use] extern crate packed_struct_codegen;
251251
//!
252-
//! #[derive(PrimitiveEnum, Clone, Copy)]
252+
//! #[derive(PrimitiveEnum, Clone, Copy, PartialEq, Debug)]
253253
//! pub enum ImplicitType {
254254
//! VariantMin = 0,
255255
//! VariantMax = 255
@@ -261,7 +261,16 @@
261261
//! VariantMax = 32767
262262
//! }
263263
//!
264-
//! # 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+
//! }
265274
//! ```
266275
//!
267276
//! # Primitive enum packing with support for catch-all unknown values

0 commit comments

Comments
 (0)