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

source

pub fn new() -> Bits

Create a new empty list of bits. This does not allocate.

source

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.

source

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());
source

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);
source

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]);
source

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);
source

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);
source

pub fn iter(&self) -> BitsIter<'_>

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]);
source

pub fn to_vec(self) -> Vec<bool, Global>

Convert our bits into a Vec<bool>.

Example
use scale_bits::bits;

let bs = bits![true, false, true, true].to_vec();
assert_eq!(bs, vec![true, false, true, true]);

Trait Implementations§

source§

impl Clone for Bits

source§

fn clone(&self) -> Bits

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Bits

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Decode for Bits

source§

fn decode<I>(input: &mut I) -> Result<Bits, Error>where I: Input,

Attempt to deserialise the value from input.
source§

fn skip<I>(input: &mut I) -> Result<(), Error>where I: Input,

Attempt to skip the encoded value from input. Read more
source§

fn encoded_fixed_size() -> Option<usize>

Returns the fixed encoded size of the type. Read more
source§

impl Default for Bits

source§

fn default() -> Bits

Returns the “default value” for a type. Read more
source§

impl<'de> Deserialize<'de> for Bits

source§

fn deserialize<D>( deserializer: D ) -> Result<Bits, <D as Deserializer<'de>>::Error>where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Encode for Bits

source§

fn size_hint(&self) -> usize

If possible give a hint of expected size of the encoding. Read more
source§

fn encode(&self) -> Vec<u8, Global>

Convert self to an owned vector.
source§

fn encoded_size(&self) -> usize

Calculates the encoded size. Read more
source§

fn encode_to<T>(&self, dest: &mut T)where T: Output + ?Sized,

Convert self to a slice and append it to the destination.
source§

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.
source§

impl EncodeAsType for Bits

source§

fn encode_as_type_to( &self, type_id: u32, types: &PortableRegistry, out: &mut Vec<u8, Global> ) -> Result<(), Error>

Given some 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>

This is a helper function which internally calls EncodeAsType::encode_as_type_to. Prefer to implement that instead.
source§

impl From<Bits> for Value<()>

source§

fn from(val: BitSequence) -> Self

Converts to this type from the input type.
source§

impl<T> From<Bits> for ValueDef<T>

source§

fn from(val: BitSequence) -> Self

Converts to this type from the input type.
source§

impl FromIterator<bool> for Bits

source§

fn from_iter<T>(iter: T) -> Bitswhere T: IntoIterator<Item = bool>,

Creates a value from an iterator. Read more
source§

impl IntoIterator for Bits

§

type Item = bool

The type of the elements being iterated over.
§

type IntoIter = BitsIntoIter

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> <Bits as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
source§

impl IntoVisitor for Bitswhere BasicVisitor<Bits>: for<'info, 'scale> Visitor<Error = Error, Value<'scale, 'info> = Bits>,

§

type Visitor = BasicVisitor<Bits>

The visitor type used to decode SCALE encoded bytes to Self.
source§

fn into_visitor() -> <Bits as IntoVisitor>::Visitor

A means of obtaining this visitor.
source§

impl PartialEq<Bits> for Bits

source§

fn eq(&self, other: &Bits) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for Bits

source§

fn serialize<S>( &self, serializer: S ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl TypeInfo for Bits

§

type Identity = Bits

The type identifying for which type info is provided. Read more
source§

fn type_info() -> Type<MetaForm>

Returns the static type identifier for Self.
source§

impl Eq for Bits

source§

impl StructuralEq for Bits

source§

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Conv for T

§

fn conv<T>(self) -> Twhere Self: Into<T>,

Converts self into T using Into<T>. Read more
source§

impl<T> DecodeAll for Twhere T: Decode,

source§

fn decode_all(input: &mut &[u8]) -> Result<T, Error>

Decode Self and consume all of the given input data. Read more
source§

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>

Given some input bytes, a 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,

source§

fn decode_all_with_depth_limit(limit: u32, input: &mut &[u8]) -> Result<T, Error>

Decode Self and consume all of the given input data. Read more
source§

fn decode_with_depth_limit<I>(limit: u32, input: &mut I) -> Result<T, Error>where I: Input,

Decode Self with the given maximum recursion depth and advance input by the number of bytes consumed. Read more
§

impl<T> FmtForward for T

§

fn fmt_binary(self) -> FmtBinary<Self>where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
§

fn fmt_display(self) -> FmtDisplay<Self>where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
§

fn fmt_octal(self) -> FmtOctal<Self>where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
§

fn fmt_pointer(self) -> FmtPointer<Self>where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
§

fn fmt_list(self) -> FmtList<Self>where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> KeyedVec for Twhere T: Codec,

source§

fn to_keyed_vec(&self, prepend_key: &[u8]) -> Vec<u8, Global>

Return an encoding of Self prepended by given slice.
§

impl<T> Pipe for Twhere T: ?Sized,

§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere R: 'a,

Borrows 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,

Mutably borrows 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,

Borrows self, then passes self.borrow() into the pipe function. Read more
§

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,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows 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,

Mutably borrows self, then passes self.as_mut() into the pipe function.
§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> Rwhere Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
§

fn pipe_deref_mut<'a, T, R>(&'a mut self, func: impl FnOnce(&'a mut T) -> R) -> Rwhere Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
§

impl<T> Tap for T

§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,

Immutable access to the 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,

Mutable access to the AsMut<R> view of a value. Read more
§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere Self: Deref<Target = T>, T: ?Sized,

Immutable access to the 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,

Mutable access to the Deref::Target of a value. Read more
§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .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,

Calls .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,

Calls .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,

Calls .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,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Selfwhere Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T> TryConv for T

§

fn try_conv<T>(self) -> Result<T, Self::Error>where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<S> Codec for Swhere S: Decode + Encode,

source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,

source§

impl<T> EncodeLike<&&T> for Twhere T: Encode,

source§

impl<T> EncodeLike<&T> for Twhere T: Encode,

source§

impl<T> EncodeLike<&mut T> for Twhere T: Encode,

source§

impl<T> EncodeLike<Arc<T>> for Twhere T: Encode,

source§

impl<T> EncodeLike<Box<T, Global>> for Twhere T: Encode,

source§

impl<'a, T> EncodeLike<Cow<'a, T>> for Twhere T: ToOwned + Encode,

source§

impl<T> EncodeLike<Rc<T>> for Twhere T: Encode,

source§

impl<T> StaticTypeInfo for Twhere T: TypeInfo + 'static,