pub struct BitString<'a>(/* private fields */);Expand description
A BIT STRING of any length: its octets, the last one padded with zero bits, and how many bits it has.
The octets are borrowed from what was decoded, or owned, as those of Octets. A
string of 64 bits at most is held in the value itself, and never allocates.
use oxirush_aper::BitString;
// A key of 256 bits: every bit of its octets
let key = BitString::from([0x5a; 32]);
assert_eq!(key.len(), 256);
assert_eq!(key.as_octets().len(), 32);
// A transport layer address of 20 bits has three octets
let bits = BitString::new(vec![0xab, 0xcd, 0xe0], 20)?;
assert_eq!(bits.get(0), Some(true));
assert_eq!(bits.get(20), None);
// The bits after the last one are zero
assert!(BitString::new(vec![0xab, 0xcd, 0xef], 20).is_err());Implementations§
Source§impl<'a> BitString<'a>
impl<'a> BitString<'a>
Sourcepub fn new(octets: impl Into<Octets<'a>>, length: usize) -> Result<Self, Error>
pub fn new(octets: impl Into<Octets<'a>>, length: usize) -> Result<Self, Error>
The bit string of length bits that octets hold from their first bit.
§Errors
An error of the kind Argument for other octets than length bits take, and
for bits after the last one that are not zero.
Sourcepub fn from_octets(octets: impl Into<Octets<'a>>) -> Self
pub fn from_octets(octets: impl Into<Octets<'a>>) -> Self
The bit string that is every bit of octets.
Sourcepub fn as_octets(&self) -> &[u8] ⓘ
pub fn as_octets(&self) -> &[u8] ⓘ
The octets of the string, the last one padded with zero bits.
Sourcepub fn get(&self, position: usize) -> Option<bool>
pub fn get(&self, position: usize) -> Option<bool>
The bit at a position, the first one at 0; None after the last.
Sourcepub fn into_owned(self) -> BitString<'static>
pub fn into_owned(self) -> BitString<'static>
The same string, with octets that it owns, kept as long as it is needed:
BitString<'static> is the type to write in a struct. The octets are copied
when they were borrowed.
Trait Implementations§
impl Eq for BitString<'_>
Source§impl Ord for BitString<'_>
By the bits, a string before those that it starts.
impl Ord for BitString<'_>
By the bits, a string before those that it starts.