xrpl_hooks/api/
slot.rs

1use super::*;
2
3/// Serialize and output a slotted object
4#[inline(always)]
5pub fn slot(slotted_obj: &mut [u8], slot_no: u32) -> Result<u64> {
6    buf_write_1arg(slotted_obj, slot_no, _c::slot)
7}
8
9/// Free up a currently occupied slot
10#[inline(always)]
11pub fn slot_clear(slot_no: u32) -> Result<u64> {
12    api_1arg_call(slot_no, _c::slot_clear)
13}
14
15/// Count the elements of an array object in a slot
16#[inline(always)]
17pub fn slot_count(slot_no: u32) -> Result<u64> {
18    api_1arg_call(slot_no, _c::slot_count)
19}
20
21/// Slot ID
22#[inline(always)]
23pub fn slot_id(slot_no: u32) -> Result<u64> {
24    api_1arg_call(slot_no, _c::slot_id)
25}
26
27/// Locate an object based on its keylet and place it into a slot
28#[inline(always)]
29pub fn slot_set(keylet: &[u8], slot_no: i32) -> Result<u64> {
30    let res = unsafe { _c::slot_set(keylet.as_ptr() as u32, keylet.len() as u32, slot_no) };
31
32    result_u64(res)
33}
34
35/// Compute the serialized size of an object in a slot
36#[inline(always)]
37pub fn slot_size(slot_no: u32) -> Result<u64> {
38    api_1arg_call(slot_no, _c::slot_size)
39}
40
41/// Index into a slotted array and assign a sub-object to another slot
42#[inline(always)]
43pub fn slot_subarray(parent_slot: u32, array_id: u32, new_slot: u32) -> Result<u64> {
44    api_3arg_call(parent_slot, array_id, new_slot, _c::slot_subarray)
45}
46
47/// Index into a slotted object and assign a sub-object to another slot
48#[inline(always)]
49pub fn slot_subfield(parent_slot: u32, field_id: FieldId, new_slot: u32) -> Result<u64> {
50    api_3arg_call(parent_slot, field_id as _, new_slot, _c::slot_subfield)
51}
52
53/// Retrieve the field code of an object in a slot and, optionally, some other information
54#[inline(always)]
55pub fn slot_type(slot_no: u32, flags: SlotTypeFlags) -> Result<FieldOrXrpAmount> {
56    match flags {
57        SlotTypeFlags::Field => {
58            let res = unsafe { _c::slot_type(slot_no, 0) };
59
60            match res {
61                res if res >= 0 => Ok(FieldOrXrpAmount::Field(unsafe {
62                    core::mem::transmute(res as u32)
63                })),
64                _ => Err(Error::from_code(res as _)),
65            }
66        }
67
68        SlotTypeFlags::XrpAmount => {
69            let res = unsafe { _c::slot_type(slot_no, 1) };
70
71            match res {
72                1 => Ok(FieldOrXrpAmount::XrpAmount),
73                res if res >= 0 => Ok(FieldOrXrpAmount::NonXrpAmount),
74                _ => Err(Error::from_code(res as _)),
75            }
76        }
77    }
78}
79
80/// Parse the STI_AMOUNT in the specified slot and return it as an XFL enclosed number
81#[inline(always)]
82pub fn slot_float(slot_no: u32) -> Result<XFL> {
83    let res = unsafe { _c::slot_float(slot_no) };
84
85    result_xfl(res)
86}