Skip to main content

Crate tlbits

Crate tlbits 

Source
Expand description

§Binary TL-B de/serialization

docs.rs crates.io

§Example

Consider the following TL-B schema:

tag$10 query_id:uint64 amount:(VarUInteger 16) = Hello;

Let’s first define a struct Hello that holds these parameters:

struct Hello {
    pub query_id: u64,
    pub amount: BigUint,
}

§Serialization

To be able to serialize a type to BitWriter, we should implement BitPack on it:

impl BitPack for Hello {
    type Args = ();

    fn pack<W>(&self, writer: &mut W, _: Self::Args) -> Result<(), W::Error>
        where W: BitWriter + ?Sized,
    {
        writer
            // tag$10
            .pack_as::<_, NBits<2>>(0b10, ())?
            // query_id:uint64
            .pack(self.query_id, ())?
            // amount:(VarUInteger 16)
            .pack_as::<_, &VarInt<4>>(&self.amount, ())?;
        Ok(())
    }
}

writer.pack(Hello {
    query_id: 0,
    amount: 1_000u64.into(),
}, ())?;

§Deserialization

To be able to deserialize a type from BitReader, we should implement BitUnpack on it:

impl<'de> BitUnpack<'de> for Hello {
    type Args = ();

    fn unpack<R>(reader: &mut R, _: Self::Args) -> Result<Self, R::Error>
        where R: BitReader<'de> + ?Sized,
    {
        // tag$10
        let tag: u8 = reader.unpack_as::<_, NBits<2>>(())?;
        if tag != 0b10 {
            return Err(Error::custom(format!("unknown tag: {tag:#b}")));
        }
        Ok(Self {
            // query_id:uint64
            query_id: reader.unpack(())?,
            // amount:(VarUInteger 16)
            amount: reader.unpack_as::<_, VarInt<4>>(())?,
        })
    }
}

let hello: Hello = parser.unpack(())?;

Re-exports§

pub use bitvec;
pub use either;

Modules§

adapters
Adapters for BitReader/BitWriter
de
Binary deserialization for TL-B
integer
Collection of de/serialization helpers for integers
ser
Binary serialization for TL-B

Structs§

AsWrap
Helper to implement de/serialize trait for adapters
BorrowCow
DefaultArgs
Adapter to implement de/serialize with Default args.
DefaultOnNone
De/serialize Default on None values
FromInto
Serialize value by converting it to/from a proxy type with serialization support.
FromIntoRef
Serialize a reference value by converting it to/from a proxy type with serialization support.
NBits
De/serialize value from/into exactly N bits.
Remainder
Same
Adapter to convert from *As to regular de/serialization traits.
StringError
String-backed Error
TryFromInto
Serialize value by converting it to/from a proxy type with serialization support.
TryFromIntoRef
Serialize a reference value by converting it to/from a proxy type with serialization support.
Unary
Unary ~n adapter
VarInt
Adapter for Var[U]Integer n where n is constant.
VarLen
De/serialize value by prefixing its length with BITS-bit integer.
VarNBits
Adapter for Var[U]Integer (n * 8) where n is dynamic.
VarNBytes
Adapter for Var[U]Integer n where n is dynamic.

Traits§

Context
Adapter for providing context on Result
Error
De/serialization error
NoArgs