Skip to main content

replace_tail_field

Function replace_tail_field 

Source
pub fn replace_tail_field(
    data: &mut [u8],
    body_end: usize,
    field_start: usize,
    old_len: usize,
    new_len: usize,
    write: impl FnOnce(&mut [u8]),
) -> Result<usize, ProgramError>
Expand description

Replace one field of a [u32 len] compact tail in place.

The tail’s fields are self-delimiting and laid out one after another behind the length prefix at body_end. field_start is the field’s offset inside the payload and old_len its current encoded length; new_len is the length of its replacement. The bytes after the field (every later field, including a raw final TailStr / TailBytes) move by new_len - old_len in one copy_within, write fills the field’s new slot, and the prefix is updated. Nothing else in the account is read or rewritten, so a setter costs one memmove of the suffix and one write of the field instead of a decode and re-encode of the whole tail.

Every bound is checked before any byte moves: the field must lie inside the current payload and the new payload must fit the account. write receives exactly new_len bytes and cannot fail, so a checked call never leaves the tail half-edited. Returns the new payload length.