zng_var/var_impl/
const_var.rs

1//! Read-only static variable that owns the value locally (on the var SmallBox).
2
3use crate::{Var, VarValue};
4
5use super::*;
6
7/// A value-to-var conversion that consumes the value.
8///
9/// Every [`Var<T>`] implements this to convert to itself, every [`VarValue`] implements this to
10/// convert to a [`const_var`].
11///
12/// This trait is used by most properties, it allows then to accept literal values, variables and context variables
13/// all with a single signature. Together with [`Var<T>`] this gives properties great flexibility of usage. Widget
14/// `when` blocks also use [`IntoVar<T>`] to support changing the property value depending on the widget state.
15///
16/// Value types can also manually implement this to support a shorthand literal syntax for when they are used in properties,
17/// this converts the *shorthand value* like a tuple into the actual value type and wraps it into a variable, usually [`const_var`].
18/// Value types can implement the trait multiple times to support different shorthand syntaxes.
19///
20/// [`const_var`]: crate::const_var
21#[diagnostic::on_unimplemented(
22    note = "`IntoVar<T>` is implemented for all `T: VarValue`",
23    note = "`IntoVar<T>` is implemented for `Var<T>`, `ContextVar<T>` and others"
24)]
25pub trait IntoVar<T: VarValue> {
26    #[allow(missing_docs)]
27    fn into_var(self) -> Var<T>;
28}
29impl<T: VarValue> IntoVar<T> for T {
30    fn into_var(self) -> Var<T> {
31        crate::const_var(self)
32    }
33}
34impl<T: VarValue> IntoVar<T> for Var<T> {
35    fn into_var(self) -> Var<T> {
36        self
37    }
38}
39
40pub(crate) struct ConstVar(BoxAnyVarValue);
41impl fmt::Debug for ConstVar {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        f.debug_tuple("AnyConstVar").field(&self.0.detailed_debug()).finish()
44    }
45}
46impl ConstVar {
47    pub(crate) fn new(small_box: BoxAnyVarValue) -> Self {
48        Self(small_box)
49    }
50}
51impl VarImpl for ConstVar {
52    fn clone_dyn(&self) -> DynAnyVar {
53        DynAnyVar::Const(ConstVar(self.0.clone_boxed()))
54    }
55
56    fn current_context(&self) -> DynAnyVar {
57        self.clone_dyn()
58    }
59
60    fn value_type(&self) -> TypeId {
61        self.0.type_id()
62    }
63
64    #[cfg(feature = "type_names")]
65    fn value_type_name(&self) -> &'static str {
66        let mut out = "";
67        self.with(&mut |v| {
68            out = v.type_name();
69        });
70        out
71    }
72
73    fn strong_count(&self) -> usize {
74        1
75    }
76
77    fn var_eq(&self, other: &DynAnyVar) -> bool {
78        match other {
79            DynAnyVar::Const(b) => std::ptr::eq(self, b),
80            _ => false,
81        }
82    }
83
84    fn var_instance_tag(&self) -> VarInstanceTag {
85        VarInstanceTag::NOT_SHARED
86    }
87
88    fn downgrade(&self) -> DynWeakAnyVar {
89        DynWeakAnyVar::Const(WeakConstVar)
90    }
91
92    fn capabilities(&self) -> VarCapability {
93        VarCapability::empty()
94    }
95
96    fn with(&self, visitor: &mut dyn FnMut(&dyn AnyVarValue)) {
97        visitor(&*self.0)
98    }
99
100    fn get(&self) -> BoxAnyVarValue {
101        self.0.clone_boxed()
102    }
103
104    fn set(&self, _: BoxAnyVarValue) -> bool {
105        false
106    }
107
108    fn update(&self) -> bool {
109        false
110    }
111
112    fn modify(&self, _: SmallBox<dyn FnMut(&mut AnyVarModify) + Send + 'static, smallbox::space::S4>) -> bool {
113        false
114    }
115
116    fn hook(&self, _: SmallBox<dyn FnMut(&AnyVarHookArgs) -> bool + Send + 'static, smallbox::space::S4>) -> VarHandle {
117        VarHandle::dummy()
118    }
119
120    fn last_update(&self) -> VarUpdateId {
121        VarUpdateId::never()
122    }
123
124    fn modify_info(&self) -> ModifyInfo {
125        ModifyInfo::never()
126    }
127
128    fn modify_importance(&self) -> usize {
129        0
130    }
131
132    fn is_animating(&self) -> bool {
133        false
134    }
135
136    fn hook_animation_stop(&self, _: AnimationStopFn) -> VarHandle {
137        VarHandle::dummy()
138    }
139}
140
141#[derive(Debug)]
142pub(crate) struct WeakConstVar;
143
144impl WeakVarImpl for WeakConstVar {
145    fn strong_count(&self) -> usize {
146        0
147    }
148
149    fn upgrade(&self) -> Option<DynAnyVar> {
150        None
151    }
152
153    fn clone_dyn(&self) -> DynWeakAnyVar {
154        DynWeakAnyVar::Const(WeakConstVar)
155    }
156}