pub struct WatchContext<'ctx, O: ?Sized = DefaultOwner> { /* private fields */ }

Implementations§

source§

impl<'ctx, O> WatchContext<'ctx, O>

source

pub fn from_owner(owner: O) -> Self

source§

impl<'ctx, O: Default> WatchContext<'ctx, O>

source

pub fn new() -> Self

Create a new WatchContext

source§

impl<'ctx, O: ?Sized> WatchContext<'ctx, O>

source

pub fn add_watch<F>(&mut self, func: F)
where F: 'ctx + Fn(&mut O, WatchArg<'_, 'ctx, O>),

source

pub fn add_watch_might_add_watcher<F, T>(&mut self, func: F)
where F: 'ctx + Fn(&mut O, WatchArg<'_, 'ctx, O>) -> Option<T>, T: 'ctx + WatcherHolder<'ctx, O>, T::Content: Watcher<'ctx, O>,

source

pub fn add_watch_raw<F, N>(&mut self, debug_name: N, f: F)
where F: 'ctx + Fn(RawWatchArg<'_, 'ctx, O>), N: Into<WatchName>,

source

pub fn add_watcher<T>(&mut self, holder: &T)
where T: 'ctx + ?Sized + WatcherHolder<'ctx, O>, T::Content: Watcher<'ctx, O>,

source

pub fn owner(&mut self) -> &mut O

source

pub fn update(&mut self)

source

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();

Trait Implementations§

source§

impl<'ctx, O: Default> Default for WatchContext<'ctx, O>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'ctx, O = DefaultOwner> !RefUnwindSafe for WatchContext<'ctx, O>

§

impl<'ctx, O = DefaultOwner> !Send for WatchContext<'ctx, O>

§

impl<'ctx, O = DefaultOwner> !Sync for WatchContext<'ctx, O>

§

impl<'ctx, O: ?Sized> Unpin for WatchContext<'ctx, O>
where O: Unpin,

§

impl<'ctx, O = DefaultOwner> !UnwindSafe for WatchContext<'ctx, O>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.