Skip to main content

FieldDecoder

Trait FieldDecoder 

Source
pub trait FieldDecoder: Sized {
    type Buffer: AsMut<[u8]> + AsRef<[u8]>;

    // Required methods
    fn empty_buffer() -> Self::Buffer;
    fn decode(
        buf: Self::Buffer,
        bytes_written: usize,
    ) -> Result<Self, DecodeError>;
}
Expand description

Decodes a typed value from the raw bytes a host function wrote.

Required Associated Types§

Source

type Buffer: AsMut<[u8]> + AsRef<[u8]>

The buffer a get_field caller allocates before invoking the host function. Each type picks its own size (an associated type, not a const, so this stays on stable Rust).

Required Methods§

Source

fn empty_buffer() -> Self::Buffer

Returns a zero-initialized buffer of this type’s Buffer size.

Source

fn decode(buf: Self::Buffer, bytes_written: usize) -> Result<Self, DecodeError>

Decodes Self from the full Buffer (as written by the host, then zero-padded to the buffer’s size), given bytes_written — the number of bytes the host actually wrote.

bytes_written carries the length that a &[u8] slice would otherwise bundle inside its fat pointer — passing the whole buffer plus bytes_written lets fixed-layout types (e.g. Amount) read the padded buffer in place, with no re-slice or re-copy.

Most fixed-size types require the host to have written exactly Buffer’s length and can then be built with a plain From<Buffer> — for those, implement this as decode_exact(*buf, bytes_written) (see [decode_exact]). Types with different semantics — e.g. Amount, where a shorter write is legitimate (XRP is 8 bytes, MPT 33, of a 48-byte buffer) and the variant is determined by the leading flag bits rather than the byte count — write a bespoke body instead.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl FieldDecoder for i32

Source§

type Buffer = [u8; 4]

Source§

fn empty_buffer() -> Self::Buffer

Source§

fn decode(buf: Self::Buffer, bytes_written: usize) -> Result<Self, DecodeError>

Source§

impl FieldDecoder for u8

Source§

type Buffer = [u8; 1]

Source§

fn empty_buffer() -> Self::Buffer

Source§

fn decode(buf: Self::Buffer, bytes_written: usize) -> Result<Self, DecodeError>

Source§

impl FieldDecoder for u16

Source§

type Buffer = [u8; 2]

Source§

fn empty_buffer() -> Self::Buffer

Source§

fn decode(buf: Self::Buffer, bytes_written: usize) -> Result<Self, DecodeError>

Source§

impl FieldDecoder for u32

Source§

type Buffer = [u8; 4]

Source§

fn empty_buffer() -> Self::Buffer

Source§

fn decode(buf: Self::Buffer, bytes_written: usize) -> Result<Self, DecodeError>

Source§

impl FieldDecoder for u64

Source§

type Buffer = [u8; 8]

Source§

fn empty_buffer() -> Self::Buffer

Source§

fn decode(buf: Self::Buffer, bytes_written: usize) -> Result<Self, DecodeError>

Implementors§

Source§

impl FieldDecoder for AccountID

FieldDecoder for XRPL account identifiers: decodes a 20-byte buffer into an AccountID, failing if the host wrote a different number of bytes.

Source§

type Buffer = [u8; 20]

Source§

impl FieldDecoder for Amount

FieldDecoder for XRPL amount values. The host writes a variable number of bytes into the fixed AMOUNT_SIZE buffer — 8 for XRP, 33 for MPT, 48 for IOU — with the remainder left as empty_buffer()’s zero-padding, which is exactly the shape Amount::from_bytes wants, so it reads the buffer in place with no re-slice or re-copy.

Source§

type Buffer = [u8; 48]

Source§

impl FieldDecoder for Currency

FieldDecoder for XRPL currency codes: decodes a 20-byte buffer into a Currency, failing if the host wrote a different number of bytes.

Source§

type Buffer = [u8; 20]

Source§

impl FieldDecoder for Issue

FieldDecoder for XRPL issues. The host writes a variable number of bytes into the fixed 40-byte buffer — 20 for XRP, 24 for MPT, 40 for IOU — and the variant is detected from bytes_written (see Issue::from_buffer); a count that matches none of those is a decode error.

Source§

type Buffer = [u8; 40]

Source§

impl FieldDecoder for Number

FieldDecoder for XRPL STNumber fields: decodes the 12-byte serialized value, failing if the host wrote a different number of bytes.

The bytes are taken as-is rather than routed back through float_from_stnumber: the host’s float representation is the STNumber serialization (rippled’s WASM host builds it with STNumber::add), so a value read off a transaction or ledger entry is already a canonical host-produced Number.

Source§

type Buffer = [u8; 12]

Source§

impl FieldDecoder for TransactionType

FieldDecoder for XRPL transaction types: decodes a 2-byte buffer via the existing (infallible) i16 mapping, failing only if the host wrote a different number of bytes.

Source§

type Buffer = [u8; 2]

Source§

impl<const N: usize> FieldDecoder for Blob<N>

FieldDecoder for any Blob<N>: copies whatever bytes the host wrote (at most N) into a Blob<N>, recording the actual length. Unlike fixed-size types, this never fails — blobs are variable-length by design.

Source§

type Buffer = [u8; N]

Source§

impl<const N: usize> FieldDecoder for UInt<N>

FieldDecoder for any fixed-width unsigned integer (Hash128/Hash160/Hash192/Hash256, and any other UInt<N> instantiation): decodes an N-byte buffer into UInt<N>, failing if the host wrote a different number of bytes.

Source§

type Buffer = [u8; N]