zng_var/
local.rs

1use std::any::TypeId;
2
3use super::arc::WeakArcVar;
4
5use super::*;
6
7/// Represents a single value as [`Var<T>`].
8///
9/// This is the var target for most [`IntoVar<T>`] implementations.
10#[derive(Clone)]
11pub struct LocalVar<T: VarValue>(pub T);
12
13impl<T: VarValue> crate::private::Sealed for LocalVar<T> {}
14
15impl<T: VarValue> AnyVar for LocalVar<T> {
16    fn clone_any(&self) -> BoxedAnyVar {
17        Box::new(self.clone())
18    }
19
20    fn as_any(&self) -> &dyn Any {
21        self
22    }
23
24    fn as_unboxed_any(&self) -> &dyn Any {
25        self
26    }
27
28    fn double_boxed_any(self: Box<Self>) -> Box<dyn Any> {
29        let me: BoxedVar<T> = self;
30        Box::new(me)
31    }
32
33    fn var_type_id(&self) -> TypeId {
34        TypeId::of::<T>()
35    }
36
37    fn get_any(&self) -> Box<dyn AnyVarValue> {
38        Box::new(self.0.clone())
39    }
40
41    fn with_any(&self, read: &mut dyn FnMut(&dyn AnyVarValue)) {
42        read(&self.0)
43    }
44
45    fn with_new_any(&self, _: &mut dyn FnMut(&dyn AnyVarValue)) -> bool {
46        false
47    }
48
49    fn set_any(&self, _: Box<dyn AnyVarValue>) -> Result<(), VarIsReadOnlyError> {
50        Err(VarIsReadOnlyError {
51            capabilities: self.capabilities(),
52        })
53    }
54
55    fn last_update(&self) -> VarUpdateId {
56        VarUpdateId::never()
57    }
58
59    fn is_contextual(&self) -> bool {
60        false
61    }
62
63    fn capabilities(&self) -> VarCapability {
64        VarCapability::empty()
65    }
66
67    fn hook_any(&self, _: Box<dyn Fn(&AnyVarHookArgs) -> bool + Send + Sync>) -> VarHandle {
68        VarHandle::dummy()
69    }
70
71    fn hook_animation_stop(&self, handler: Box<dyn FnOnce() + Send>) -> Result<(), Box<dyn FnOnce() + Send>> {
72        Err(handler)
73    }
74
75    fn strong_count(&self) -> usize {
76        0
77    }
78
79    fn weak_count(&self) -> usize {
80        0
81    }
82
83    fn actual_var_any(&self) -> BoxedAnyVar {
84        self.clone_any()
85    }
86
87    fn downgrade_any(&self) -> BoxedAnyWeakVar {
88        Box::new(WeakArcVar::<T>::new())
89    }
90
91    fn is_animating(&self) -> bool {
92        false
93    }
94
95    fn modify_importance(&self) -> usize {
96        0
97    }
98
99    fn var_ptr(&self) -> VarPtr {
100        VarPtr::new_never_eq(self)
101    }
102
103    fn get_debug(&self) -> Txt {
104        self.with(var_debug)
105    }
106
107    fn update(&self) -> Result<(), VarIsReadOnlyError> {
108        Var::modify(self, var_update)
109    }
110
111    fn map_debug(&self) -> BoxedVar<Txt> {
112        Var::map(self, var_debug).boxed()
113    }
114}
115
116impl<T: VarValue> IntoVar<T> for LocalVar<T> {
117    type Var = Self;
118
119    fn into_var(self) -> Self::Var {
120        self
121    }
122}
123impl<T: VarValue> IntoVar<T> for T {
124    type Var = LocalVar<T>;
125
126    fn into_var(self) -> Self::Var {
127        LocalVar(self)
128    }
129}
130
131impl<T: VarValue> Var<T> for LocalVar<T> {
132    type ReadOnly = Self;
133
134    type ActualVar = Self;
135
136    type Downgrade = WeakArcVar<T>;
137
138    type Map<O: VarValue> = LocalVar<O>;
139    type MapBidi<O: VarValue> = LocalVar<O>;
140
141    type FlatMap<O: VarValue, V: Var<O>> = V;
142
143    type FilterMap<O: VarValue> = LocalVar<O>;
144    type FilterMapBidi<O: VarValue> = LocalVar<O>;
145
146    type MapRef<O: VarValue> = LocalVar<O>;
147    type MapRefBidi<O: VarValue> = LocalVar<O>;
148
149    type Easing = LocalVar<T>;
150
151    fn with<R, F>(&self, read: F) -> R
152    where
153        F: FnOnce(&T) -> R,
154    {
155        read(&self.0)
156    }
157
158    fn modify<F>(&self, _: F) -> Result<(), VarIsReadOnlyError>
159    where
160        F: FnOnce(&mut VarModify<T>) + 'static,
161    {
162        Err(VarIsReadOnlyError {
163            capabilities: self.capabilities(),
164        })
165    }
166
167    fn actual_var(self) -> Self::ActualVar {
168        self
169    }
170
171    fn downgrade(&self) -> Self::Downgrade {
172        WeakArcVar::new()
173    }
174
175    fn into_value(self) -> T {
176        self.0
177    }
178
179    fn read_only(&self) -> Self::ReadOnly {
180        self.clone()
181    }
182
183    fn map<O, M>(&self, map: M) -> Self::Map<O>
184    where
185        O: VarValue,
186        M: FnMut(&T) -> O + Send + 'static,
187    {
188        LocalVar(self.with(map))
189    }
190
191    fn map_bidi<O, M, B>(&self, map: M, _: B) -> Self::MapBidi<O>
192    where
193        O: VarValue,
194        M: FnMut(&T) -> O + Send + 'static,
195        B: FnMut(&O) -> T + Send + 'static,
196    {
197        self.map(map)
198    }
199
200    fn flat_map<O, V, M>(&self, map: M) -> Self::FlatMap<O, V>
201    where
202        O: VarValue,
203        V: Var<O>,
204        M: FnMut(&T) -> V + Send + 'static,
205    {
206        self.with(map)
207    }
208
209    fn filter_map<O, M, I>(&self, map: M, fallback: I) -> Self::FilterMap<O>
210    where
211        O: VarValue,
212        M: FnMut(&T) -> Option<O> + Send + 'static,
213        I: Fn() -> O + Send + Sync + 'static,
214    {
215        LocalVar(self.with(map).unwrap_or_else(fallback))
216    }
217
218    fn filter_map_bidi<O, M, B, I>(&self, map: M, _: B, fallback: I) -> Self::FilterMapBidi<O>
219    where
220        O: VarValue,
221        M: FnMut(&T) -> Option<O> + Send + 'static,
222        B: FnMut(&O) -> Option<T> + Send + 'static,
223        I: Fn() -> O + Send + Sync + 'static,
224    {
225        self.filter_map(map, fallback)
226    }
227
228    fn map_ref<O, M>(&self, map: M) -> Self::MapRef<O>
229    where
230        O: VarValue,
231        M: Fn(&T) -> &O + Send + Sync + 'static,
232    {
233        LocalVar(self.with(|v| map(v).clone()))
234    }
235
236    fn map_ref_bidi<O, M, B>(&self, map: M, _: B) -> Self::MapRefBidi<O>
237    where
238        O: VarValue,
239        M: Fn(&T) -> &O + Send + Sync + 'static,
240        B: Fn(&mut T) -> &mut O + Send + Sync + 'static,
241    {
242        self.map_ref(map)
243    }
244
245    fn easing<F>(&self, _: Duration, _: F) -> Self::Easing
246    where
247        T: Transitionable,
248        F: Fn(EasingTime) -> EasingStep + Send + Sync + 'static,
249    {
250        self.clone()
251    }
252
253    fn easing_with<F, S>(&self, _: Duration, _: F, _: S) -> Self::Easing
254    where
255        T: Transitionable,
256        F: Fn(EasingTime) -> EasingStep + Send + Sync + 'static,
257        S: Fn(&animation::Transition<T>, EasingStep) -> T + Send + Sync + 'static,
258    {
259        self.clone()
260    }
261}