guion/widgets/util/
state.rs1use super::*;
3use std::borrow::Cow;
4
5pub trait AtomState<E,T> where E: Env {
7 #[inline]
8 fn get(&self, _: &mut E::Context) -> T {
9 self.get_direct().unwrap()
10 }
11 fn get_direct(&self) -> Result<T,()>;
12}
13pub trait AtomStateMut<E,T>: AtomState<E,T> where E: Env {
15 #[inline]
16 fn set(&mut self, v: T, _: &mut E::Context) {
17 self.set_direct(v).unwrap()
18 }
19 fn set_direct(&mut self, v: T) -> Result<(),()>;
20}
21
22impl<E,T> AtomState<E,T> for T where T: Clone, E: Env {
23 #[inline]
24 fn get_direct(&self) -> Result<T,()> {
25 Ok(self.clone())
26 }
27}
28impl<E,T> AtomState<E,T> for &T where T: Clone, E: Env {
29 #[inline]
30 fn get_direct(&self) -> Result<T,()> {
31 Ok((**self).clone())
32 }
33}
34impl<E,T> AtomState<E,T> for &mut T where T: Clone, E: Env {
35 #[inline]
36 fn get_direct(&self) -> Result<T,()> {
37 Ok((**self).clone())
38 }
39}
40impl<E,T> AtomStateMut<E,T> for &mut T where T: Clone, E: Env {
41 #[inline]
42 fn set_direct(&mut self, v: T) -> Result<(),()> {
43 **self = v;
44 Ok(())
45 }
46}
47impl<E,T> AtomStateMut<E,T> for T where T: Clone, E: Env {
48 #[inline]
49 fn set_direct(&mut self, v: T) -> Result<(),()> {
50 *self = v;
51 Ok(())
52 }
53}
54
55impl<E,T> AtomState<E,T> for Cow<'_,T> where T: Clone, E: Env {
56 #[inline]
57 fn get_direct(&self) -> Result<T,()> {
58 Ok((*self.as_ref()).clone())
59 }
60}
61impl<E,T> AtomStateMut<E,T> for Cow<'_,T> where T: Clone, E: Env {
62 #[inline]
63 fn set_direct(&mut self, v: T) -> Result<(),()> {
64 *self.to_mut() = v;
65 Ok(())
66 }
67}
68
69unsafe impl<T,E> Statize<E> for dyn AtomState<E,T> where T: 'static, E: Env {
70 type Statur = dyn AtomState<E,T>;
71}
72unsafe impl<T,E> Statize<E> for dyn AtomStateMut<E,T> where T: 'static, E: Env {
73 type Statur = dyn AtomStateMut<E,T>;
74}
75
76unsafe impl<'w,T,E> Traitcast<dyn AtomState<E,T>+'w,E> for dyn Widget<E>+'w where E: Env, T: 'static {
77 type DestTypeID = dyn AtomState<E,T>;
78}
79unsafe impl<'w,T,E> TraitcastMut<dyn AtomState<E,T>+'w,E> for dyn WidgetMut<E>+'w where E: Env, T: 'static {
80 type DestTypeID = dyn AtomState<E,T>;
81}
82unsafe impl<'w,T,E> TraitcastMut<dyn AtomStateMut<E,T>+'w,E> for dyn WidgetMut<E>+'w where E: Env, T: 'static {
83 type DestTypeID = dyn AtomStateMut<E,T>;
84}