1use std::{
2 any::{Any, TypeId},
3 fmt, ops,
4 sync::Arc,
5};
6
7use smallbox::*;
8
9use crate::WeakVar;
10
11pub struct BoxAnyVarValue(SmallBox<dyn AnyVarValue, space::S4>);
13impl ops::Deref for BoxAnyVarValue {
14 type Target = dyn AnyVarValue;
15
16 fn deref(&self) -> &Self::Target {
17 &*self.0
18 }
19}
20impl ops::DerefMut for BoxAnyVarValue {
21 fn deref_mut(&mut self) -> &mut Self::Target {
22 &mut *self.0
23 }
24}
25impl BoxAnyVarValue {
26 pub fn new(value: impl AnyVarValue) -> Self {
28 BoxAnyVarValue(smallbox!(value))
29 }
30
31 pub fn downcast<T: VarValue>(self) -> Result<T, Self> {
33 match self.downcast_ref::<T>() {
35 Some(v) => Ok(v.clone()),
36 None => Err(self),
37 }
38 }
39
40 pub fn type_id(&self) -> TypeId {
45 (*self.0).type_id()
46 }
47
48 pub fn detailed_debug(&self) -> impl fmt::Debug {
53 struct DetailedDebug<'a>(&'a BoxAnyVarValue);
54 impl<'a> fmt::Debug for DetailedDebug<'a> {
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 let mut b = f.debug_struct("BoxAnyVarValue");
57 b.field("value", &*self.0.0);
58 #[cfg(feature = "type_names")]
59 b.field("type_name()", &self.0.0.type_name());
60 #[cfg(not(feature = "type_names"))]
61 b.field("type_id()", &self.0.type_id());
62 b.field("is_heap()", &SmallBox::is_heap(&self.0.0));
63 b.finish()
64 }
65 }
66 DetailedDebug(self)
67 }
68}
69impl Clone for BoxAnyVarValue {
70 fn clone(&self) -> Self {
71 self.0.clone_boxed()
72 }
73}
74impl fmt::Debug for BoxAnyVarValue {
75 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76 fmt::Debug::fmt(&*self.0, f)
77 }
78}
79pub trait AnyVarValue: fmt::Debug + Any + Send + Sync {
91 fn clone_boxed(&self) -> BoxAnyVarValue;
93 fn eq_any(&self, other: &dyn AnyVarValue) -> bool;
95 #[cfg(feature = "type_names")]
99 fn type_name(&self) -> &'static str;
100
101 fn try_swap(&mut self, other: &mut dyn AnyVarValue) -> bool;
103}
104impl dyn AnyVarValue {
105 pub fn downcast_ref<T: VarValue>(&self) -> Option<&T> {
108 let any: &dyn Any = self;
109 any.downcast_ref()
110 }
111
112 pub fn downcast_mut<T: VarValue>(&mut self) -> Option<&mut T> {
115 let any: &mut dyn Any = self;
116 any.downcast_mut()
117 }
118
119 pub fn is<T: VarValue>(&self) -> bool {
121 let any: &dyn Any = self;
122 any.is::<T>()
123 }
124}
125impl PartialEq for dyn AnyVarValue {
126 fn eq(&self, other: &Self) -> bool {
127 self.eq_any(other)
128 }
129}
130impl<T> AnyVarValue for T
131where
132 T: fmt::Debug + PartialEq + Clone + Any + Send + Sync,
133{
134 fn clone_boxed(&self) -> BoxAnyVarValue {
135 BoxAnyVarValue::new(self.clone())
136 }
137
138 fn eq_any(&self, other: &dyn AnyVarValue) -> bool {
139 match other.downcast_ref::<T>() {
140 Some(o) => self == o,
141 None => false,
142 }
143 }
144
145 #[cfg(feature = "type_names")]
146 fn type_name(&self) -> &'static str {
147 std::any::type_name::<T>()
148 }
149
150 fn try_swap(&mut self, other: &mut dyn AnyVarValue) -> bool {
151 if let Some(other) = other.downcast_mut::<T>() {
152 std::mem::swap(self, other);
153 return true;
154 }
155 false
156 }
157}
158
159pub trait VarValue: AnyVarValue + Clone + PartialEq {}
182impl<T: AnyVarValue + Clone + PartialEq> VarValue for T {}
183
184pub struct ArcEq<T: fmt::Debug + Send + Sync>(pub Arc<T>);
189impl<T: fmt::Debug + Send + Sync> ArcEq<T> {
190 pub fn downgrade(this: &Self) -> WeakEq<T> {
192 WeakEq(Arc::downgrade(&this.0))
193 }
194}
195impl<T: fmt::Debug + Send + Sync> ops::Deref for ArcEq<T> {
196 type Target = Arc<T>;
197
198 fn deref(&self) -> &Self::Target {
199 &self.0
200 }
201}
202impl<T: fmt::Debug + Send + Sync> ArcEq<T> {
203 pub fn new(value: T) -> Self {
205 Self(Arc::new(value))
206 }
207}
208impl<T: fmt::Debug + Send + Sync> PartialEq for ArcEq<T> {
209 fn eq(&self, other: &Self) -> bool {
210 Arc::ptr_eq(&self.0, &other.0)
211 }
212}
213impl<T: fmt::Debug + Send + Sync> Eq for ArcEq<T> {}
214impl<T: fmt::Debug + Send + Sync> Clone for ArcEq<T> {
215 fn clone(&self) -> Self {
216 Self(Arc::clone(&self.0))
217 }
218}
219impl<T: fmt::Debug + Send + Sync> fmt::Debug for ArcEq<T> {
220 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
221 fmt::Debug::fmt(&*self.0, f)
222 }
223}
224
225pub struct WeakEq<T: Send + Sync>(pub std::sync::Weak<T>);
230impl<T: fmt::Debug + Send + Sync> WeakEq<T> {
231 pub fn upgrade(&self) -> Option<ArcEq<T>> {
233 self.0.upgrade().map(ArcEq)
234 }
235}
236impl<T: Send + Sync> WeakEq<T> {
237 pub const fn new() -> Self {
239 Self(std::sync::Weak::new())
240 }
241}
242impl<T: Send + Sync> Default for WeakEq<T> {
243 fn default() -> Self {
244 Self(Default::default())
245 }
246}
247impl<T: Send + Sync> ops::Deref for WeakEq<T> {
248 type Target = std::sync::Weak<T>;
249
250 fn deref(&self) -> &Self::Target {
251 &self.0
252 }
253}
254impl<T: Send + Sync> PartialEq for WeakEq<T> {
255 fn eq(&self, other: &Self) -> bool {
256 std::sync::Weak::ptr_eq(&self.0, &other.0)
257 }
258}
259impl<T: Send + Sync> Eq for WeakEq<T> {}
260impl<T: Send + Sync> Clone for WeakEq<T> {
261 fn clone(&self) -> Self {
262 Self(std::sync::Weak::clone(&self.0))
263 }
264}
265impl<T: Send + Sync> fmt::Debug for WeakEq<T> {
266 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
267 fmt::Debug::fmt(&self.0, f)
268 }
269}
270
271#[derive(Clone)]
285pub struct VarEq<T: VarValue>(pub crate::Var<T>);
286impl<T: VarValue> ops::Deref for VarEq<T> {
287 type Target = crate::Var<T>;
288
289 fn deref(&self) -> &Self::Target {
290 &self.0
291 }
292}
293impl<T: VarValue> fmt::Debug for VarEq<T> {
294 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
295 fmt::Debug::fmt(&self.0, f)
296 }
297}
298impl<T: VarValue> PartialEq for VarEq<T> {
299 fn eq(&self, other: &Self) -> bool {
300 self.0.var_eq(&other.0)
301 }
302}
303impl<T: VarValue> VarEq<T> {
304 pub fn downgrade(&self) -> WeakVarEq<T> {
306 WeakVarEq(self.0.downgrade())
307 }
308}
309
310#[derive(Clone)]
314pub struct WeakVarEq<T: VarValue>(pub crate::WeakVar<T>);
315impl<T: VarValue> ops::Deref for WeakVarEq<T> {
316 type Target = WeakVar<T>;
317
318 fn deref(&self) -> &Self::Target {
319 &self.0
320 }
321}
322impl<T: VarValue> fmt::Debug for WeakVarEq<T> {
323 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
324 fmt::Debug::fmt(&self.0, f)
325 }
326}
327impl<T: VarValue> PartialEq for WeakVarEq<T> {
328 fn eq(&self, other: &Self) -> bool {
329 self.0.var_eq(&other.0)
330 }
331}
332impl<T: VarValue> WeakVarEq<T> {
333 pub fn upgrade(&self) -> Option<VarEq<T>> {
335 self.0.upgrade().map(VarEq)
336 }
337}
338
339#[diagnostic::on_unimplemented(
348 note = "`IntoValue<T>` is implemented for all `T: VarValue`",
349 note = "you can use `impl_from_and_into_var!` to implement conversions"
350)]
351pub trait IntoValue<T: VarValue>: Into<T> {}
352impl<T: VarValue> IntoValue<T> for T {}