1use crate::typed::Typed;
2use crate::*;
3pub use bytes::Bytes;
4
5#[derive(Clone, Debug)]
7pub enum OwnedValue<'ty> {
8 U64(u64),
9 U32(u32),
10 U16(u16),
11 U8(u8),
12 I64(i64),
13 I32(i32),
14 I16(i16),
15 I8(i8),
16 F32(f32),
17 F64(f64),
18 Bool(bool),
19 String(String),
20 Vec(Box<dyn VecInstance<'ty> + 'ty>),
21 HashMap(Box<dyn HashMapInstance<'ty> + 'ty>),
22 Struct(Box<dyn StructInstance<'ty> + 'ty>),
23 Enum(Box<dyn EnumInstance<'ty> + 'ty>),
24 Bytes(Bytes),
25 Option(Box<dyn OptionInstance<'ty> + 'ty>),
26}
27
28impl<'ty> PartialEq for OwnedValue<'ty> {
29 fn eq(&self, other: &Self) -> bool {
30 use OwnedValue::*;
31 match (self, other) {
32 (U64(l), U64(r)) => l == r,
33 (U32(l), U32(r)) => l == r,
34 (U16(l), U16(r)) => l == r,
35 (U8(l), U8(r)) => l == r,
36 (I64(l), I64(r)) => l == r,
37 (I32(l), I32(r)) => l == r,
38 (I16(l), I16(r)) => l == r,
39 (I8(l), I8(r)) => l == r,
40 (F32(l), F32(r)) => l == r,
41 (F64(l), F64(r)) => l == r,
42 (Bool(l), Bool(r)) => l == r,
43 (String(l), String(r)) => l == r,
44 (Vec(l), Vec(r)) => l == r,
45 (HashMap(l), HashMap(r)) => l == r,
46 (Struct(l), Struct(r)) => l == r,
47 (Enum(l), Enum(r)) => l == r,
48 (Bytes(l), Bytes(r)) => l == r,
49 _ => false,
50 }
51 }
52}
53
54impl<'v> OwnedValue<'v> {
55 pub fn as_ref<'val>(&'val self) -> Value<'val, 'v>
57 where
58 'v: 'val,
59 {
60 match self {
61 OwnedValue::U64(a) => a.as_value(),
62 OwnedValue::U32(a) => a.as_value(),
63 OwnedValue::U16(a) => a.as_value(),
64 OwnedValue::U8(a) => a.as_value(),
65 OwnedValue::I64(a) => a.as_value(),
66 OwnedValue::I32(a) => a.as_value(),
67 OwnedValue::I16(a) => a.as_value(),
68 OwnedValue::I8(a) => a.as_value(),
69 OwnedValue::F32(a) => a.as_value(),
70 OwnedValue::F64(a) => a.as_value(),
71 OwnedValue::Bool(a) => a.as_value(),
72 OwnedValue::String(a) => a.as_value(),
73 OwnedValue::Vec(a) => Value::from_vec(a.as_ref()),
74 OwnedValue::HashMap(a) => Value::from_hashmap(a.as_ref()),
75 OwnedValue::Struct(a) => Value::from_struct(a.as_ref()),
76 OwnedValue::Enum(a) => Value::from_enum(a.as_ref()),
77 OwnedValue::Bytes(b) => b.as_value(),
78 OwnedValue::Option(o) => Value::from_option(o.as_ref()),
79 }
80 }
81}
82
83impl<'ty> From<String> for OwnedValue<'ty> {
84 fn from(val: String) -> OwnedValue<'ty> {
85 OwnedValue::String(val)
86 }
87}
88
89#[derive(Clone)]
90pub enum CowValue<'val, 'ty> {
91 Owned(OwnedValue<'ty>),
92 Ref(Value<'val, 'ty>),
93}
94
95impl<'val, 'ty> From<Value<'val, 'ty>> for CowValue<'val, 'ty> {
96 fn from(v: Value<'val, 'ty>) -> Self {
97 Self::Ref(v)
98 }
99}
100
101impl<'val, 'ty> From<OwnedValue<'ty>> for CowValue<'val, 'ty> {
102 fn from(v: OwnedValue<'ty>) -> Self {
103 Self::Owned(v)
104 }
105}
106
107impl<'val, 'ty> CowValue<'val, 'ty> {
108 pub fn slow_eq<'c, 'd>(&self, other: &CowValue<'c, 'd>) -> bool {
109 self.as_ref().slow_eq(&other.as_ref())
110 }
111}
112
113impl std::fmt::Debug for CowValue<'_, '_> {
114 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
115 match self {
116 CowValue::Owned(o) => std::fmt::Debug::fmt(&o, f),
117 CowValue::Ref(r) => std::fmt::Debug::fmt(&r, f),
118 }
119 }
120}
121
122impl PartialEq for CowValue<'_, '_> {
123 fn eq(&self, other: &Self) -> bool {
124 self.as_ref() == other.as_ref()
125 }
126}
127
128impl<'val, 'ty> CowValue<'val, 'ty> {
129 pub fn as_ref(&'val self) -> Value<'val, 'ty> {
130 match self {
131 CowValue::Owned(owned) => owned.as_ref(),
132 CowValue::Ref(r) => r.clone(),
133 }
134 }
135
136 pub fn to_owned(&self) -> OwnedValue<'ty> {
137 match self {
138 CowValue::Owned(owned) => owned.clone(),
139 CowValue::Ref(r) => r.to_owned(),
140 }
141 }
142}