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