xur-runtime 0.1.4

Values, native-function registry and facet conversions for the Xur language
Documentation
//! Wrapper types for the typed function API that extract from Value
//! without copying.

use std::sync::{Arc, Mutex};

use crate::convert::ConvertCx;
use crate::{RunResult, RuntimeError, Value};

use super::{FromValue, IntoXurValue};

/// A shared mutable list, extracted from `Value::List` without copying.
pub struct XurList(pub Arc<Mutex<Vec<Value>>>);

impl FromValue for XurList {
    fn from_value(_cx: &dyn ConvertCx, value: &Value) -> RunResult<Self> {
        match value {
            Value::List(arc) => Ok(XurList(Arc::clone(arc))),
            _ => Err(RuntimeError::new("expected a list")),
        }
    }
}

impl IntoXurValue for XurList {
    fn into_xur_value(self, _cx: &dyn ConvertCx) -> RunResult<Value> {
        Ok(Value::List(self.0))
    }
}

/// A string extracted from `Value::Str` without copying.
pub struct XurStr(pub Arc<str>);

impl std::ops::Deref for XurStr {
    type Target = str;
    fn deref(&self) -> &str {
        &self.0
    }
}

impl FromValue for XurStr {
    fn from_value(_cx: &dyn ConvertCx, value: &Value) -> RunResult<Self> {
        match value {
            Value::Str(arc) => Ok(XurStr(Arc::clone(arc))),
            _ => Err(RuntimeError::new("expected a string")),
        }
    }
}

impl IntoXurValue for XurStr {
    fn into_xur_value(self, _cx: &dyn ConvertCx) -> RunResult<Value> {
        Ok(Value::Str(self.0))
    }
}

/// A shared mutable byte buffer, extracted from `Value::Bytes` without copying.
pub struct XurBytes(pub Arc<Mutex<Vec<u8>>>);

impl FromValue for XurBytes {
    fn from_value(_cx: &dyn ConvertCx, value: &Value) -> RunResult<Self> {
        match value {
            Value::Bytes(arc) => Ok(XurBytes(Arc::clone(arc))),
            _ => Err(RuntimeError::new("expected bytes")),
        }
    }
}

impl IntoXurValue for XurBytes {
    fn into_xur_value(self, _cx: &dyn ConvertCx) -> RunResult<Value> {
        Ok(Value::Bytes(self.0))
    }
}

/// A raw pointer address, extracted from `Value::Ptr`.
///
/// `Value::Ptr` holds a bare `u64` with no `Facet` type, so it can't be a typed
/// arg via the blanket impl; this wrapper lets `.function`/`.method` take a
/// pointer directly (e.g. `|p: XurPtr|`) instead of hand-matching in a
/// `raw_function`.
#[derive(Clone, Copy)]
pub struct XurPtr(pub u64);

impl FromValue for XurPtr {
    fn from_value(_cx: &dyn ConvertCx, value: &Value) -> RunResult<Self> {
        match value {
            Value::Ptr(addr) => Ok(XurPtr(*addr)),
            _ => Err(RuntimeError::new("expected a pointer")),
        }
    }
}

impl IntoXurValue for XurPtr {
    fn into_xur_value(self, _cx: &dyn ConvertCx) -> RunResult<Value> {
        Ok(Value::Ptr(self.0))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::lock;

    #[test]
    fn xur_list_shares_the_arc() {
        let list = Value::list(vec![Value::int(1), Value::int(2)]);
        let Value::List(arc) = &list else { panic!() };
        let orig = Arc::clone(arc);

        let XurList(inner) = XurList(Arc::clone(arc));
        lock(&inner).push(Value::int(3));

        assert_eq!(lock(&orig).len(), 3);
    }

    #[test]
    fn xur_bytes_shares_the_arc() {
        let bytes = Value::Bytes(Arc::new(Mutex::new(vec![1, 2])));
        let Value::Bytes(arc) = &bytes else { panic!() };
        let orig = Arc::clone(arc);

        let XurBytes(inner) = XurBytes(Arc::clone(arc));
        lock(&inner).push(3);

        assert_eq!(lock(&orig).len(), 3);
    }
}