pub fn with_encoded<T: Serialize, R>(
value: T,
limits: Limits,
scratch: &mut [u8],
consume: impl FnOnce(&[u8]) -> R,
) -> Result<R, Error>Expand description
Encode a payload using scratch storage or owned fallback, then consume its bytes.
Preflight selects a destination once: scratch when the admitted payload fits,
otherwise an owned buffer. Both paths retain encode’s two-pass admission
and allocation allowance; custom serializers may still allocate independently.
The owned value is dropped before the synchronous callback. Passing a
reference instead keeps ownership with the caller.
Encoding failure never calls consume; scratch contents on error are
unspecified. Only successful bytes may be published. Copy bytes that must
survive the callback, including saved retries. No framing is added.
let limits = ic_wire::Limits { bytes: 128, allocation: 2048, nodes: 64, depth: 8 };
let mut scratch = [0; 128];
let length = ic_wire::native::with_encoded(7_u64, limits, &mut scratch, |bytes| {
assert_eq!(bytes, [7]);
bytes.len()
})?;
assert_eq!(length, 1);Callback bytes cannot escape as a borrow or inside a future:
ⓘ
let limits = ic_wire::Limits { bytes: 128, allocation: 2048, nodes: 64, depth: 8 };
let mut scratch = [0; 128];
ic_wire::native::with_encoded(7_u64, limits, &mut scratch, |bytes| bytes);ⓘ
let limits = ic_wire::Limits { bytes: 128, allocation: 2048, nodes: 64, depth: 8 };
let mut scratch = [0; 128];
ic_wire::native::with_encoded(7_u64, limits, &mut scratch, |bytes| async move {
bytes.len()
});