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§
source§impl 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);
Trait Implementations§
source§impl Decode for Bits
impl Decode for Bits
source§impl<'de> Deserialize<'de> for Bits
impl<'de> Deserialize<'de> for Bits
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
source§impl Encode for Bits
impl Encode for Bits
source§fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
source§fn encoded_size(&self) -> usize
fn encoded_size(&self) -> usize
source§fn encode_to<T>(&self, dest: &mut T)where
T: Output + ?Sized,
fn encode_to<T>(&self, dest: &mut T)where
T: Output + ?Sized,
source§fn 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,
source§impl FromIterator<bool> for Bits
impl FromIterator<bool> for Bits
source§impl IntoIterator for Bits
impl IntoIterator 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§
source§impl<T> DecodeLimit for Twhere
T: Decode,
impl<T> DecodeLimit for Twhere
T: Decode,
source§impl<T> FmtForward for T
impl<T> FmtForward for T
source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
self
, then passes self.as_ref()
into the pipe function.source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
source§impl<T> Tap for T
impl<T> Tap for T
source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
Borrow<B>
of a value. Read moresource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
BorrowMut<B>
of a value. Read moresource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
AsRef<R>
view of a value. Read moresource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
AsMut<R>
view of a value. Read moresource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
Deref::Target
of a value. Read moresource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
Deref::Target
of a value. Read moresource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds. Read moresource§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
.tap_borrow()
only in debug builds, and is erased in release
builds. Read moresource§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
.tap_borrow_mut()
only in debug builds, and is erased in release
builds. Read moresource§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
.tap_ref()
only in debug builds, and is erased in release
builds. Read moresource§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
.tap_ref_mut()
only in debug builds, and is erased in release
builds. Read more