1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//! [`Value`] — the safe, reference-counted handle to a core `ray_t` object.
//!
//! `Value` owns exactly one reference. `Clone` retains, `Drop` releases (both
//! no-ops for the null singleton and error objects, per the core). It is
//! deliberately `!Send`/`!Sync`: the core is single-threaded with a
//! thread-local VM, so values must never cross threads.
use crate::error::{self, Result};
use crate::raw::{self, Raw};
use rayforce_sys as sys;
use std::fmt;
use std::marker::PhantomData;
/// A handle to a RayforceDB object (atom, vector, list, dict, table, …).
pub struct Value {
ptr: Raw,
/// Makes `Value` `!Send` + `!Sync`.
_not_send: PhantomData<*mut ()>,
}
impl Value {
/// Wrap a pointer whose reference this `Value` now owns (the common case for
/// values returned by core constructors and `eval`).
///
/// # Safety
/// `ptr` must be a valid, non-error core pointer carrying a reference that
/// is transferred to the returned `Value`.
pub(crate) unsafe fn from_owned(ptr: Raw) -> Value {
debug_assert!(!ptr.is_null(), "from_owned: null pointer");
// Defensive: value-null must be RAY_NULL_OBJ, never a bare C NULL.
// Normalize so a stray NULL can never reach `ray_release` on Drop.
let ptr = if ptr.is_null() { raw::null_obj() } else { ptr };
Value {
ptr,
_not_send: PhantomData,
}
}
/// Wrap a borrowed pointer, taking a new reference (`ray_retain`). Use for
/// pointers the core still owns (e.g. `ray_list_get`, `ray_dict_keys`).
///
/// # Safety
/// `ptr` must be a valid core pointer.
pub(crate) unsafe fn from_borrowed(ptr: Raw) -> Value {
sys::ray_retain(ptr);
Value {
ptr,
_not_send: PhantomData,
}
}
/// The untyped null singleton (`RAY_NULL_OBJ`).
pub fn null() -> Value {
// Arena-allocated singleton: retain/release are no-ops.
Value {
ptr: raw::null_obj(),
_not_send: PhantomData,
}
}
/// Borrow the underlying raw pointer (does not transfer ownership).
#[inline]
pub(crate) fn as_ptr(&self) -> Raw {
self.ptr
}
/// Consume `self`, returning the raw pointer and its reference (no release).
#[inline]
pub(crate) fn into_raw(self) -> Raw {
let p = self.ptr;
std::mem::forget(self);
p
}
/// Replace the held pointer after a C call that already **consumed** our old
/// reference (copy-on-write move semantics) and returned `new` as an owned
/// reference. The old pointer is not released here.
#[inline]
pub(crate) unsafe fn replace_ptr_consumed(&mut self, new: Raw) {
self.ptr = new;
}
/// The signed type tag (negative = atom, positive = vector, 0 = list, …).
#[inline]
pub fn type_code(&self) -> i8 {
unsafe { raw::type_code(self.ptr) }
}
/// `|type|` — the canonical (unsigned) type id.
#[inline]
pub fn abs_type(&self) -> i8 {
unsafe { raw::abs_type(self.ptr) }
}
/// True for atoms (scalars and function objects).
#[inline]
pub fn is_atom(&self) -> bool {
unsafe { raw::is_atom(self.ptr) }
}
/// True for homogeneous vectors (bool…str).
#[inline]
pub fn is_vec(&self) -> bool {
unsafe { raw::is_vec(self.ptr) }
}
/// True if this is the null singleton.
#[inline]
pub fn is_null(&self) -> bool {
raw::is_null_singleton(self.ptr)
}
/// Element / pair count for vectors, lists, and dicts; for other objects the
/// raw `len` field (not meaningful for atoms).
#[inline]
pub fn len_raw(&self) -> i64 {
unsafe { raw::len(self.ptr) }
}
/// Current core reference count (diagnostic).
#[inline]
pub fn ref_count(&self) -> u32 {
unsafe { raw::rc(self.ptr) }
}
/// Pretty-print via the core formatter (`ray_fmt`).
pub fn format(&self) -> String {
unsafe {
let s = sys::ray_fmt(self.ptr, 1);
if s.is_null() {
return String::new();
}
// ray_fmt can return an error object; don't read it as a string
// (and ray_release is a no-op on errors, so free explicitly).
if raw::is_err(s) {
sys::ray_error_free(s);
return String::new();
}
let p = sys::ray_str_ptr(s).cast::<u8>();
let n = sys::ray_str_len(s);
let out = if p.is_null() || n == 0 {
String::new()
} else {
String::from_utf8_lossy(std::slice::from_raw_parts(p, n)).into_owned()
};
sys::ray_release(s);
out
}
}
/// Serialize to a byte vector (core wire format with IPC header).
pub fn serialize(&self) -> Result<Vec<u8>> {
unsafe {
let ser = error::check(sys::ray_ser(self.ptr))?;
if ser.is_null() {
return Err(crate::error::RayError::binding("serialize returned null"));
}
let v = Value::from_owned(ser);
let p = raw::data(v.ptr) as *const u8;
let n = raw::len(v.ptr) as usize;
Ok(std::slice::from_raw_parts(p, n).to_vec())
}
}
/// Deserialize a value from bytes previously produced by [`Value::serialize`]
/// (or another Rayforce wire-format encoder).
pub fn deserialize(bytes: &[u8]) -> Result<Value> {
// Wrap the bytes in a U8 vector for ray_de.
let buf = Value::vec(bytes);
unsafe {
let de = error::check(sys::ray_de(buf.ptr))?;
if de.is_null() {
return Err(crate::error::RayError::binding("deserialize returned null"));
}
Ok(Value::from_owned(error::materialize(de)?))
}
}
}
impl Clone for Value {
fn clone(&self) -> Value {
unsafe {
sys::ray_retain(self.ptr);
Value {
ptr: self.ptr,
_not_send: PhantomData,
}
}
}
}
impl Drop for Value {
fn drop(&mut self) {
unsafe { sys::ray_release(self.ptr) }
}
}
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.format())
}
}
impl fmt::Debug for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Value(type={}, {})", self.type_code(), self.format())
}
}