Struct drying_paint::Watched
source · [−]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
sourceimpl<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
sourceimpl<T: ?Sized> Watched<T>
impl<T: ?Sized> Watched<T>
sourcepub fn get_unwatched(this: &Self) -> &T
pub fn get_unwatched(this: &Self) -> &T
Get a referenced to the wrapped value, without binding the current watch closure.
sourceimpl<T: PartialEq> Watched<T>
impl<T: PartialEq> Watched<T>
sourcepub fn set_if_neq(wrapper: &mut Watched<T>, value: T)
pub fn set_if_neq(wrapper: &mut Watched<T>, value: T)
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
sourceimpl<T, U> AddAssign<U> for Watched<T> where
T: AddAssign<U> + ?Sized,
impl<T, U> AddAssign<U> for Watched<T> where
T: AddAssign<U> + ?Sized,
sourcefn add_assign(&mut self, rhs: U)
fn add_assign(&mut self, rhs: U)
Performs the += operation. Read more
sourceimpl<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,
sourcefn bitand_assign(&mut self, rhs: U)
fn bitand_assign(&mut self, rhs: U)
Performs the &= operation. Read more
sourceimpl<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,
sourcefn bitor_assign(&mut self, rhs: U)
fn bitor_assign(&mut self, rhs: U)
Performs the |= operation. Read more
sourceimpl<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,
sourcefn bitxor_assign(&mut self, rhs: U)
fn bitxor_assign(&mut self, rhs: U)
Performs the ^= operation. Read more
sourceimpl<T, U> DivAssign<U> for Watched<T> where
T: DivAssign<U> + ?Sized,
impl<T, U> DivAssign<U> for Watched<T> where
T: DivAssign<U> + ?Sized,
sourcefn div_assign(&mut self, rhs: U)
fn div_assign(&mut self, rhs: U)
Performs the /= operation. Read more
sourceimpl<T, U> MulAssign<U> for Watched<T> where
T: MulAssign<U> + ?Sized,
impl<T, U> MulAssign<U> for Watched<T> where
T: MulAssign<U> + ?Sized,
sourcefn mul_assign(&mut self, rhs: U)
fn mul_assign(&mut self, rhs: U)
Performs the *= operation. Read more
sourceimpl<T, U> PartialOrd<U> for Watched<T> where
T: PartialOrd<U> + ?Sized,
U: ?Sized,
impl<T, U> PartialOrd<U> for Watched<T> where
T: PartialOrd<U> + ?Sized,
U: ?Sized,
sourcefn partial_cmp(&self, other: &U) -> Option<Ordering>
fn partial_cmp(&self, other: &U) -> Option<Ordering>
This method returns an ordering between self and other values if one exists. Read more
sourcefn lt(&self, other: &U) -> bool
fn lt(&self, other: &U) -> bool
This method tests less than (for self and other) and is used by the < operator. Read more
sourcefn le(&self, other: &U) -> bool
fn le(&self, other: &U) -> bool
This method tests less than or equal to (for self and other) and is used by the <=
operator. Read more
sourceimpl<T, U> RemAssign<U> for Watched<T> where
T: RemAssign<U> + ?Sized,
impl<T, U> RemAssign<U> for Watched<T> where
T: RemAssign<U> + ?Sized,
sourcefn rem_assign(&mut self, rhs: U)
fn rem_assign(&mut self, rhs: U)
Performs the %= operation. Read more
sourceimpl<T, U> ShlAssign<U> for Watched<T> where
T: ShlAssign<U> + ?Sized,
impl<T, U> ShlAssign<U> for Watched<T> where
T: ShlAssign<U> + ?Sized,
sourcefn shl_assign(&mut self, rhs: U)
fn shl_assign(&mut self, rhs: U)
Performs the <<= operation. Read more
sourceimpl<T, U> ShrAssign<U> for Watched<T> where
T: ShrAssign<U> + ?Sized,
impl<T, U> ShrAssign<U> for Watched<T> where
T: ShrAssign<U> + ?Sized,
sourcefn shr_assign(&mut self, rhs: U)
fn shr_assign(&mut self, rhs: U)
Performs the >>= operation. Read more
Auto Trait Implementations
impl<T> !RefUnwindSafe for Watched<T>
impl<T> !Send for Watched<T>
impl<T> !Sync for Watched<T>
impl<T: ?Sized> Unpin for Watched<T> where
T: Unpin,
impl<T> !UnwindSafe for Watched<T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more