Struct scale_value::BitSequence
source · [−]pub struct BitSequence { /* 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) -> Bits
pub fn with_capacity(num_bits: usize) -> Bits
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
sourceimpl Decode for Bits
impl Decode for Bits
sourcefn decode<I>(input: &mut I) -> Result<Bits, Error>where
I: Input,
fn decode<I>(input: &mut I) -> Result<Bits, Error>where
I: Input,
sourcefn skip<I>(input: &mut I) -> Result<(), Error>where
I: Input,
fn skip<I>(input: &mut I) -> Result<(), Error>where
I: Input,
sourcefn encoded_fixed_size() -> Option<usize>
fn encoded_fixed_size() -> Option<usize>
sourceimpl<'de> Deserialize<'de> for Bits
impl<'de> Deserialize<'de> for Bits
sourcefn deserialize<D>(
deserializer: D
) -> Result<Bits, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D
) -> Result<Bits, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
sourceimpl Encode for Bits
impl Encode for Bits
sourcefn size_hint(&self) -> usize
fn size_hint(&self) -> usize
sourcefn encoded_size(&self) -> usize
fn encoded_size(&self) -> usize
sourcefn encode_to<T>(&self, dest: &mut T)where
T: Output + ?Sized,
fn encode_to<T>(&self, dest: &mut T)where
T: Output + ?Sized,
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,
sourceimpl From<Bits> for Value<()>
impl From<Bits> for Value<()>
sourcefn from(val: BitSequence) -> Self
fn from(val: BitSequence) -> Self
sourceimpl<T> From<Bits> for ValueDef<T>
impl<T> From<Bits> for ValueDef<T>
sourcefn from(val: BitSequence) -> Self
fn from(val: BitSequence) -> Self
sourceimpl FromIterator<bool> for Bits
impl FromIterator<bool> for Bits
sourceimpl IntoIterator for Bits
impl IntoIterator for Bits
type IntoIter = BitsIntoIter
type IntoIter = BitsIntoIter
sourceimpl Serialize for Bits
impl Serialize for Bits
sourcefn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
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
sourceimpl<T> DecodeLimit for Twhere
T: Decode,
impl<T> DecodeLimit for Twhere
T: Decode,
impl<T> FmtForward for T
impl<T> FmtForward for T
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
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 morefn 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 morefn 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,
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,
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.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,
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> Rwhere
Self: Deref<Target = T>,
T: 'a + ?Sized,
R: 'a,
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> Rwhere
Self: Deref<Target = T>,
T: 'a + ?Sized,
R: 'a,
self
, then passes self.deref()
into the pipe function.impl<T> Tap for T
impl<T> Tap for T
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 morefn 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 morefn 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 morefn 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 morefn 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 morefn 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 morefn 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.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 morefn 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 morefn 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 morefn 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 morefn 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