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§
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
source§impl<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.
source§impl<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§
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)
Performs the
+=
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)
Performs the
&=
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)
Performs the
|=
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)
Performs the
^=
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)
Performs the
/=
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)
Performs the
*=
operation. Read moresource§impl<T, U> PartialOrd<U> for Watched<T>
impl<T, U> PartialOrd<U> for Watched<T>
source§fn 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 moresource§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)
Performs the
%=
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)
Performs the
<<=
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)
Performs the
>>=
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)
Performs the
-=
operation. Read moresource§impl<'a, T> WatchedValueCore<'static, DefaultOwner> for &'a Watched<T>where
T: ?Sized,
impl<'a, T> WatchedValueCore<'static, DefaultOwner> for &'a Watched<T>where
T: ?Sized,
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§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more