Skip to main content

xur_runtime/marshal/
wrappers.rs

1//! Wrapper types for the typed function API that extract from Value
2//! without copying.
3
4use std::sync::{Arc, Mutex};
5
6use crate::convert::ConvertCx;
7use crate::{RunResult, RuntimeError, Value};
8
9use super::{FromValue, IntoXurValue, TypeDesc};
10
11/// A shared mutable list, extracted from `Value::List` without copying.
12pub struct XurList(pub Arc<Mutex<Vec<Value>>>);
13
14impl FromValue for XurList {
15    fn type_desc() -> TypeDesc {
16        TypeDesc::Name("list")
17    }
18    fn from_value(_cx: &dyn ConvertCx, value: &Value) -> RunResult<Self> {
19        match value {
20            Value::List(arc) => Ok(XurList(Arc::clone(arc))),
21            _ => Err(RuntimeError::new("expected a list")),
22        }
23    }
24}
25
26impl IntoXurValue for XurList {
27    fn type_desc() -> TypeDesc {
28        TypeDesc::Name("list")
29    }
30    fn into_xur_value(self, _cx: &dyn ConvertCx) -> RunResult<Value> {
31        Ok(Value::List(self.0))
32    }
33}
34
35/// A string extracted from `Value::Str` without copying.
36pub struct XurStr(pub Arc<str>);
37
38impl std::ops::Deref for XurStr {
39    type Target = str;
40    fn deref(&self) -> &str {
41        &self.0
42    }
43}
44
45impl FromValue for XurStr {
46    fn type_desc() -> TypeDesc {
47        TypeDesc::Name("str")
48    }
49    fn from_value(_cx: &dyn ConvertCx, value: &Value) -> RunResult<Self> {
50        match value {
51            Value::Str(arc) => Ok(XurStr(Arc::clone(arc))),
52            _ => Err(RuntimeError::new("expected a string")),
53        }
54    }
55}
56
57impl IntoXurValue for XurStr {
58    fn type_desc() -> TypeDesc {
59        TypeDesc::Name("str")
60    }
61    fn into_xur_value(self, _cx: &dyn ConvertCx) -> RunResult<Value> {
62        Ok(Value::Str(self.0))
63    }
64}
65
66/// A shared mutable byte buffer, extracted from `Value::Bytes` without copying.
67pub struct XurBytes(pub Arc<Mutex<Vec<u8>>>);
68
69impl FromValue for XurBytes {
70    fn type_desc() -> TypeDesc {
71        TypeDesc::Name("bytes")
72    }
73    fn from_value(_cx: &dyn ConvertCx, value: &Value) -> RunResult<Self> {
74        match value {
75            Value::Bytes(arc) => Ok(XurBytes(Arc::clone(arc))),
76            _ => Err(RuntimeError::new("expected bytes")),
77        }
78    }
79}
80
81impl IntoXurValue for XurBytes {
82    fn type_desc() -> TypeDesc {
83        TypeDesc::Name("bytes")
84    }
85    fn into_xur_value(self, _cx: &dyn ConvertCx) -> RunResult<Value> {
86        Ok(Value::Bytes(self.0))
87    }
88}
89
90/// A raw pointer address, extracted from `Value::Ptr`.
91///
92/// `Value::Ptr` holds a bare `u64` with no `Facet` type, so it can't be a typed
93/// arg via the blanket impl; this wrapper lets `.function`/`.method` take a
94/// pointer directly (e.g. `|p: XurPtr|`) instead of hand-matching in a
95/// `raw_function`.
96#[derive(Clone, Copy)]
97pub struct XurPtr(pub u64);
98
99impl FromValue for XurPtr {
100    fn type_desc() -> TypeDesc {
101        TypeDesc::Name("ptr")
102    }
103    fn from_value(_cx: &dyn ConvertCx, value: &Value) -> RunResult<Self> {
104        match value {
105            Value::Ptr(addr) => Ok(XurPtr(*addr)),
106            _ => Err(RuntimeError::new("expected a pointer")),
107        }
108    }
109}
110
111impl IntoXurValue for XurPtr {
112    fn type_desc() -> TypeDesc {
113        TypeDesc::Name("ptr")
114    }
115    fn into_xur_value(self, _cx: &dyn ConvertCx) -> RunResult<Value> {
116        Ok(Value::Ptr(self.0))
117    }
118}
119
120#[cfg(test)]
121mod tests {
122    use super::*;
123    use crate::lock;
124
125    #[test]
126    fn xur_list_shares_the_arc() {
127        let list = Value::list(vec![Value::int(1), Value::int(2)]);
128        let Value::List(arc) = &list else { panic!() };
129        let orig = Arc::clone(arc);
130
131        let XurList(inner) = XurList(Arc::clone(arc));
132        lock(&inner).push(Value::int(3));
133
134        assert_eq!(lock(&orig).len(), 3);
135    }
136
137    #[test]
138    fn xur_bytes_shares_the_arc() {
139        let bytes = Value::Bytes(Arc::new(Mutex::new(vec![1, 2])));
140        let Value::Bytes(arc) = &bytes else { panic!() };
141        let orig = Arc::clone(arc);
142
143        let XurBytes(inner) = XurBytes(Arc::clone(arc));
144        lock(&inner).push(3);
145
146        assert_eq!(lock(&orig).len(), 3);
147    }
148}