pub(crate) fn ensure_ifds_byte_slots(slots: &mut Vec<Vec<u8>>, count: usize) -> Result<(), String> {
crate::staging_reserve::ensure_vec_slots_at_least(slots, count, "IFDS GPU byte slot")?;
for slot in slots.iter_mut().take(count) {
slot.clear();
}
Ok(())
}
pub(crate) fn write_ifds_zero_bytes(out: &mut Vec<u8>, len: usize) -> Result<(), String> {
crate::dispatch_decode::try_write_zero_bytes(out, len, "IFDS GPU zero byte staging")
}
pub(crate) fn write_ifds_u32_bytes(out: &mut Vec<u8>, values: &[u32]) -> Result<(), String> {
crate::dispatch_decode::try_pack_u32_into(values, out, "IFDS GPU u32 byte staging")
}
pub(crate) fn write_ifds_padded_u32_bytes(out: &mut Vec<u8>, values: &[u32]) -> Result<(), String> {
if values.is_empty() {
write_ifds_zero_bytes(out, std::mem::size_of::<u32>())
} else {
write_ifds_u32_bytes(out, values)
}
}
pub(crate) fn padded_pack_u32_into(words: &[u32], bytes: &mut Vec<u8>) -> Result<(), String> {
if words.is_empty() {
write_ifds_zero_bytes(bytes, std::mem::size_of::<u32>())
} else {
crate::dispatch_decode::try_pack_u32_into(words, bytes, "IFDS GPU padded u32 staging")
}
}
#[cfg(test)]
mod tests {
use super::{
ensure_ifds_byte_slots, padded_pack_u32_into, write_ifds_padded_u32_bytes,
write_ifds_u32_bytes, write_ifds_zero_bytes,
};
#[test]
fn byte_slots_reuse_existing_buffers() {
let mut slots = vec![vec![1, 2, 3]];
let original_capacity = slots[0].capacity();
ensure_ifds_byte_slots(&mut slots, 2).unwrap();
assert_eq!(slots.len(), 2);
assert!(slots[0].is_empty());
assert_eq!(slots[0].capacity(), original_capacity);
}
#[test]
fn u32_pack_helpers_preserve_wire_order_and_padding_contract() {
let mut bytes = Vec::new();
write_ifds_u32_bytes(&mut bytes, &[0x1122_3344, 0xaabb_ccdd]).unwrap();
assert_eq!(bytes, [0x44, 0x33, 0x22, 0x11, 0xdd, 0xcc, 0xbb, 0xaa]);
write_ifds_padded_u32_bytes(&mut bytes, &[]).unwrap();
assert_eq!(bytes, [0, 0, 0, 0]);
padded_pack_u32_into(&[0x0102_0304], &mut bytes).unwrap();
assert_eq!(bytes, [0x04, 0x03, 0x02, 0x01]);
write_ifds_zero_bytes(&mut bytes, 6).unwrap();
assert_eq!(bytes, [0, 0, 0, 0, 0, 0]);
}
}