shape_runtime/stdlib/byte_utils.rs
1//! Shared byte array conversion utilities.
2//!
3//! Used by the `compress` and `archive` modules. With the option β
4//! Array<T> marshal landing (cluster #3, 2026-05-06 defections.md
5//! entry), the FromSlot layer extracts owned `Vec<i64>` directly
6//! from `Array<int>`-typed slots (`HeapKind::TypedArray` /
7//! `TypedArrayData::I64`). This module's job shrinks to the
8//! per-element 0..=255 range check that turns a `Vec<i64>` of
9//! arbitrary integers into a `Vec<u8>`.
10//!
11//! `bytes_to_array` is gone — bodies return `TypedReturn::Concrete(
12//! ConcreteReturn::Bytes(vec))` directly and the dispatcher projects
13//! the variant into a typed array slot.
14
15/// Range-check a `Vec<i64>` (semantically `Array<int>` of bytes) into
16/// a `Vec<u8>`.
17///
18/// Each element must be in `0..=255`. Returns an error message naming
19/// the out-of-range value on the first violation.
20pub fn bytes_from_i64_slice(arr: &[i64]) -> Result<Vec<u8>, String> {
21 let mut bytes = Vec::with_capacity(arr.len());
22 for &byte_val in arr.iter() {
23 if !(0..=255).contains(&byte_val) {
24 return Err(format!("byte value out of range: {}", byte_val));
25 }
26 bytes.push(byte_val as u8);
27 }
28 Ok(bytes)
29}