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

Create a new watched value.

Consumes the Watched, returning the wrapped value

Replaces the wrapped value with a new one, returning the old value, without deinitializing either one.

Get a referenced to the wrapped value, without binding the current watch closure.

Takes the wrapped value, leaving Default::default() in its place.

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

The resulting type after applying the + operator.

Performs the + operation. Read more

Performs the += operation. Read more

The resulting type after applying the & operator.

Performs the & operation. Read more

Performs the &= operation. Read more

The resulting type after applying the | operator.

Performs the | operation. Read more

Performs the |= operation. Read more

The resulting type after applying the ^ operator.

Performs the ^ operation. Read more

Performs the ^= operation. Read more

Formats the value using the given formatter. Read more

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

The resulting type after dereferencing.

Dereferences the value.

Mutably dereferences the value.

The resulting type after applying the / operator.

Performs the / operation. Read more

Performs the /= operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

Performs the *= operation. Read more

The resulting type after applying the - operator.

Performs the unary - operation. Read more

The resulting type after applying the ! operator.

Performs the unary ! operation. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

The resulting type after applying the % operator.

Performs the % operation. Read more

Performs the %= operation. Read more

The resulting type after applying the << operator.

Performs the << operation. Read more

Performs the <<= operation. Read more

The resulting type after applying the >> operator.

Performs the >> operation. Read more

Performs the >>= operation. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

Performs the -= operation. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.