Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(bitfield): document generated methods #173

Merged
merged 1 commit into from
May 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions bitfield/src/bitfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,44 +227,71 @@ macro_rules! bitfield {
(stringify!($Field), Self::$Field.typed())
),+];

/// Constructs a new instance of `Self` from the provided raw bits.
$vis const fn from_bits(bits: $T) -> Self {
Self(bits)
}

/// Constructs a new instance of `Self` with all bits set to 0.
$vis const fn new() -> Self {
Self(0)
}

$vis fn with<T>(self, packer: $crate::bitfield! { @t $T, T }, value: T) -> Self
/// Packs the bit representation of `value` into `self` at the bit
/// range designated by `field`, returning a new bitfield.
$vis fn with<T>(self, field: $crate::bitfield! { @t $T, T }, value: T) -> Self
where
T: $crate::FromBits<$T>,
{
Self(packer.pack(value, self.0))
Self(field.pack(value, self.0))
}

$vis fn set<T>(&mut self, packer: $crate::bitfield! { @t $T, T }, value: T) -> &mut Self

/// Packs the bit representation of `value` into `self` at the range
/// designated by `field`, mutating `self` in place.
$vis fn set<T>(&mut self, field: $crate::bitfield! { @t $T, T }, value: T) -> &mut Self
where
T: $crate::FromBits<$T>,
{
packer.pack_into(value, &mut self.0);
field.pack_into(value, &mut self.0);
self
}

$vis fn get<T>(self, packer: $crate::bitfield! { @t $T, T }) -> 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
/// [`mycelium_bitfield::FromBits::try_from_bits`] implementation.
$vis fn get<T>(self, field: $crate::bitfield! { @t $T, T }) -> T
where
T: $crate::FromBits<$T>,
{
packer.unpack(self.0)
field.unpack(self.0)
}

$vis fn try_get<T>(self, packer: $crate::bitfield! { @t $T, T }) -> 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 a `T`-typed value could be constructed from the
/// bits in `src`
/// - `Err(T::Error)` if `src` does not contain a valid bit
/// pattern for a `T`-typed value, as determined by `T`'s
/// [`mycelium_bitfield::FromBits::try_from_bits`] implementation.
$vis fn try_get<T>(self, field: $crate::bitfield! { @t $T, T }) -> Result<T, T::Error>
where
T: $crate::FromBits<$T>,
{
packer.try_unpack(self.0)
field.try_unpack(self.0)
}


/// Asserts that all the packing specs for this type are valid.
///
/// This is intended to be used in unit tests.
$vis fn assert_valid() {
<$crate::bitfield! { @t $T, $T }>::assert_all_valid(&Self::FIELDS);
}
Expand Down