Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions rayforce-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ const INTERNAL_FNS: &[&str] = &[
// src/store/serde.h — serialize / deserialize a U8 vector with IPC header
"ray_ser",
"ray_de",
// src/store/serde.h — payload size `ray_ser`/`.log.write` will emit (excluding the
// IPC header; <= 0 on failure). Paired with the `ray_ipc_header_t` type below.
"ray_serde_size",
// src/core/runtime.h — last per-VM error message (set with a RAY_ERROR)
"ray_error_msg",
// src/core/ipc.h — whether a handle still names a live connection
Expand Down Expand Up @@ -231,6 +234,9 @@ fn main() {
for func in INTERNAL_FNS {
builder = builder.allowlist_function(func);
}
// src/store/serde.h — the IPC frame header `ray_ser` prepends, so the safe crate can
// size a full frame from the real layout instead of a hand-copied constant.
builder = builder.allowlist_type("ray_ipc_header_t");

builder
.generate()
Expand Down
2 changes: 1 addition & 1 deletion rayforce/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub(crate) unsafe fn check(v: Raw) -> Result<Raw> {

/// `RAY_LAZY` — the deferred-DAG result type returned by `ray_eval` and
/// graph-aware builtins.
const RAY_LAZY: i8 = 104;
pub(crate) const RAY_LAZY: i8 = 104;

/// Materialize a lazy DAG result into a concrete value. A no-op for non-lazy
/// inputs; consumes the lazy reference and returns the owned materialized one.
Expand Down
38 changes: 38 additions & 0 deletions rayforce/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,44 @@ impl Value {
}
}

/// Exact length in bytes of the frame [`serialize`](Self::serialize) would return for
/// this value — IPC header plus payload — computed without allocating the serialized frame buffer.
///
/// This is also the number of bytes the core's `.log.write` appends for the same value
/// (`ray_log_write_fn`: one `ray_ipc_header_t` + `ray_serde_size` payload bytes).
///
/// Errors (never a guessed size): the core reports a non-positive payload size (e.g. an
/// encoded column that fails to decode, or size overflow), the total overflows `usize`,
/// or the value is a deferred (lazy) result. Values built by this crate are always
/// materialized; a lazy one is refused rather than evaluated just to be sized.
pub fn frame_len(&self) -> Result<usize> {
unsafe {
if raw::type_code(self.as_ptr()) == error::RAY_LAZY {
return Err(crate::error::RayError::binding(
"frame_len: lazy value (materialize first)",
));
}
let payload = sys::ray_serde_size(self.as_ptr());
if payload <= 0 {
let detail = {
let m = sys::ray_error_msg();
if m.is_null() {
String::new()
} else {
std::ffi::CStr::from_ptr(m).to_string_lossy().into_owned()
}
};
return Err(crate::error::RayError::binding(format!(
"frame_len: payload size {payload} {detail}"
)));
}
usize::try_from(payload)
.ok()
.and_then(|p| p.checked_add(std::mem::size_of::<sys::ray_ipc_header_t>()))
.ok_or_else(|| crate::error::RayError::binding("frame_len: size overflow"))
}
}

/// Serialize to a byte vector (core wire format with IPC header).
pub fn serialize(&self) -> Result<Vec<u8>> {
unsafe {
Expand Down
33 changes: 33 additions & 0 deletions rayforce/tests/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,36 @@ fn deserialize_garbage_errors() {
})
.unwrap();
}

/// `frame_len` is exactly `serialize().len()` for every value shape, without serializing.
#[test]
fn frame_len_matches_serialize() {
Runtime::scope(|rt| {
let mut values = vec![
Value::i64(7),
Value::f64(1.25),
Value::bool(false),
Value::sym("s"),
Value::string(""),
Value::string("a string value"),
Value::string(&"x".repeat(1 << 16)),
Value::vec(&[1i64, 2, 3]),
Value::vec::<i64>(&[]),
Value::vec(&[1.5f64, 2.5]),
Value::list(&[Value::i64(1), Value::string("two"), Value::sym("three")]),
Value::list(&[]),
];
values.push(rt.eval("(dict [a b] (list 1 \"x\"))").unwrap());
values.push(
rt.eval("(table [a b c] (list [1 2 3] ['x 'y 'z] (list \"p\" \"q\" \"r\")))")
.unwrap(),
);
values.push(rt.eval("(list (list 1 (list 2 \"deep\")) [4 5])").unwrap());
for v in &values {
let bytes = v.serialize().unwrap();
assert_eq!(v.frame_len().unwrap(), bytes.len(), "{v:?}");
}
Ok(())
})
.unwrap();
}
Loading