Struct 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

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>

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 duplicate 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 decode_into<I>( input: &mut I, dst: &mut MaybeUninit<Self>, ) -> Result<DecodeFinished, Error>
where I: Input,

Attempt to deserialize the value from input into a pre-allocated piece of memory. Read more
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>

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) -> R
where 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<R>( &self, type_id: <R as TypeResolver>::TypeId, types: &R, out: &mut Vec<u8>, ) -> Result<(), Error>
where R: TypeResolver,

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<R>( &self, type_id: <R as TypeResolver>::TypeId, types: &R, ) -> Result<Vec<u8>, Error>
where R: TypeResolver,

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) -> Bits
where T: IntoIterator<Item = bool>,

Creates a value from an iterator. Read more
Source§

impl IntoIterator for Bits

Source§

type Item = bool

The type of the elements being iterated over.
Source§

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 Bits

Source§

type AnyVisitor<R: TypeResolver> = BasicVisitor<Bits, R>

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

fn into_visitor<R>() -> <Bits as IntoVisitor>::AnyVisitor<R>
where R: TypeResolver,

A means of obtaining this visitor.
Source§

impl PartialEq for Bits

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

Source§

type Identity = Bits

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

fn type_info() -> Type

Returns the static type identifier for Self.
Source§

impl Eq for Bits

Source§

impl StructuralPartialEq for Bits

Auto Trait Implementations§

§

impl Freeze for Bits

§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DecodeAll for T
where 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 T
where T: IntoVisitor,

Source§

fn decode_as_type_maybe_compact<R>( input: &mut &[u8], type_id: <R as TypeResolver>::TypeId, types: &R, is_compact: bool, ) -> Result<T, Error>
where R: TypeResolver,

Source§

fn decode_as_type<R>( input: &mut &[u8], type_id: <R as TypeResolver>::TypeId, types: &R, ) -> Result<Self, Error>
where R: TypeResolver,

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 T
where 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
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> KeyedVec for T
where T: Codec,

Source§

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

Return an encoding of Self prepended by given slice.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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
Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

impl<T> EncodeLike<Box<T>> for T
where T: Encode,

Source§

impl<T> EncodeLike<Cow<'_, T>> for T
where T: ToOwned + Encode,

Source§

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

Source§

impl<T> JsonSchemaMaybe for T

Source§

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

Source§

impl<T> TypeId for T
where T: Clone + Debug + Default,