Enum borsh::schema::Definition
source · pub enum Definition {
Primitive(u8),
Sequence {
length_width: u8,
length_range: RangeInclusive<u64>,
elements: Declaration,
},
Tuple {
elements: Vec<Declaration>,
},
Enum {
tag_width: u8,
variants: Vec<(DiscriminantValue, VariantName, Declaration)>,
},
Struct {
fields: Fields,
},
}
Expand description
The type that we use to represent the definition of the Borsh type. Description of data encoding on the wire.
Variants§
Primitive(u8)
A fixed-size type, which is considered undivisible
Sequence
Fields
length_width: u8
How many bytes does the length tag occupy.
Zero if this is fixed-length array or the length must be determined by means not specified in the schema. The schema is invalid if the value is greater than eight.
length_range: RangeInclusive<u64>
Bounds on the possible lengths of the sequence.
Note: The schema is invalid if the range is empty or length_width
is non-zero and either bound of the range cannot be represented as
length_width
-byte-wide unsigned integer.
elements: Declaration
Type of each element of the sequence.
A sequence of homogeneous elements.
If length_width
is non-zero, the sequence is tagged, i.e. prefixed by
the number of elements in the sequence. In that case, the length is
encoded as a length_width
-byte wide little-endian unsigned integer.
If length_width
is zero, the sequence is untagged. In that case, if
length_range
contains a single number, the sequence is fixed-sized
with the range determining number of elements. Otherwise, knowledge of
the type is necessary to be able to decode the number of elements.
Prototypical examples of the use of this definitions are:
[T; N]
→length_width: 0, length_range: N..=N, elements: "T"
andVec<T>
→length_width: 4, length_range: 0..=u32::MAX, elements: "T"
.
With length_width
and length_range
other custom encoding formats can
also be expressed. For example:
BoundedVec<LO, HI, T>
→length_width: 4, length_range: LO..=HI
;PascalString
→length_width: 1, length_range: 0..=255
;Ipv4Packet
→length_width: 0, length_range: 20..=65536
orVarInt<u32>
→length_width: 0, length_range: 1..=5
.
Tuple
Fields
elements: Vec<Declaration>
A fixed-size tuple with the length known at the compile time and the elements of different types.
Enum
Fields
tag_width: u8
Width in bytes of the discriminant tag.
Zero indicates this is an untagged union. In standard borsh encoding this is one. Custom encoding formats may use larger width if they need to encode more than 256 variants. The schema is invalid if the value is greater than eight.
variants: Vec<(DiscriminantValue, VariantName, Declaration)>
Possible variants of the enumeration.
VariantName
is metadata, not present in a type’s serialized representation.
A possibly tagged union, a.k.a enum.
Tagged unions are prefixed by a tag identifying encoded variant followed
by encoding of that variant. The tag is tag_width
-byte wide
little-endian number.
Untagged unions don’t have a separate tag which means that knowledge of the type is necessary to fully analyse the binary. Variants may still be used to list possible values or determine the longest possible encoding.
Struct
A structure, structurally similar to a tuple.
Implementations§
source§impl Definition
impl Definition
sourcepub const ARRAY_LENGTH_WIDTH: u8 = 0u8
pub const ARRAY_LENGTH_WIDTH: u8 = 0u8
Array length isn’t present in payload, it’s determined by type of data serialized.
sourcepub const DEFAULT_LENGTH_WIDTH: u8 = 4u8
pub const DEFAULT_LENGTH_WIDTH: u8 = 4u8
Convenience constant representing the length width of a standard borsh sequence.
Can be used for Definition::Sequence::length_width
.
sourcepub const DEFAULT_LENGTH_RANGE: RangeInclusive<u64> = _
pub const DEFAULT_LENGTH_RANGE: RangeInclusive<u64> = _
Convenience constant representing the length range of a standard borsh sequence.
It equals 0..=u32::MAX
. Can be used with
Definition::Sequence::length_range
.
Trait Implementations§
source§impl BorshDeserialize for Definition
impl BorshDeserialize for Definition
fn deserialize_reader<__R: Read>(reader: &mut __R) -> Result<Self, Error>
source§fn deserialize(buf: &mut &[u8]) -> Result<Self>
fn deserialize(buf: &mut &[u8]) -> Result<Self>
source§fn try_from_slice(v: &[u8]) -> Result<Self>
fn try_from_slice(v: &[u8]) -> Result<Self>
fn try_from_reader<R: Read>(reader: &mut R) -> Result<Self>
source§impl BorshSchema for Definition
impl BorshSchema for Definition
source§fn declaration() -> Declaration
fn declaration() -> Declaration
source§fn add_definitions_recursively(
definitions: &mut BTreeMap<Declaration, Definition>
)
fn add_definitions_recursively( definitions: &mut BTreeMap<Declaration, Definition> )
source§impl BorshSerialize for Definition
impl BorshSerialize for Definition
source§impl Clone for Definition
impl Clone for Definition
source§fn clone(&self) -> Definition
fn clone(&self) -> Definition
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for Definition
impl Debug for Definition
source§impl EnumExt for Definition
impl EnumExt for Definition
source§impl PartialEq for Definition
impl PartialEq for Definition
source§fn eq(&self, other: &Definition) -> bool
fn eq(&self, other: &Definition) -> bool
self
and other
values to be equal, and is used
by ==
.