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" and
  • Vec<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;
  • PascalStringlength_width: 1, length_range: 0..=255;
  • Ipv4Packetlength_width: 0, length_range: 20..=65536 or
  • VarInt<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

Fields

§fields: Fields

A structure, structurally similar to a tuple.

Implementations§

source§

impl Definition

source

pub const ARRAY_LENGTH_WIDTH: u8 = 0u8

Array length isn’t present in payload, it’s determined by type of data serialized.

source

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.

source

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

source§

fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self, Error>

source§

fn deserialize(buf: &mut &[u8]) -> Result<Self>

Deserializes this instance from a given slice of bytes. Updates the buffer to point at the remaining bytes.
source§

fn try_from_slice(v: &[u8]) -> Result<Self>

Deserialize this instance from a slice of bytes.
source§

fn try_from_reader<R: Read>(reader: &mut R) -> Result<Self>

source§

impl BorshSchema for Definition

source§

fn declaration() -> Declaration

Get the name of the type without brackets.
source§

fn add_definitions_recursively( definitions: &mut BTreeMap<Declaration, Definition> )

Recursively, using DFS, add type definitions required for this type. Type definition partially explains how to serialize/deserialize a type.
source§

impl BorshSerialize for Definition

source§

fn serialize<W: Write>(&self, writer: &mut W) -> Result<(), Error>

source§

impl Clone for Definition

source§

fn clone(&self) -> Definition

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Definition

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl EnumExt for Definition

source§

fn deserialize_variant<R: Read>( reader: &mut R, variant_tag: u8 ) -> Result<Self, Error>

Deserialises given variant of an enum from the reader. Read more
source§

impl PartialEq for Definition

source§

fn eq(&self, other: &Definition) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Definition

source§

impl StructuralEq for Definition

source§

impl StructuralPartialEq for Definition

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.