logo
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

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);

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());

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));

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]);

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);

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);

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());

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);

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

Converts this type into a mutable reference of the (usually inferred) input type.

Converts this type into a shared reference of the (usually inferred) input type.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Returns the “default value” for a type. Read more

Converts to this type from the input type.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.