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§

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

Create a new empty list of bits. Pre-allocates enough space for the number of bits provided here.

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

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

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

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

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

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

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§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Attempt to deserialise the value from input.
Attempt to skip the encoded value from input. Read more
Returns the fixed encoded size of the type. Read more
Returns the “default value” for a type. Read more
Deserialize this value from the given Serde deserializer. Read more
If possible give a hint of expected size of the encoding. Read more
Convert self to an owned vector.
Calculates the encoded size. Read more
Convert self to a slice and append it to the destination.
Convert self to a slice and then invoke the given closure with it.
Creates a value from an iterator. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
Serialize this value into the given Serde serializer. Read more
The type identifying for which type info is provided. Read more
Returns the static type identifier for Self.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Converts self into T using Into<T>. Read more
Decode Self and consume all of the given input data. Read more
Decode Self and consume all of the given input data. Read more
Decode Self with the given maximum recursion depth and advance input by the number of bytes consumed. Read more
Causes self to use its Binary implementation when Debug-formatted.
Causes self to use its Display implementation when Debug-formatted. Read more
Causes self to use its LowerExp implementation when Debug-formatted. Read more
Causes self to use its LowerHex implementation when Debug-formatted. Read more
Causes self to use its Octal implementation when Debug-formatted.
Causes self to use its Pointer implementation when Debug-formatted. Read more
Causes self to use its UpperExp implementation when Debug-formatted. Read more
Causes self to use its UpperHex implementation when Debug-formatted. Read more
Formats each item in a sequence. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Return an encoding of Self prepended by given slice.
Pipes by value. This is generally the method you want to use. Read more
Borrows self and passes that borrow into the pipe function. Read more
Mutably borrows self and passes that borrow into the pipe function. Read more
Borrows self, then passes self.borrow() into the pipe function. Read more
Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
Borrows self, then passes self.as_ref() into the pipe function.
Mutably borrows self, then passes self.as_mut() into the pipe function. Read more
Borrows self, then passes self.deref() into the pipe function.
Mutably borrows self, then passes self.deref_mut() into the pipe function. Read more
Immutable access to a value. Read more
Mutable access to a value. Read more
Immutable access to the Borrow<B> of a value. Read more
Mutable access to the BorrowMut<B> of a value. Read more
Immutable access to the AsRef<R> view of a value. Read more
Mutable access to the AsMut<R> view of a value. Read more
Immutable access to the Deref::Target of a value. Read more
Mutable access to the Deref::Target of a value. Read more
Calls .tap() only in debug builds, and is erased in release builds.
Calls .tap_mut() only in debug builds, and is erased in release builds. Read more
Calls .tap_borrow() only in debug builds, and is erased in release builds. Read more
Calls .tap_borrow_mut() only in debug builds, and is erased in release builds. Read more
Calls .tap_ref() only in debug builds, and is erased in release builds. Read more
Calls .tap_ref_mut() only in debug builds, and is erased in release builds. Read more
Calls .tap_deref() only in debug builds, and is erased in release builds. Read more
Calls .tap_deref_mut() only in debug builds, and is erased in release builds. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Attempts to convert self into T using TryInto<T>. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.