pub struct Format { /* private fields */ }
Expand description

A description of a format to encode/decode Bits to/from. The format basically defines the “store type” and “order type” that will be used to SCALE encode or decode some Bits. These concepts are the same as in bitvec, but essentially:

  • The StoreType defines the size of each chunk that’s written (eg u8, u16 etc).
  • The OrderType determines the order in which we write to the store type; ie do we write to the least significant bit first and work up, or write to the most significant byte first and work down.

Implementations

Define a new format by providing a store and order.

Example
use scale_bits::dynamic::{ Format, StoreType, OrderType };

let format = Format::new(StoreType::U8, OrderType::Lsb0);

Use metadata to obtain details about the format.

Given some number of bits, how many bytes, in total, would it take to encode that number of bits given the specified format.

Example
use scale_bits::{ Bits, bits, dynamic::{ Format, StoreType, OrderType } };

let bits = bits![1,0,1,1,0,1,0,1,0,0,1];
let format = Format::new(StoreType::U8, OrderType::Lsb0);

// Encode bits with a given format:
let mut out = vec![];
format.encode_bits_to(&bits, &mut out);

// `encoded_size()` returns the same length without needing to allocate/encode:
let expected_len = format.encoded_size(bits.len());
assert_eq!(out.len(), expected_len);

A convenience wrapper around Format::encode_bits_to.

Example
use scale_bits::{ Bits, bits, dynamic::{ Format, StoreType, OrderType } };

let bits = bits![1,0,1,1,0,1,0,1,0,0,1];
let format = Format::new(StoreType::U8, OrderType::Lsb0);

// SCALE encode bits into the chosen format:
let out = format.encode_bits(&bits);

Encode the provided Bits to the output in the given format.

Example
use scale_bits::{ Bits, bits, dynamic::{ Format, StoreType, OrderType } };

let bits = bits![1,0,1,1,0,1,0,1,0,0,1];
let format = Format::new(StoreType::U8, OrderType::Lsb0);

// SCALE encode bits into the chosen format:
let mut out = vec![];
format.encode_bits_to(&bits, &mut out);

Decode the provided bytes into Bits assuming the given format.

Example
use scale_bits::{ Bits, bits, dynamic::{ Format, StoreType, OrderType } };

let bits = bits![1,0,1,1,0,1,0,1,0,0,1];
let format = Format::new(StoreType::U8, OrderType::Lsb0);

// SCALE encode bits into the chosen format:
let mut out = vec![];
format.encode_bits_to(&bits, &mut out);

// Then, we can decode these back given the same format:
let new_bits = format.decode_bits_from(&mut &*out).unwrap();

assert_eq!(bits, new_bits);

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

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

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

Returns the argument unchanged.

Calls U::from(self).

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

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

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.