Struct scale_bits::bits::Bits
source · [−]pub struct Bits { /* private fields */ }
Expand description
This represents a sequence of boolean values, packed into bits.
One of the defining features of this type is that it SCALE encodes and decodes into an
identical representation to a BitVec<u8, Lsb0>
, and has matching a scale_info::TypeInfo
implementation to align with this. This allows it to be used in place of BitVec<u8, Lsb0>
when you need something with an identical SCALE representation and a simple API and don’t wish
to pull in the bitvec
crate.
In addition to this, we can use the crate::scale::Format
type to encode and decode Bits
in the same way as BitVec
’s do with order types of Lsb0
and Msb0
, and store types of
u8
, u16
, and u32
.
With the serde
feature enabled we can also serialize and seserialize Bits
from sequences
of booleans.
Example
use scale_bits::bits::Bits;
let mut bits = Bits::new();
bits.push(true);
bits.push(false);
bits.push(false);
assert_eq!(bits.len(), 3);
Converting to and from Vec<bool>
:
use scale_bits::bits::Bits;
let bools = vec![true, false, true, false, true];
let bits: Bits = bools.into_iter().collect();
let new_bools: Vec<bool> = bits.into_iter().collect();
assert_eq!(new_bools, vec![true, false, true, false, true]);
Implementations
sourceimpl Bits
impl Bits
sourcepub fn with_capacity(num_bits: usize) -> Self
pub fn with_capacity(num_bits: usize) -> Self
Create a new empty list of bits. Pre-allocates enough space for the number of bits provided here.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if no bits are stored.
Example
use scale_bits::bits::Bits;
let mut bits = Bits::new();
assert!(bits.is_empty());
bits.push(true);
assert!(!bits.is_empty());
bits.pop();
assert!(bits.is_empty());
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Return the number of bits stored.
Example
use scale_bits::bits::Bits;
let mut bits = Bits::new();
assert_eq!(bits.len(), 0);
bits.push(true);
bits.push(false);
bits.push(true);
assert_eq!(bits.len(), 3);
bits.pop();
bits.pop();
assert_eq!(bits.len(), 1);
sourcepub fn push(&mut self, b: bool)
pub fn push(&mut self, b: bool)
Push new bits to the end of the list.
Example
use scale_bits::{ bits::Bits, bits };
let mut bs = Bits::new();
bs.push(true);
bs.push(false);
bs.push(true);
assert_eq!(bs, bits![1,0,1]);
sourcepub fn pop(&mut self) -> Option<bool>
pub fn pop(&mut self) -> Option<bool>
Remove bits from the end of the list, returning them if they are present.
Example
use scale_bits::{ bits::Bits, bits };
let mut bs = bits![true, false, true, true];
assert_eq!(bs.pop(), Some(true));
assert_eq!(bs.pop(), Some(true));
assert_eq!(bs.pop(), Some(false));
assert_eq!(bs.pop(), Some(true));
assert_eq!(bs.pop(), None);
assert_eq!(bs.pop(), None);
sourcepub fn get(&self, idx: usize) -> Option<bool>
pub fn get(&self, idx: usize) -> Option<bool>
Retrieve a bit at a given index, returning None
if no bit exists
at that index.
Example
use scale_bits::bits;
let bs = bits![true, false, true, true];
assert_eq!(bs.get(0), Some(true));
assert_eq!(bs.get(1), Some(false));
assert_eq!(bs.get(2), Some(true));
assert_eq!(bs.get(3), Some(true));
assert_eq!(bs.get(4), None);
sourcepub fn iter(&self) -> BitsIter<'_>ⓘNotable traits for BitsIter<'a>impl<'a> Iterator for BitsIter<'a> type Item = bool;
pub fn iter(&self) -> BitsIter<'_>ⓘNotable traits for BitsIter<'a>impl<'a> Iterator for BitsIter<'a> type Item = bool;
Iterate over each bit in order, returning true
or false
for each.
Example
use scale_bits::bits;
let bs = bits![true, false, true, true];
let v: Vec<bool> = bs.iter().collect();
assert_eq!(v, vec![true, false, true, true]);