Struct scale_bits::Bits
source · [−]pub struct Bits { /* private fields */ }
Expand description
This represents a sequence of bits (normally expressed as a sequence of boolean values).
Internally it packs these bits into u8
s to minimise the space needed to store them.
One of the defining features of this type is that it SCALE encodes and decodes into an
identical representation to a BitVec<Lsb0, u8>
, and has matching a scale_info::TypeInfo
implementation to align with this. This allows it to be used in place of BitVec<Lsb0, u8>
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 dynamically decode a range of TypeDefBitSequence
’s into Bits
using the [crate::DynamicBits
] type, and with the serde
feature enabled it can be
serialized and deserialized from an array of booleans.
Example
use scale_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;
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;
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;
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 };
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 };
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<'a>(&'a self) -> impl Iterator<Item = bool> + 'a
pub fn iter<'a>(&'a self) -> impl Iterator<Item = bool> + 'a
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]);
sourcepub fn into_iter(self) -> impl Iterator<Item = bool> + 'static
pub fn into_iter(self) -> impl Iterator<Item = bool> + 'static
Iterate over each bit in order, returning true
or false
for each.
This method consumes self
.
Example
use scale_bits::bits;
let bs = bits![true, false, true, true];
let v: Vec<bool> = bs.into_iter().collect();
assert_eq!(v, vec![true, false, true, true]);
Trait Implementations
sourceimpl Decode for Bits
impl Decode for Bits
sourcefn decode<I: Input>(input: &mut I) -> Result<Self, Error>
fn decode<I: Input>(input: &mut I) -> Result<Self, Error>
Attempt to deserialise the value from input.
sourcefn skip<I>(input: &mut I) -> Result<(), Error>where
I: Input,
fn skip<I>(input: &mut I) -> Result<(), Error>where
I: Input,
Attempt to skip the encoded value from input. Read more
sourcefn encoded_fixed_size() -> Option<usize>
fn encoded_fixed_size() -> Option<usize>
Returns the fixed encoded size of the type. Read more
sourceimpl Encode for Bits
impl Encode for Bits
sourcefn size_hint(&self) -> usize
fn size_hint(&self) -> usize
If possible give a hint of expected size of the encoding. Read more
sourcefn encoded_size(&self) -> usize
fn encoded_size(&self) -> usize
Calculates the encoded size. Read more
sourcefn encode_to<T>(&self, dest: &mut T)where
T: Output + ?Sized,
fn encode_to<T>(&self, dest: &mut T)where
T: Output + ?Sized,
Convert self to a slice and append it to the destination.
sourcefn using_encoded<R, F>(&self, f: F) -> Rwhere
F: FnOnce(&[u8]) -> R,
fn using_encoded<R, F>(&self, f: F) -> Rwhere
F: FnOnce(&[u8]) -> R,
Convert self to a slice and then invoke the given closure with it.
sourceimpl FromIterator<bool> for Bits
impl FromIterator<bool> for Bits
sourcefn from_iter<T: IntoIterator<Item = bool>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = bool>>(iter: T) -> Self
Creates a value from an iterator. Read more
sourceimpl PartialEq<Bits> for Bits
impl PartialEq<Bits> for Bits
impl Eq for Bits
impl StructuralEq for Bits
impl StructuralPartialEq for Bits
Auto Trait Implementations
impl RefUnwindSafe for Bits
impl Send for Bits
impl Sync for Bits
impl Unpin for Bits
impl UnwindSafe for Bits
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more