Macro bitfield::bitfield_fields
source · macro_rules! bitfield_fields { (@field $(#[$attribute:meta])* ($($vis:tt)*) $t:ty, $from:ty, $into:ty, _, $setter:ident: $msb:expr, $lsb:expr, $count:expr) => { ... }; (@field $(#[$attribute:meta])* ($($vis:tt)*) $t:ty, $from:ty, $into:ty, _, $setter:ident: $msb:expr, $lsb:expr) => { ... }; (@field $(#[$attribute:meta])* ($($vis:tt)*) $t:ty, $from:ty, $into:ty, _, $setter:ident: $bit:expr) => { ... }; (@field $(#[$attribute:meta])* ($($vis:tt)*) $t:ty, $from:ty, $into:ty, $getter:ident, _: $msb:expr, $lsb:expr, $count:expr) => { ... }; (@field $(#[$attribute:meta])* ($($vis:tt)*) $t:ty, $from:ty, $into:ty, $getter:ident, _: $msb:expr, $lsb:expr) => { ... }; (@field $(#[$attribute:meta])* ($($vis:tt)*) $t:ty, $from:ty, $into:ty, $getter:ident, _: $bit:expr) => { ... }; (@field $(#[$attribute:meta])* ($($vis:tt)*) $t:ty, $from:ty, $into:ty, $getter:ident, $setter:ident: $($exprs:expr),*) => { ... }; ($t:ty;) => { ... }; ($default_ty:ty; pub $($rest:tt)*) => { ... }; ($default_ty:ty; #[$attribute:meta] $($rest:tt)*) => { ... }; ($default_ty:ty; ($(#[$attributes:meta])*) #[$attribute:meta] $($rest:tt)*) => { ... }; ($default_ty:ty; ($(#[$attribute:meta])*) pub $t:ty, from into $into:ty, $getter:tt, $setter:tt: $($exprs:expr),*; $($rest:tt)*) => { ... }; ($default_ty:ty; ($(#[$attribute:meta])*) pub $t:ty, into $into:ty, $getter:tt, $setter:tt: $($exprs:expr),*; $($rest:tt)*) => { ... }; ($default_ty:ty; ($(#[$attribute:meta])*) pub $t:ty, $getter:tt, $setter:tt: $($exprs:expr),*; $($rest:tt)*) => { ... }; ($default_ty:ty; ($(#[$attribute:meta])*) pub from into $into:ty, $getter:tt, $setter:tt: $($exprs:expr),*; $($rest:tt)*) => { ... }; ($default_ty:ty; ($(#[$attribute:meta])*) pub into $into:ty, $getter:tt, $setter:tt: $($exprs:expr),*; $($rest:tt)*) => { ... }; ($default_ty:ty; ($(#[$attribute:meta])*) pub $getter:tt, $setter:tt: $($exprs:expr),*; $($rest:tt)*) => { ... }; ($default_ty:ty; ($(#[$attribute:meta])*) $t:ty, from into $into:ty, $getter:tt, $setter:tt: $($exprs:expr),*; $($rest:tt)*) => { ... }; ($default_ty:ty; ($(#[$attribute:meta])*) $t:ty, into $into:ty, $getter:tt, $setter:tt: $($exprs:expr),*; $($rest:tt)*) => { ... }; ($default_ty:ty; ($(#[$attribute:meta])*) $t:ty, $getter:tt, $setter:tt: $($exprs:expr),*; $($rest:tt)*) => { ... }; ($default_ty:ty; ($(#[$attribute:meta])*) from into $into:ty, $getter:tt, $setter:tt: $($exprs:expr),*; $($rest:tt)*) => { ... }; ($default_ty:ty; ($(#[$attribute:meta])*) into $into:ty, $getter:tt, $setter:tt: $($exprs:expr),*; $($rest:tt)*) => { ... }; ($default_ty:ty; ($(#[$attribute:meta])*) $getter:tt, $setter:tt: $($exprs:expr),*; $($rest:tt)*) => { ... }; ($previous_default_ty:ty; $default_ty:ty; $($rest:tt)*) => { ... }; ($default_ty:ty; $($rest:tt)*) => { ... }; ($($rest:tt)*) => { ... }; }
Expand description
Declares the fields of struct.
This macro will generate the methods to access the fields of a bitfield. It must be called
from an impl
block for a type that implements the BitRange
and/or the Bit
traits
(which traits are required depending on what type of fields are used).
The syntax of this macro is composed of declarations ended by semicolons. There are two types of declarations: default type, and fields.
A default type is just a type followed by a semicolon. This will affect all the following field declarations.
A field declaration is composed of the following:
- Optional attributes (
#[...]
), documentation comments (///
) are attributes; - An optional pub keyword to make the methods public
- An optional type followed by a comma
- Optionally, the word
into
followed by a type, followed by a comma - The getter and setter idents, separated by a comma
- A colon
- One to three expressions of type
usize
The attributes and pub will be applied to the two methods generated.
If the into
part is used, the getter will convert the field after reading it.
The getter and setter idents can be _
to not generate one of the two. For example, if the
setter is _
, the field will be read-only.
The expressions at the end are the bit positions. Their meaning depends on the number of expressions:
- One expression: the field is a single bit. The type is ignored and
bool
is used. The traitBit
is used. - Two expressions:
msb, lsb
, the field is composed of the bits frommsb
tolsb
, included. - Three expressions:
msb, lsb, count
, the field is an array. The first element is composed of the bits frommsb
tolsb
. The following elements are consecutive bits range of the same size.
§Example
bitfield_fields!{
// The default type will be `u64
u64;
// filed1 is read-write, public, the methods are inline
#[inline]
pub field1, set_field1: 10, 0;
// `field2` is read-only, private, and of type bool.
field2, _ : 0;
// `field3` will be read as an `u32` and then converted to `FooBar`.
// The setter is not affected, it still need an `u32` value.
u32, into FooBar, field3, set_field3: 10, 0;
// `field4` will be read as an `u32` and then converted to `FooBar`.
// The setter will take a `FooBar`, and converted back to an `u32`.
u32, from into FooBar, field4, set_field4: 10, 0;
}