pub struct ExampleBitfield(/* private fields */);
trace_macros
only.Expand description
An example bitfield type.
This type was generated by the following bitfield!
macro invocation:
use crate::{
bitfield,
example::{AnotherTestEnum, TestEnum},
};
bitfield! {
/// An example bitfield type.
///
/// This type was generated by the following [`bitfield!`]
/// macro invocation:
/// ```
#[doc = include_str!("example_bitfield.rs")]
/// ```
#[derive(PartialEq, Eq, Hash)]
pub struct ExampleBitfield<u64> {
/// Six bits of arbitrary meaning.
pub const SOME_BITS = 6;
/// A bit flag.
///
/// This is `true` if foo is enabled. What that means is left
/// as an exercise to the reader.
pub const FOO_ENABLED: bool;
/// Another bit flag.
///
/// This is `true` if bar is enabled. What that means is left
/// as an exercise to the reader.
pub const BAR_ENABLED: bool;
/// These bits are reserved and should always be 0.
const _RESERVED_1 = 2;
/// An enum value
pub const TEST_ENUM: TestEnum;
const _RESERVED_BITS = 4;
/// Another enum.
pub const ANOTHER_ENUM: AnotherTestEnum;
/// An 8-bit signed integer value.
///
/// Who knows what this means.
pub const A_BYTE: i8;
/// `..` can be used to create a packing spec for all the remaining
/// bits in a bitfield.
pub const REST = ..;
}
}
Implementations§
Source§impl ExampleBitfield
impl ExampleBitfield
Sourcepub const FOO_ENABLED: Pack64<bool, Self>
pub const FOO_ENABLED: Pack64<bool, Self>
A bit flag.
This is true
if foo is enabled. What that means is left
as an exercise to the reader.
Sourcepub const BAR_ENABLED: Pack64<bool, Self>
pub const BAR_ENABLED: Pack64<bool, Self>
Another bit flag.
This is true
if bar is enabled. What that means is left
as an exercise to the reader.
Sourcepub const ANOTHER_ENUM: Pack64<AnotherTestEnum, Self>
pub const ANOTHER_ENUM: Pack64<AnotherTestEnum, Self>
Another enum.
Sourcepub const REST: Pack64<u64, Self>
pub const REST: Pack64<u64, Self>
..
can be used to create a packing spec for all the remaining
bits in a bitfield.
Sourcepub const fn from_bits(bits: u64) -> Self
pub const fn from_bits(bits: u64) -> Self
Constructs a new instance of Self
from the provided raw bits.
Sourcepub fn with<T>(self, field: Pack64<T, Self>, value: T) -> Self
pub fn with<T>(self, field: Pack64<T, Self>, value: T) -> Self
Packs the bit representation of value
into self
at the bit
range designated by field
, returning a new bitfield.
Sourcepub fn set<T>(&mut self, field: Pack64<T, Self>, value: T) -> &mut Self
pub fn set<T>(&mut self, field: Pack64<T, Self>, value: T) -> &mut Self
Packs the bit representation of value
into self
at the range
designated by field
, mutating self
in place.
Sourcepub fn get<T>(self, field: Pack64<T, Self>) -> T
pub fn get<T>(self, field: Pack64<T, Self>) -> T
Unpacks the bit range represented by field
from self
, and
converts it into a T
-typed value.
§Panics
This method panics if self
does not contain a valid bit
pattern for a T
-typed value, as determined by T
’s
FromBits::try_from_bits
implementation.
Sourcepub fn try_get<T>(self, field: Pack64<T, Self>) -> Result<T, T::Error>
pub fn try_get<T>(self, field: Pack64<T, Self>) -> Result<T, T::Error>
Unpacks the bit range represented by field
from self
and attempts to convert it into a T
-typed value.
§Returns
Ok(T)
if aT
-typed value could be constructed from the bits insrc
Err(T::Error)
ifsrc
does not contain a valid bit pattern for aT
-typed value, as determined byT
’s [FromBits::try_from_bits
implementation.
Sourcepub fn assert_valid()
pub fn assert_valid()
Asserts that all the packing specs for this type are valid.
This is intended to be used in unit tests.
Sourcepub fn display_ascii(&self) -> impl Display
pub fn display_ascii(&self) -> impl Display
Returns a value that formats this bitfield in a multi-line format, using only ASCII characters.
This is equivalent to formatting this bitfield using a {}
display specifier, but will never use Unicode box-drawing
characters, even when an upstream formatter uses the {:#}
fmt::Display
specifier. This is intended for use on platforms
where Unicode box drawing characters are never available.
Sourcepub fn display_unicode(&self) -> impl Display
pub fn display_unicode(&self) -> impl Display
Returns a value that formats this bitfield in a multi-line format, always using Unicode box-drawing characters.
This is equivalent to formatting this bitfield using a {:#}
format specifier, but will always use Unicode box-drawing
characters, even when an upstream formatter uses the {}
fmt::Display
specifier.
Trait Implementations§
Source§impl Binary for ExampleBitfield
impl Binary for ExampleBitfield
Source§impl Clone for ExampleBitfield
impl Clone for ExampleBitfield
Source§fn clone(&self) -> ExampleBitfield
fn clone(&self) -> ExampleBitfield
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more