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§
source§impl 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] ⓘ
pub fn as_bytes(&self) -> &[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] ⓘ
pub fn as_bytes_mut(&mut self) -> &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§
source§impl Ord for Bitfield
impl Ord for Bitfield
source§impl PartialEq for Bitfield
impl PartialEq for Bitfield
source§impl PartialOrd for Bitfield
impl PartialOrd for Bitfield
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moreimpl Eq for Bitfield
impl StructuralPartialEq for Bitfield
Auto Trait Implementations§
impl Freeze for Bitfield
impl RefUnwindSafe for Bitfield
impl Send for Bitfield
impl Sync for Bitfield
impl Unpin for Bitfield
impl UnwindSafe for Bitfield
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)