get_variable_size_field

Function get_variable_size_field 

Source
pub fn get_variable_size_field<const N: usize, F>(
    field_code: i32,
    host_fn: F,
) -> Result<([u8; N], usize)>
where F: FnOnce(i32, *mut u8, usize) -> i32,
Expand description

Helper function for retrieving variable-size fields.

This function encapsulates the common pattern for variable-size fields where:

  1. A buffer of maximum size is allocated
  2. A host function is called to retrieve the field
  3. The actual number of bytes written is returned (not validated for exact match)
  4. Both the buffer and the actual length are returned

This is used for fields like Amount and Blob where the actual size can vary.

§Type Parameters

  • N - The maximum size of the buffer (compile-time constant)
  • F - The type of the host function closure

§Arguments

  • field_code - The field code identifying which field to retrieve
  • host_fn - A closure that calls the appropriate host function
    • Takes: (field_code: i32, buffer_ptr: *mut u8, buffer_size: usize) -> i32
    • Returns: result code (number of bytes written or error code)

§Returns

Returns Result<([u8; N], usize)> containing the buffer and actual length if successful

§Example

let (buffer, len) = get_variable_size_field::<48>(
    field_code,
    |fc, buf, size| unsafe { get_current_ledger_obj_field(fc, buf, size) },
)?;