looking_glass/
primatives.rs1use crate::*;
2pub use bytes::Bytes;
3pub use smol_str::SmolStr;
4
5macro_rules! impl_value_for_value {
6 ($enum:ident, $type:ident) => {
7 impl<'val, 'ty> FromValue<'val, 'ty> for $type {
8 fn from_value(value: &Value<'val, 'ty>) -> Option<Self> {
9 if let ValueInner::$enum(val) = value.0 {
10 Some(val)
11 } else {
12 None
13 }
14 }
15 }
16
17 impl<'ty> Typed<'ty> for $type {
18 fn ty() -> ValueTy {
19 ValueTy::$enum
20 }
21 fn as_value<'val>(&'val self) -> Value<'val, 'ty>
22 where
23 'ty: 'val,
24 {
25 Value(ValueInner::$enum(self.clone()))
26 }
27 }
28
29 impl IntoValue for $type {
30 fn into_value<'val, 'ty>(self) -> Value<'val, 'ty> {
31 Value(ValueInner::$enum(self.clone()))
32 }
33 }
34
35 impl<'val> IntoInner<$type> for OwnedValue<'val> {
36 fn into_inner(self) -> Result<$type, Error> {
37 if let OwnedValue::$enum(x) = self {
38 Ok(x)
39 } else {
40 Err(Error::TypeError {
41 expected: SmolStr::new("$enum"),
42 found: SmolStr::new("n/a"),
43 })
44 }
45 }
46 }
47
48 impl<'ty> From<$type> for OwnedValue<'ty> {
49 fn from(val: $type) -> Self {
50 OwnedValue::$enum(val)
51 }
52 }
53 };
54}
55
56impl_value_for_value!(U64, u64);
57impl_value_for_value!(U32, u32);
58impl_value_for_value!(U16, u16);
59impl_value_for_value!(U8, u8);
60impl_value_for_value!(I64, i64);
61impl_value_for_value!(I32, i32);
62impl_value_for_value!(I16, i16);
63impl_value_for_value!(I8, i8);
64impl_value_for_value!(F32, f32);
65impl_value_for_value!(F64, f64);
66impl_value_for_value!(Bool, bool);