tl-proto

A collection of traits for working with TL serialization/deserialization.
Example
/* my_proto.tl */
int ? = Int;
long ? = Long;
string ? = String;
bytes data:string = Bytes;
int256 8*[ int ] = Int256;
pub.ed25519 key:int256 = PublicKey;
pub.aes key:int256 = PublicKey;
pub.overlay name:bytes = PublicKey;
adnl.address.udp ip:int port:int = adnl.Address;
tonNode.blockId workchain:int shard:long seqno:int = tonNode.BlockId;
liteServer.lookupBlock mode:# id:tonNode.blockId lt:mode.1?long utime:mode.2?int = liteServer.BlockHeader;
NOTE: TL scheme is parsed by
tl-schemecrate at compile time. It doesn't cover full TL grammer, but it's enough for most of the cases.
use ;
/// You can declare "bare" structs, which
/// doesn't have an associated TL id.
///
/// NOTE: enums can only be used as bare
/// with TlWrite, because there is no way to
/// know the exact variant without an id to
/// implement TlRead.
;
/// Or you can declare "boxed" structs, which
/// have one or more associated TL ids.
///
/// NOTE: in case of boxed enum with provided scheme,
/// all variants must have the same constructor kind
/// (all functions or all types). And if all variants
/// are types, they must refer to the same boxed type.
/// `with` is similar to the same attribute in serde
/// You can also declare "bare" structs and specify
/// the type id of their boxed variant, so something
/// like `tl_proto::BoxedWrapper` can be used later.
///
/// See also:
/// - `tl_proto::deserialize_as_boxed` - read bare type as boxed
/// - `tl_proto::serialize_as_boxed` - write bare type as boxed
/// - `tl_proto::hash_as_boxed` - compute hash of the boxed repr
/// There s a way to have a struct with optional fields
/// You can constraint the type by its representation
/// (`tl_proto::Bare` / `tl_proto::Boxed`)
Specification
| Type | Pseudocode |
|---|---|
() |
[] |
i32,u32,i64,u64 |
little_endian(x) |
true |
[0xb5, 0x75, 0x72, 0x99] |
false |
[0x37, 0x97, 0x79, 0xbc] |
[u8; N], N % 4 ≡ 0) |
[…x] |
Vec<u8>, len < 254) |
[len as u8, …x, …padding_to_4(len)] |
Vec<u8>, len ≥ 254) |
[254, …little_endian(x)[0..=2], …x, …padding_to_4(len)] |
Vec<T> |
[…little_endian(len as u32), …map(…x, repr)] |
(T0, … , Tn) |
[…repr(T0), … , …repr(Tn)] |
Option<T> |
{ Some(x) ⇒ repr(x), None ⇒ [] } |
enum { T0, …, Tn } |
{ T0(x) ⇒ […id(T0), …repr(x)], …, Tn(x) ⇒ […id(Tn), …repr(x)] } |