Skip to main content

BitString

Struct BitString 

Source
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>

Source

pub const EMPTY: Self

No bit.

Source

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.

Source

pub fn from_octets(octets: impl Into<Octets<'a>>) -> Self

The bit string that is every bit of octets.

Source

pub fn len(&self) -> usize

How many bits the string has.

Source

pub fn is_empty(&self) -> bool

Whether the string has no bit.

Source

pub fn as_octets(&self) -> &[u8] ⓘ

The octets of the string, the last one padded with zero bits.

Source

pub fn get(&self, position: usize) -> Option<bool>

The bit at a position, the first one at 0; None after the last.

Source

pub fn to_bits(&self) -> Option<Bits>

The string as a number, when it has 64 bits at most.

Source

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§

Source§

impl<'a> Clone for BitString<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BitString<'_>

The octets in hexadecimal, and the length: abcde0/20.

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BitString<'_>

Source§

fn default() -> Self

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

impl Eq for BitString<'_>

Source§

impl<'a, const N: usize> From<&'a [u8; N]> for BitString<'a>

Source§

fn from(octets: &'a [u8; N]) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a [u8]> for BitString<'a>

Source§

fn from(octets: &'a [u8]) -> Self

Converts to this type from the input type.
Source§

impl From<Bits> for BitString<'_>

Source§

fn from(bits: Bits) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for BitString<'_>

Source§

fn from(octets: Vec<u8>) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize> From<[u8; N]> for BitString<'_>

Source§

fn from(octets: [u8; N]) -> Self

Converts to this type from the input type.
Source§

impl Hash for BitString<'_>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl Ord for BitString<'_>

By the bits, a string before those that it starts.

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for BitString<'_>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for BitString<'_>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

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

Auto Trait Implementations§

§

impl<'a> Freeze for BitString<'a>

§

impl<'a> RefUnwindSafe for BitString<'a>

§

impl<'a> Send for BitString<'a>

§

impl<'a> Sync for BitString<'a>

§

impl<'a> Unpin for BitString<'a>

§

impl<'a> UnsafeUnpin for BitString<'a>

§

impl<'a> UnwindSafe for BitString<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.