1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//! # Bitbite derive
//! The Bitbite derive is used to save you a bit of code and make the usage cleaner. \
//! For the cleanest code I recommend using [deref-derive](https://crates.io/crates/deref_derive)
//! # Usage:
//! ```rust
//! use bitbite::*;
//! use bitbite_derive::Bitbite;
//! use deref_derive::*;
//!
//! #[derive(Bitbite, Deref, DerefMut)]
//! struct Header(pub u8);
//!
//! impl Header {
//! pub const FIRST_NIBBLE_FLAG : Flag<u8> = Flag::new(0b0000_1111);
//! }
//!
//! let h = Header(0b1100_1010);
//! assert_eq!(h.get_flag(&Header::FIRST_NIBBLE_FLAG), 0b1010);
//! ```
//! # Multi fielded structs
//! In case you have more than one field you can use the `bitbite` attribute
//! # Usage:
//! ```rust
//! use bitbite::*;
//! use bitbite_derive::Bitbite;
//! use deref_derive::*;
//!
//! #[derive(Bitbite, Deref, DerefMut)]
//! #[bitbite(u16)]
//! struct Header {
//! #[deref]
//! pub field: u16,
//! pub count:u8
//! }
//!
//! impl Header {
//! pub const MIDDLE_NIBBLE : Flag<u16> = Flag::new(0b0000_0011_1100_0000);
//! }
//!
//! let h = Header {
//! field: 0b0010_1010_1000_0110,
//! count : 0,
//! };
//! assert_eq!(h.get_flag(&Header::MIDDLE_NIBBLE), 0b1010);
//! ```
use TokenStream;
use quote;
const ATTR_NAME: &str = "bitbite";
/// # Bitbite derive
/// The Bitbite derive is used to save you a bit of code and make the usage cleaner. \
/// For the cleanest code I recommend using [deref-derive](https://crates.io/crates/deref_derive)
/// # Usage:
/// ```
/// use bitbite::*;
/// use bitbite_derive::Bitbite;
/// use deref_derive::*;
///
/// #[derive(Bitbite, Deref, DerefMut)]
/// struct Header(pub u8);
///
/// impl Header {
/// pub const FIRST_NIBBLE_FLAG : Flag<u8> = Flag::new(0b0000_1111);
/// }
///
/// let h = Header(0b1100_1010);
/// assert_eq!(h.get_flag(&Header::FIRST_NIBBLE_FLAG), 0b1010);
/// ```
/// # Multi fielded structs
/// In case you have more than one field you can use the `bitbite` attribute
/// # Usage:
/// ```rust
/// use bitbite::*;
/// use bitbite_derive::Bitbite;
/// use deref_derive::*;
///
/// #[derive(Bitbite, Deref, DerefMut)]
/// #[bitbite(u16)]
/// struct Header {
/// #[deref]
/// pub field: u16,
/// pub count:u8
/// }
///
/// impl Header {
/// pub const MIDDLE_NIBBLE : Flag<u16> = Flag::new(0b0000_0011_1100_0000);
/// }
///
/// let h = Header {
/// field: 0b0010_1010_1000_0110,
/// count : 0,
/// };
/// assert_eq!(h.get_flag(&Header::MIDDLE_NIBBLE), 0b1010);
/// ```