pub struct Watched<T: ?Sized> { /* private fields */ }Expand description
This represents some value which will be interesting to watch. Watcher functions that reference this value will be re-run when this value changes.
Implementations§
Source§impl<T> Watched<T>
impl<T> Watched<T>
Sourcepub fn into_inner(this: Self) -> T
pub fn into_inner(this: Self) -> T
Consumes the Watched, returning the wrapped value
Sourcepub fn replace(this: &mut Self, value: T) -> T
pub fn replace(this: &mut Self, value: T) -> T
Replaces the wrapped value with a new one, returning the old value, without deinitializing either one.
Sourcepub fn take(this: &mut Self) -> Twhere
T: Default,
pub fn take(this: &mut Self) -> Twhere
T: Default,
Takes the wrapped value, leaving Default::default() in its place.
Sourcepub fn set_if_neq(wrapper: &mut Watched<T>, value: T)where
T: PartialEq,
pub fn set_if_neq(wrapper: &mut Watched<T>, value: T)where
T: PartialEq,
This function provides a way to set a value for a watched value only if is has changed. This is useful for cases where setting a value would otherwise cause an infinite loop.
§Examples
The following example uses the watch system to keep two variables in sync. This would normally cause an infinite loop as each update of one would cause the other one to re-evaluate. However using set_if_neq allows it to detect that the value is the same and stop propogating.
#[derive(Default)]
struct KeepBalanced {
left: Watched<i32>,
right: Watched<i32>,
}
impl Watcher<'static> for KeepBalanced {
fn init(mut init: impl WatcherInit<'static, Self>) {
init.watch(|root| {
Watched::set_if_neq(&mut root.left, *root.right);
});
init.watch(|root| {
Watched::set_if_neq(&mut root.right, *root.left);
});
}
}
let keep_balanced = Rc::new(RefCell::new(KeepBalanced {
left: Watched::new(7),
right: Watched::new(7),
}));
let weak = Rc::downgrade(&keep_balanced);
let mut ctx = WatchContext::new();
ctx.set_frame_limit(Some(10));
ctx.add_watcher(&weak);
*keep_balanced.borrow_mut().left = 3;
ctx.update();
assert_eq!(keep_balanced.borrow().right, 3);
*keep_balanced.borrow_mut().right = 21;
ctx.update();
assert_eq!(keep_balanced.borrow().left, 21);Trait Implementations§
Source§impl<T, U> AddAssign<U> for Watched<T>
impl<T, U> AddAssign<U> for Watched<T>
Source§fn add_assign(&mut self, rhs: U)
fn add_assign(&mut self, rhs: U)
+= operation. Read moreSource§impl<T, U> BitAndAssign<U> for Watched<T>where
T: BitAndAssign<U> + ?Sized,
impl<T, U> BitAndAssign<U> for Watched<T>where
T: BitAndAssign<U> + ?Sized,
Source§fn bitand_assign(&mut self, rhs: U)
fn bitand_assign(&mut self, rhs: U)
&= operation. Read moreSource§impl<T, U> BitOrAssign<U> for Watched<T>where
T: BitOrAssign<U> + ?Sized,
impl<T, U> BitOrAssign<U> for Watched<T>where
T: BitOrAssign<U> + ?Sized,
Source§fn bitor_assign(&mut self, rhs: U)
fn bitor_assign(&mut self, rhs: U)
|= operation. Read moreSource§impl<T, U> BitXorAssign<U> for Watched<T>where
T: BitXorAssign<U> + ?Sized,
impl<T, U> BitXorAssign<U> for Watched<T>where
T: BitXorAssign<U> + ?Sized,
Source§fn bitxor_assign(&mut self, rhs: U)
fn bitxor_assign(&mut self, rhs: U)
^= operation. Read moreSource§impl<T, U> DivAssign<U> for Watched<T>
impl<T, U> DivAssign<U> for Watched<T>
Source§fn div_assign(&mut self, rhs: U)
fn div_assign(&mut self, rhs: U)
/= operation. Read moreSource§impl<T, U> MulAssign<U> for Watched<T>
impl<T, U> MulAssign<U> for Watched<T>
Source§fn mul_assign(&mut self, rhs: U)
fn mul_assign(&mut self, rhs: U)
*= operation. Read moreSource§impl<T, U> PartialOrd<U> for Watched<T>
impl<T, U> PartialOrd<U> for Watched<T>
Source§impl<T, U> RemAssign<U> for Watched<T>
impl<T, U> RemAssign<U> for Watched<T>
Source§fn rem_assign(&mut self, rhs: U)
fn rem_assign(&mut self, rhs: U)
%= operation. Read moreSource§impl<T, U> ShlAssign<U> for Watched<T>
impl<T, U> ShlAssign<U> for Watched<T>
Source§fn shl_assign(&mut self, rhs: U)
fn shl_assign(&mut self, rhs: U)
<<= operation. Read moreSource§impl<T, U> ShrAssign<U> for Watched<T>
impl<T, U> ShrAssign<U> for Watched<T>
Source§fn shr_assign(&mut self, rhs: U)
fn shr_assign(&mut self, rhs: U)
>>= operation. Read moreSource§impl<T, U> SubAssign<U> for Watched<T>
impl<T, U> SubAssign<U> for Watched<T>
Source§fn sub_assign(&mut self, rhs: U)
fn sub_assign(&mut self, rhs: U)
-= operation. Read more