Skip to main content

rshooks_api/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<i64> {
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<i64> {
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<i64> {
18    api_1arg_call(slot_no, _c::slot_count)
19}
20
21/// Locate an object based on its keylet and place it into a slot
22#[inline(always)]
23pub fn slot_set(keylet: &[u8], slot_no: u32) -> Result<i64> {
24    let res = unsafe { _c::slot_set(keylet.as_ptr() as u32, keylet.len() as u32, slot_no) };
25
26    result_i64(res)
27}
28
29/// Compute the serialized size of an object in a slot
30#[inline(always)]
31pub fn slot_size(slot_no: u32) -> Result<i64> {
32    api_1arg_call(slot_no, _c::slot_size)
33}
34
35/// Index into a slotted array and assign a sub-object to another slot
36#[inline(always)]
37pub fn slot_subarray(parent_slot: u32, array_id: u32, new_slot: u32) -> Result<i64> {
38    api_3arg_call(parent_slot, array_id, new_slot, _c::slot_subarray)
39}
40
41/// Index into a slotted object and assign a sub-object to another slot
42#[inline(always)]
43pub fn slot_subfield(parent_slot: u32, field_id: FieldId, new_slot: u32) -> Result<i64> {
44    api_3arg_call(parent_slot, field_id as _, new_slot, _c::slot_subfield)
45}
46
47/// Retrieve the field code of an object in a slot and, optionally, some other information
48#[inline(always)]
49pub fn slot_type(slot_no: u32, flags: SlotTypeFlags) -> Result<FieldOrXrpAmount> {
50    match flags {
51        SlotTypeFlags::Field => {
52            let res = unsafe { _c::slot_type(slot_no, 0) };
53
54            match res {
55                res if res >= 0 => Ok(FieldOrXrpAmount::Field(unsafe {
56                    core::mem::transmute(res as u32)
57                })),
58                _ => Err(Error::from_code(res as _)),
59            }
60        }
61
62        SlotTypeFlags::NativeAmount => {
63            let res = unsafe { _c::slot_type(slot_no, 1) };
64
65            match res {
66                1 => Ok(FieldOrXrpAmount::NativeAmount),
67                res if res >= 0 => Ok(FieldOrXrpAmount::NonNativeAmount),
68                _ => Err(Error::from_code(res as _)),
69            }
70        }
71    }
72}
73
74/// Parse the STI_AMOUNT in the specified slot and return it as an XFL enclosed number
75#[inline(always)]
76pub fn slot_float(slot_no: u32) -> Result<XFL> {
77    let res = unsafe { _c::slot_float(slot_no) };
78
79    result_xfl(res)
80}