Skip to main content

xur_runtime/marshal/
dynamic.rs

1//! Dynamic values that say what they are, for signatures that would
2//! otherwise read `any`.
3//!
4//! A native function that takes or returns whatever its receiver holds (a
5//! list's element, a map's value) works on a raw [`Value`]. Taking or
6//! returning [`Dyn<D>`] instead converts nothing (the value passes through
7//! as is) but describes the parameter as `D` does: `list.get` returns
8//! `Opt<Dyn<desc::T>>`, shown `Option<T>`.
9
10use std::marker::PhantomData;
11
12use super::{FromValue, IntoXurValue, TypeDesc};
13use crate::convert::ConvertCx;
14use crate::{RunResult, Value};
15
16/// A type's description, for [`Dyn`].
17pub trait Describe {
18    fn type_desc() -> TypeDesc;
19}
20
21/// A [`Value`] passed through unchanged, described as `D`.
22pub struct Dyn<D>(pub Value, PhantomData<fn() -> D>);
23
24impl<D> Dyn<D> {
25    pub fn new(value: Value) -> Self {
26        Self(value, PhantomData)
27    }
28}
29
30impl<D: Describe + 'static> FromValue for Dyn<D> {
31    fn type_desc() -> TypeDesc {
32        D::type_desc()
33    }
34    fn from_value(_cx: &dyn ConvertCx, value: &Value) -> RunResult<Self> {
35        Ok(Self::new(value.clone()))
36    }
37}
38
39impl<D: Describe + 'static> IntoXurValue for Dyn<D> {
40    fn type_desc() -> TypeDesc {
41        D::type_desc()
42    }
43    fn into_xur_value(self, _cx: &dyn ConvertCx) -> RunResult<Value> {
44        Ok(self.0)
45    }
46}
47
48/// Descriptions for [`Dyn`]: type parameters and common shapes.
49pub mod desc {
50    use std::marker::PhantomData;
51
52    use super::{Describe, TypeDesc};
53
54    macro_rules! params {
55        ($($name:ident: $doc:literal),*) => {$(
56            #[doc = $doc]
57            pub struct $name;
58            impl Describe for $name {
59                fn type_desc() -> TypeDesc {
60                    TypeDesc::Name(stringify!($name))
61                }
62            }
63        )*};
64    }
65    params! {
66        T: "The receiver's element type: a list's items, a cell's value.",
67        U: "A second type parameter, chosen by the caller (what a closure returns).",
68        K: "A map's key type.",
69        V: "A map's value type."
70    }
71
72    /// Any value.
73    pub struct Any;
74    impl Describe for Any {
75        fn type_desc() -> TypeDesc {
76            TypeDesc::ANY
77        }
78    }
79
80    /// An integer or a float.
81    pub struct Number;
82    impl Describe for Number {
83        fn type_desc() -> TypeDesc {
84            TypeDesc::Union(vec![TypeDesc::Name("int"), TypeDesc::Name("float")])
85        }
86    }
87
88    /// `list<D>`.
89    pub struct ListOf<D>(PhantomData<fn() -> D>);
90    impl<D: Describe> Describe for ListOf<D> {
91        fn type_desc() -> TypeDesc {
92            TypeDesc::Generic("list", vec![D::type_desc()])
93        }
94    }
95
96    /// `map<K, V>`.
97    pub struct MapOf<K, V>(PhantomData<fn() -> (K, V)>);
98    impl<K: Describe, V: Describe> Describe for MapOf<K, V> {
99        fn type_desc() -> TypeDesc {
100            TypeDesc::Generic("map", vec![K::type_desc(), V::type_desc()])
101        }
102    }
103
104    /// `(A, B)`.
105    pub struct Tuple2<A, B>(PhantomData<fn() -> (A, B)>);
106    impl<A: Describe, B: Describe> Describe for Tuple2<A, B> {
107        fn type_desc() -> TypeDesc {
108            TypeDesc::Tuple(vec![A::type_desc(), B::type_desc()])
109        }
110    }
111
112    /// `str`, for tuples like `(str, T)`.
113    pub struct Str;
114    impl Describe for Str {
115        fn type_desc() -> TypeDesc {
116            TypeDesc::STR
117        }
118    }
119
120    /// `i64`, for tuples like `(i64, T)`.
121    pub struct Int;
122    impl Describe for Int {
123        fn type_desc() -> TypeDesc {
124            TypeDesc::Name("i64")
125        }
126    }
127}