Expand description
§TL-B de/serialization
§Example
Consider the following TL-B schema:
tag$10 query_id:uint64 amount:(VarUInteger 16) payload:(Maybe ^Cell) = Hello;Let’s first define a struct Hello that holds these parameters:
struct Hello {
pub query_id: u64,
pub amount: BigUint,
pub payload: Option<Cell>,
}§Serialization
To be able to serialize a type to Cell, we should implement
CellSerialize on it:
impl CellSerialize for Hello {
type Args = ();
fn store(&self, builder: &mut CellBuilder, _: Self::Args) -> Result<(), CellBuilderError> {
builder
// tag$10
.pack_as::<_, NBits<2>>(0b10, ())?
// query_id:uint64
.pack(self.query_id, ())?
// amount:(VarUInteger 16)
.pack_as::<_, &VarInt<4>>(&self.amount, ())?
// payload:(Maybe ^Cell)
.store_as::<_, Option<Ref>>(self.payload.as_ref(), ())?;
Ok(())
}
}
// serialize value into cell
let hello = Hello {
query_id: 0,
amount: 1_000u64.into(),
payload: None,
};
let cell = hello.to_cell(())?;§Deserialization
To be able to deserialize a type from Cell, we should implement
CellDeserialize on it:
impl<'de> CellDeserialize<'de> for Hello {
type Args = ();
fn parse(parser: &mut CellParser<'de>, _: Self::Args) -> Result<Self, CellParserError<'de>> {
// tag$10
let tag: u8 = parser.unpack_as::<_, NBits<2>>(())?;
if tag != 0b10 {
return Err(Error::custom(format!("unknown tag: {tag:#b}")));
}
Ok(Self {
// query_id:uint64
query_id: parser.unpack(())?,
// amount:(VarUInteger 16)
amount: parser.unpack_as::<_, VarInt<4>>(())?,
// payload:(Maybe ^Cell)
payload: parser.parse_as::<_, Option<Ref<ParseFully>>>(())?,
})
}
}
let mut parser = cell.parser();
let hello: Hello = parser.parse(())?;Re-exports§
Modules§
- bin_
tree - Collection of bintree-like de/serializable data structures
- de
- Deserialization for TL-B
- hashmap
- Collection of hashmap-like de/serializable data structures
- ser
- Serialization for TL-B
Structs§
- AsWrap
- Helper to implement de/serialize trait for adapters
- BagOf
Cells - Bag Of Cells is used to de/serialize a set of cells from/into bytes.
- BagOf
Cells Args BitPack::ArgsforBagOfCells- Cell
- A Cell.
- Data
- Adapter to implement cell de/serialization from/into binary data.
- Default
Args - Adapter to implement de/serialize with
Defaultargs. - Default
OnNone - De/serialize
DefaultonNonevalues - Either
Inline OrRef - From
Into - Serialize value by converting it to/from a proxy type with serialization support.
- From
Into Ref - Serialize a reference value by converting it to/from a proxy type with serialization support.
- List
- Parse
Fully - Adapter to deserialize value and ensure that no more data and references left.
- Ref
- Adapter to de/serialize value from/into reference to the child cell.
- Same
- Adapter to convert from
*Asto regular de/serialization traits. - Snake
Data - From TEP-64:
- String
Error String-backedError- TryFrom
Into - Serialize value by converting it to/from a proxy type with serialization support.
- TryFrom
Into Ref - Serialize a reference value by converting it to/from a proxy type with serialization support.
Traits§
Type Aliases§
- BoC
- Alias to
BagOfCells - Text
- From TEP-64: