From bd5dfe8be54a0dd62afbb58c854dba29702291cd Mon Sep 17 00:00:00 2001 From: Johny <353479812+Johny-Ivory@users.noreply.github.com> Date: Tue, 17 Nov 2026 03:45:00 -0500 Subject: [PATCH] Expose checked serialized frame length without allocating a frame Patch authored by Stevey; submitted by Johny. --- rayforce-sys/build.rs | 6 ++++++ rayforce/src/error.rs | 2 +- rayforce/src/value.rs | 38 ++++++++++++++++++++++++++++++++++++++ rayforce/tests/serde.rs | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/rayforce-sys/build.rs b/rayforce-sys/build.rs index be7daea..2d7691d 100644 --- a/rayforce-sys/build.rs +++ b/rayforce-sys/build.rs @@ -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 @@ -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() diff --git a/rayforce/src/error.rs b/rayforce/src/error.rs index d5b77aa..fc854e0 100644 --- a/rayforce/src/error.rs +++ b/rayforce/src/error.rs @@ -146,7 +146,7 @@ pub(crate) unsafe fn check(v: Raw) -> Result { /// `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. diff --git a/rayforce/src/value.rs b/rayforce/src/value.rs index db66a5b..fbe8e20 100644 --- a/rayforce/src/value.rs +++ b/rayforce/src/value.rs @@ -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 { + 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::())) + .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> { unsafe { diff --git a/rayforce/tests/serde.rs b/rayforce/tests/serde.rs index 6074007..45f518c 100644 --- a/rayforce/tests/serde.rs +++ b/rayforce/tests/serde.rs @@ -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::(&[]), + 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(); +}