Skip to content

Commit

Permalink
add CodecFlags methods from_[iter|slice]
Browse files Browse the repository at this point in the history
  • Loading branch information
joseluis committed Feb 22, 2025
1 parent ee73761 commit 795acd9
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/data/codec/encode/combinators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,28 @@ mod flags {
impl CodecFlags {
/// Creates a new [`CodecFlags`] combinator from 8 `bool`s.
pub const fn new(flags: [bool; 8]) -> Self { Self(flags) }
/// Creates a new [`CodecFlags`] from a slice of `bool`s.
///
/// Takes the first 8 `bool`s and fills missing with `false`.
pub fn from_slice(slice: &[bool]) -> Self {
let mut flags = [false; 8];
for (i, &b) in slice.iter().take(8).enumerate() { flags[i] = b; }
Self(flags)
}
/// Creates a new [`CodecFlags`] from a slice of arbitrary types.
///
/// The closure `f` is run for each element.
pub fn from_iter<T, I, F>(iter: I, mut f: F) -> Self
where
I: IntoIterator<Item = T>,
F: FnMut(T) -> bool,
{
let mut flags = [false; 8];
for (i, v) in iter.into_iter().take(8).enumerate() {
flags[i] = f(v);
}
Self(flags)
}
}
impl Debug for CodecFlags {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult<()> {
Expand Down

0 comments on commit 795acd9

Please sign in to comment.