Struct drying_paint::WatchContext
source · pub struct WatchContext<'ctx, O: ?Sized = DefaultOwner> { /* private fields */ }
Implementations§
source§impl<'ctx, O> WatchContext<'ctx, O>
impl<'ctx, O> WatchContext<'ctx, O>
pub fn from_owner(owner: O) -> Self
source§impl<'ctx, O: Default> WatchContext<'ctx, O>
impl<'ctx, O: Default> WatchContext<'ctx, O>
source§impl<'ctx, O: ?Sized> WatchContext<'ctx, O>
impl<'ctx, O: ?Sized> WatchContext<'ctx, O>
pub fn add_watch<F>(&mut self, func: F)
pub fn add_watch_might_add_watcher<F, T>(&mut self, func: F)
pub fn add_watch_raw<F, N>(&mut self, debug_name: N, f: F)
pub fn add_watcher<T>(&mut self, holder: &T)
pub fn owner(&mut self) -> &mut O
pub fn update(&mut self)
sourcepub fn set_frame_limit(&mut self, value: Option<usize>)
pub fn set_frame_limit(&mut self, value: Option<usize>)
Set the number of cycles this watch context will execute before panicking. This is useful for catching bugs involving cyclical watch triggers. None indicates no limit. The default behaviour is to provide a high value for debug builds and no limit for release builds.
If you get an unwanted panic because your use case runs up against the default limit without any truely unbounded cycle, you can use this function to increase or disable the limit.
Avoiding cyclical watch triggers
Generally recursive watches should be avoided, but one valid use case
is to keep two values in a kind of mutual sync where changing either
value updates the other. For this purpose, you may be interested in
a function which only triggers a watch if the value has actually
changed, such as Watched::set_if_neq
. The following example
panics, but it wouldn’t if used Watched::set_if_neq
.
Examples
#[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| {
*root.left = *root.right;
});
init.watch(|root| {
*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(1000));
ctx.add_watcher(&weak);
ctx.update();