pub struct BitVec(/* private fields */);
Expand description
Wrapper for Vec<Bit>
used for implementing From
trait.
Implementations§
Source§impl BitVec
impl BitVec
Sourcepub fn filled_with(bit_val: u8, length: usize) -> BitVec
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 Bitlength
- 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 From<BitVec> for u32
impl From<BitVec> for u32
Source§fn from(bit_vec: BitVec) -> Self
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<u32> for BitVec
impl From<u32> for BitVec
Source§fn from(number: u32) -> Self
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>
impl TryFrom<BitVec> for Vec<u8>
Source§fn try_from(bit_vec: BitVec) -> Result<Vec<u8>, Self::Error>
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
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> 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
Mutably borrows from an owned value. Read more