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§
source§impl 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§
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<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>,
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 EncodeAsType for Bits
impl EncodeAsType for Bits
source§fn encode_as_type_to(
&self,
type_id: u32,
types: &PortableRegistry,
out: &mut Vec<u8, Global>
) -> Result<(), Error>
fn encode_as_type_to( &self, type_id: u32, types: &PortableRegistry, out: &mut Vec<u8, Global> ) -> Result<(), Error>
type_id
, types
, a context
and some output target for the SCALE encoded bytes,
attempt to SCALE encode the current value into the type given by type_id
.source§fn encode_as_type(
&self,
type_id: u32,
types: &PortableRegistry
) -> Result<Vec<u8, Global>, Error>
fn encode_as_type( &self, type_id: u32, types: &PortableRegistry ) -> Result<Vec<u8, Global>, Error>
EncodeAsType::encode_as_type_to
. Prefer to
implement that instead.source§impl From<Bits> for Value<()>
impl From<Bits> for Value<()>
source§fn from(val: BitSequence) -> Self
fn from(val: BitSequence) -> Self
source§impl<T> From<Bits> for ValueDef<T>
impl<T> From<Bits> for ValueDef<T>
source§fn from(val: BitSequence) -> Self
fn from(val: BitSequence) -> Self
source§impl FromIterator<bool> for Bits
impl FromIterator<bool> for Bits
source§impl IntoIterator for Bits
impl IntoIterator for Bits
source§impl IntoVisitor for Bitswhere
BasicVisitor<Bits>: for<'info, 'scale> Visitor<Error = Error, Value<'scale, 'info> = Bits>,
impl IntoVisitor for Bitswhere BasicVisitor<Bits>: for<'info, 'scale> Visitor<Error = Error, Value<'scale, 'info> = Bits>,
source§fn into_visitor() -> <Bits as IntoVisitor>::Visitor
fn into_visitor() -> <Bits as IntoVisitor>::Visitor
source§impl PartialEq<Bits> for Bits
impl PartialEq<Bits> for Bits
source§impl Serialize for Bits
impl Serialize for Bits
source§fn 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§
§impl<T> Conv for T
impl<T> Conv for T
source§impl<T> DecodeAsType for Twhere
T: IntoVisitor,
Error: From<<<T as IntoVisitor>::Visitor as Visitor>::Error>,
impl<T> DecodeAsType for Twhere T: IntoVisitor, Error: From<<<T as IntoVisitor>::Visitor as Visitor>::Error>,
source§fn decode_as_type(
input: &mut &[u8],
type_id: u32,
types: &PortableRegistry
) -> Result<T, Error>
fn decode_as_type( input: &mut &[u8], type_id: u32, types: &PortableRegistry ) -> Result<T, Error>
type_id
, and type registry, attempt to decode said bytes into
Self
. Implementations should modify the &mut
reference to the bytes such that any bytes
not used in the course of decoding are still pointed to after decoding is complete.source§impl<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,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§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.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§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) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere Self: Sized,
§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 more§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 more§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,
§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,
self
, then passes self.as_mut()
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 more§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 more§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 more§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 more§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 more§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 more§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.§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.§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.§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.§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.§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.