pub struct TxPathBuilder { /* private fields */ }Expand description
Fluent builder for reading an inner field from the current transaction.
Obtained from the context via
ctx.tx().path(); rooting
it there is what guarantees the terminal get reads through the
current-transaction host function and never crosses into a ledger-object read. Each
field / index call appends one 4-byte segment to the
underlying Locator buffer.
A path longer than the 64-byte buffer (more than 16 segments) can hold is not silently
truncated: the overflow is remembered and surfaced as host::Error::LocatorMalformed from
get, rather than sending the host a shorter path than the author wrote.
use xrpl_common_stdlib::current_tx::traits::TransactionCommonFields;
use xrpl_common_stdlib::host::Result;
use xrpl_common_stdlib::sfield;
use xrpl_common_stdlib::types::blob::StandardBlob;
// Walk every entry of an array field.
if let Result::Ok(count) = tx.path().field(sfield::Memos).array_len() {
for i in 0..count {
let memo_type = tx
.path()
.field(sfield::Memos)
.index(i)
.field(sfield::Memo)
.field(sfield::MemoType)
.get::<StandardBlob>();
}
}Implementations§
Source§impl TxPathBuilder
impl TxPathBuilder
Sourcepub fn field<T, const CODE: i32>(self, _field: SField<T, CODE>) -> Self
pub fn field<T, const CODE: i32>(self, _field: SField<T, CODE>) -> Self
Append a field code to the path.
Takes a typed SField<T, CODE> constant (e.g. sfield::Memos) so the field code is a
compile-time constant; only the code is encoded — the field’s declared type T is
irrelevant to the path and is chosen instead at get.
Sourcepub fn index(self, index: u32) -> Self
pub fn index(self, index: u32) -> Self
Append an array slot index to the path (e.g. the 0 in Memos[0]).
Locator segments are i32, so an index above i32::MAX would pack as a negative value the
host would read back as a field code. Rather than encode that, the path is marked malformed
and the terminals report host::Error::LocatorMalformed. No array reachable through
array_len can be that long — the host reports lengths as i32 — so this
only rejects an index that did not come from walking the array.
Sourcepub fn get<T: FromCurrentTx>(&self) -> Result<T>
pub fn get<T: FromCurrentTx>(&self) -> Result<T>
Execute the tx_inner host call for the built path and decode the result as T.
T picks the terminal type (and therefore the read buffer size and decoder); it must be
readable from a transaction, hence the FromCurrentTx bound.
Returns host::Error::LocatorMalformed without calling the host if the path overflowed
the buffer while being built.
Sourcepub fn get_optional<T: FromCurrentTx>(&self) -> Result<Option<T>>
pub fn get_optional<T: FromCurrentTx>(&self) -> Result<Option<T>>
Like get but treats an absent field as Ok(None) rather than an error —
the inner-path counterpart to
get_field_optional.
Returns host::Error::LocatorMalformed without calling the host if the path overflowed.
Sourcepub fn array_len(&self) -> Result<u32>
pub fn array_len(&self) -> Result<u32>
Ask the host how many entries the array at this path holds, so it can be iterated.
Named array_len rather than len because Locator::len already means “bytes packed into
the path”; this is the length of the array the path points at. Returns Ok(0) for a
present-but-empty array, and host::Error::LocatorMalformed without calling the host if
the path overflowed the buffer while being built.
A single-segment path — one field and no index — is the
length of a top-level array such as Memos, so top-level arrays need no separate accessor.
Trait Implementations§
Source§impl Clone for TxPathBuilder
impl Clone for TxPathBuilder
Source§fn clone(&self) -> TxPathBuilder
fn clone(&self) -> TxPathBuilder
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more