BitVec

Struct BitVec 

Source
pub struct BitVec(/* private fields */);
Expand description

Wrapper for Vec<Bit> used for implementing From trait.

Implementations§

Source§

impl BitVec

Source

pub fn filled_with(bit_val: u8, length: usize) -> BitVec

Constructs a BitVec of given length filled with bit_val.

§Arguments
  • bit_val - value that will be set to the Bit
  • length - length of the output vector
§Examples
§Create a BitVec filled with 8 zero bits
use ptero::binary::{Bit, BitVec};

let vec: Vec<Bit> = BitVec::filled_with(0, 8).into();
assert_eq!(vec, &[Bit(0), Bit(0), Bit(0), Bit(0), Bit(0), Bit(0), Bit(0), Bit(0)]);    

Trait Implementations§

Source§

impl Debug for BitVec

Source§

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

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

impl From<BitVec> for Vec<Bit>

Source§

fn from(bit_vec: BitVec) -> Self

Converts to this type from the input type.
Source§

impl From<BitVec> for u32

Source§

fn from(bit_vec: BitVec) -> Self

Conversion implementation for u32. Converts array of Bits to the corresponding number. The function expects that the first element is the most significant bit.

§Examples
§Convert 5 bits to number
use ptero::binary::{Bit, BitVec};

let array: BitVec = vec![1, 0, 1, 1, 1]
                        .iter()
                        .map(|v| Bit(*v))
                        .collect::<Vec<Bit>>()
                        .into();
let number: u32 = array.into();
assert_eq!(number, 23);
Source§

impl From<Vec<Bit>> for BitVec

Source§

fn from(bit_vec: Vec<Bit>) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for BitVec

Source§

fn from(number: u32) -> Self

Conversion implementation for u32. Converts u32 number to the vector of Bits. The result vector has the most significant bit at the beginning.

§Examples
§Convert the 65 number
use ptero::binary::{Bit, BitVec};

let array: BitVec = vec![1, 0, 1, 1, 1]
                        .iter()
                        .map(|v| Bit(*v))
                        .collect::<Vec<Bit>>()
                        .into();
let number: u32 = array.into();
assert_eq!(number, 23);
Source§

impl TryFrom<BitVec> for Vec<u8>

Source§

fn try_from(bit_vec: BitVec) -> Result<Vec<u8>, Self::Error>

Tries to convert array of Bits to the array of bytes. The function expects that each left most bit in byte-size boundary is the most significant bit.

§Arguments
  • bits - reference to array of bits &[Bit]
§Behavior

Function return BinaryConversionError when array is not padded to byte-size boundary i.e. length to divisible by 8.

§Examples
§Convert 16 bits to 2 bytes
use ptero::binary::{Bit, BitVec, BinaryConversionError};
use std::convert::TryFrom;

let array: BitVec = vec![0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1]
                            .iter()
                            .map(|v| Bit(*v))
                            .collect::<Vec<Bit>>()
                            .into();
let result: Result<Vec<u8>, BinaryConversionError> = TryFrom::try_from(array);
assert!(result.is_ok());
assert_eq!(result.unwrap(), vec![42, 129]);
§Return error if array is not in byte-size boundary
use ptero::binary::{Bit, BinaryConversionError, BitVec};
use std::convert::TryFrom;

let array: BitVec = vec![0, 0, 1]
                            .iter()
                            .map(|v| Bit(*v))
                            .collect::<Vec<Bit>>()
                            .into();
let result: Result<Vec<u8>, BinaryConversionError> = TryFrom::try_from(array);
assert!(!result.is_ok());
Source§

type Error = BinaryConversionError

The type returned in the event of a conversion error.

Auto Trait Implementations§

§

impl Freeze for BitVec

§

impl RefUnwindSafe for BitVec

§

impl Send for BitVec

§

impl Sync for BitVec

§

impl Unpin for BitVec

§

impl UnwindSafe for BitVec

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V