Struct sequoia_openpgp::types::Bitfield
source · [−]pub struct Bitfield { /* private fields */ }
Expand description
A variable-sized set of boolean flags.
This encodes flags in signature subpackets such as Features
and KeyFlags
. The Bitfield
grows to accommodate all bits
that are set, and querying a bit outside the allocated space will
return false
. Note that it will not automatically shrink if
clearing a bit would leave trailing bytes to be zero. To do that,
explicitly call Bitfield::canonicalize
.
Implementations
sourceimpl Bitfield
impl Bitfield
sourcepub fn iter_set(&self) -> impl Iterator<Item = usize> + Send + Sync + '_
pub fn iter_set(&self) -> impl Iterator<Item = usize> + Send + Sync + '_
Returns all bits that are set starting from bit 0, the least-significant bit in the left-most byte.
Examples
let f = Bitfield::from(vec![0b0000_0001, 0b0000_0010]);
let mut i = f.iter_set();
assert_eq!(i.next(), Some(0));
assert_eq!(i.next(), Some(9));
assert_eq!(i.next(), None);
sourcepub fn padding_bytes(&self) -> Option<NonZeroUsize>
pub fn padding_bytes(&self) -> Option<NonZeroUsize>
Returns the number of trailing zero bytes.
Examples
let mut f = Bitfield::from(vec![0b0000_0001]);
assert!(f.padding_bytes().is_none());
f.clear(0);
assert_eq!(f.padding_bytes().unwrap().get(), 1);
f.canonicalize();
assert!(f.padding_bytes().is_none());
sourcepub fn normalized_eq(&self, other: &Self) -> bool
pub fn normalized_eq(&self, other: &Self) -> bool
Compares two feature sets for semantic equality.
Returns true if both sets have the same flags set, i.e. this function ignores any trailing zero bytes.
Examples
let f = Bitfield::from(vec![0b0000_0001]);
let g = Bitfield::from(vec![0b0000_0001, 0b0000_0000]);
assert!(f != g);
assert!(f.normalized_eq(&g));
sourcepub fn as_bytes(&self) -> &[u8]ⓘNotable traits for &[u8]impl<'_> Read for &[u8]impl<'_> Write for &mut [u8]
pub fn as_bytes(&self) -> &[u8]ⓘNotable traits for &[u8]impl<'_> Read for &[u8]impl<'_> Write for &mut [u8]
Returns a slice containing the raw values.
Examples
let mut f = Bitfield::default();
assert_eq!(f.as_bytes(), &[]);
f.set(0);
assert_eq!(f.as_bytes(), &[0b0000_0001]);
sourcepub fn as_bytes_mut(&mut self) -> &mut [u8]ⓘNotable traits for &[u8]impl<'_> Read for &[u8]impl<'_> Write for &mut [u8]
pub fn as_bytes_mut(&mut self) -> &mut [u8]ⓘNotable traits for &[u8]impl<'_> Read for &[u8]impl<'_> Write for &mut [u8]
Returns a mutable slice containing the raw values.
Examples
let mut f = Bitfield::from(vec![0b0000_0000]);
assert_eq!(f.get(0), false);
f.as_bytes_mut()[0] = 0b0000_0001;
assert_eq!(f.get(0), true);
sourcepub fn get(&self, bit: usize) -> bool
pub fn get(&self, bit: usize) -> bool
Returns whether the specified flag is set.
Examples
let f = Bitfield::default();
assert_eq!(f.get(0), false);
assert_eq!(f.get(23), false);
let f = Bitfield::from(vec![0b0000_0001]);
assert_eq!(f.get(0), true);
sourcepub fn canonicalize(&mut self)
pub fn canonicalize(&mut self)
Canonicalize by removing any trailing zero bytes.
Examples
let mut f = Bitfield::from(vec![0b0000_0001]);
assert!(f.padding_bytes().is_none());
f.clear(0);
assert_eq!(f.padding_bytes().unwrap().get(), 1);
f.canonicalize();
assert!(f.padding_bytes().is_none());
sourcepub fn set(&mut self, bit: usize)
pub fn set(&mut self, bit: usize)
Sets the specified flag.
Examples
let mut f = Bitfield::default();
assert_eq!(f.get(0), false);
f.set(0);
assert_eq!(f.get(0), true);
sourcepub fn clear(&mut self, bit: usize)
pub fn clear(&mut self, bit: usize)
Clears the specified flag.
Note: This does not implicitly canonicalize the bit field. To
do that, invoke Bitfield::canonicalize
.
Examples
let mut f = Bitfield::from(vec![0b0000_0001]);
assert_eq!(f.get(0), true);
f.clear(0);
assert_eq!(f.get(0), false);
assert_eq!(f.padding_bytes().unwrap().get(), 1);
Trait Implementations
sourceimpl Ord for Bitfield
impl Ord for Bitfield
sourceimpl PartialOrd<Bitfield> for Bitfield
impl PartialOrd<Bitfield> for Bitfield
sourcefn partial_cmp(&self, other: &Bitfield) -> Option<Ordering>
fn partial_cmp(&self, other: &Bitfield) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl Eq for Bitfield
impl StructuralEq for Bitfield
impl StructuralPartialEq for Bitfield
Auto Trait Implementations
impl RefUnwindSafe for Bitfield
impl Send for Bitfield
impl Sync for Bitfield
impl Unpin for Bitfield
impl UnwindSafe for Bitfield
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more