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

A description of a format to encode/decode Bits to/from.

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

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

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