Struct scale_bits::dynamic::Format
source · [−]pub struct Format { /* private fields */ }
Expand description
A description of a format to encode/decode Bits
to/from.
Implementations
sourceimpl Format
impl Format
sourcepub fn new(store: StoreType, order: OrderType) -> Self
pub fn new(store: StoreType, order: OrderType) -> Self
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);
sourcepub fn from_metadata(
ty: &TypeDefBitSequence<PortableForm>,
types: &PortableRegistry
) -> Result<Format, BitsDetailsError>
pub fn from_metadata(
ty: &TypeDefBitSequence<PortableForm>,
types: &PortableRegistry
) -> Result<Format, BitsDetailsError>
Use metadata to obtain details about the format.
sourcepub fn encoded_size(&self, number_of_bits: usize) -> usize
pub fn encoded_size(&self, number_of_bits: usize) -> usize
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);
sourcepub fn encode_bits_to(&self, bits: &Bits, out: &mut Vec<u8>)
pub fn encode_bits_to(&self, bits: &Bits, out: &mut Vec<u8>)
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);
sourcepub fn decode_bits_from(&self, bytes: &mut &[u8]) -> Result<Bits, CodecError>
pub fn decode_bits_from(&self, bytes: &mut &[u8]) -> Result<Bits, CodecError>
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
impl RefUnwindSafe for Format
impl Send for Format
impl Sync for Format
impl Unpin for Format
impl UnwindSafe for Format
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more