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>

source

pub fn new(value: T) -> Self

Create a new watched value.

source

pub fn into_inner(this: Self) -> T

Consumes the Watched, returning the wrapped value

source

pub fn replace(this: &mut Self, value: T) -> T

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

source§

impl<T: ?Sized> Watched<T>

source

pub fn get_unwatched(this: &Self) -> &T

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

source§

impl<T: Default> Watched<T>

source

pub fn take(this: &mut Self) -> T

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

source§

impl<T: PartialEq> Watched<T>

source

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<'a, T, U> Add<U> for &'a Watched<T>
where T: ?Sized, &'a T: Add<U>,

§

type Output = <&'a T as Add<U>>::Output

The resulting type after applying the + operator.
source§

fn add(self, other: U) -> <&'a T as Add<U>>::Output

Performs the + operation. Read more
source§

impl<T, U> AddAssign<U> for Watched<T>
where T: AddAssign<U> + ?Sized,

source§

fn add_assign(&mut self, rhs: U)

Performs the += operation. Read more
source§

impl<'a, T, U> BitAnd<U> for &'a Watched<T>
where T: ?Sized, &'a T: BitAnd<U>,

§

type Output = <&'a T as BitAnd<U>>::Output

The resulting type after applying the & operator.
source§

fn bitand(self, other: U) -> <&'a T as BitAnd<U>>::Output

Performs the & operation. Read more
source§

impl<T, U> BitAndAssign<U> for Watched<T>
where T: BitAndAssign<U> + ?Sized,

source§

fn bitand_assign(&mut self, rhs: U)

Performs the &= operation. Read more
source§

impl<'a, T, U> BitOr<U> for &'a Watched<T>
where T: ?Sized, &'a T: BitOr<U>,

§

type Output = <&'a T as BitOr<U>>::Output

The resulting type after applying the | operator.
source§

fn bitor(self, other: U) -> <&'a T as BitOr<U>>::Output

Performs the | operation. Read more
source§

impl<T, U> BitOrAssign<U> for Watched<T>
where T: BitOrAssign<U> + ?Sized,

source§

fn bitor_assign(&mut self, rhs: U)

Performs the |= operation. Read more
source§

impl<'a, T, U> BitXor<U> for &'a Watched<T>
where T: ?Sized, &'a T: BitXor<U>,

§

type Output = <&'a T as BitXor<U>>::Output

The resulting type after applying the ^ operator.
source§

fn bitxor(self, other: U) -> <&'a T as BitXor<U>>::Output

Performs the ^ operation. Read more
source§

impl<T, U> BitXorAssign<U> for Watched<T>
where T: BitXorAssign<U> + ?Sized,

source§

fn bitxor_assign(&mut self, rhs: U)

Performs the ^= operation. Read more
source§

impl<T: Debug + ?Sized> Debug for Watched<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: Default + ?Sized> Default for Watched<T>

source§

fn default() -> Watched<T>

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

impl<T: ?Sized> Deref for Watched<T>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &T

Dereferences the value.
source§

impl<T: ?Sized> DerefMut for Watched<T>

source§

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

Mutably dereferences the value.
source§

impl<'a, T, U> Div<U> for &'a Watched<T>
where T: ?Sized, &'a T: Div<U>,

§

type Output = <&'a T as Div<U>>::Output

The resulting type after applying the / operator.
source§

fn div(self, other: U) -> <&'a T as Div<U>>::Output

Performs the / operation. Read more
source§

impl<T, U> DivAssign<U> for Watched<T>
where T: DivAssign<U> + ?Sized,

source§

fn div_assign(&mut self, rhs: U)

Performs the /= operation. Read more
source§

impl<'a, T, U> Mul<U> for &'a Watched<T>
where T: ?Sized, &'a T: Mul<U>,

§

type Output = <&'a T as Mul<U>>::Output

The resulting type after applying the * operator.
source§

fn mul(self, other: U) -> <&'a T as Mul<U>>::Output

Performs the * operation. Read more
source§

impl<T, U> MulAssign<U> for Watched<T>
where T: MulAssign<U> + ?Sized,

source§

fn mul_assign(&mut self, rhs: U)

Performs the *= operation. Read more
source§

impl<'a, T: ?Sized> Neg for &'a Watched<T>
where &'a T: Neg,

§

type Output = <&'a T as Neg>::Output

The resulting type after applying the - operator.
source§

fn neg(self) -> <&'a T as Neg>::Output

Performs the unary - operation. Read more
source§

impl<'a, T: ?Sized> Not for &'a Watched<T>
where &'a T: Not,

§

type Output = <&'a T as Not>::Output

The resulting type after applying the ! operator.
source§

fn not(self) -> <&'a T as Not>::Output

Performs the unary ! operation. Read more
source§

impl<T, U> PartialEq<U> for Watched<T>
where T: PartialEq<U> + ?Sized, U: ?Sized,

source§

fn eq(&self, other: &U) -> bool

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

fn ne(&self, other: &U) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T, U> PartialOrd<U> for Watched<T>
where T: PartialOrd<U> + ?Sized, U: ?Sized,

source§

fn partial_cmp(&self, other: &U) -> Option<Ordering>

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

fn lt(&self, other: &U) -> bool

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

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
source§

fn ge(&self, other: &U) -> bool

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

fn gt(&self, other: &U) -> bool

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

impl<'a, T, U> Rem<U> for &'a Watched<T>
where T: ?Sized, &'a T: Rem<U>,

§

type Output = <&'a T as Rem<U>>::Output

The resulting type after applying the % operator.
source§

fn rem(self, other: U) -> <&'a T as Rem<U>>::Output

Performs the % operation. Read more
source§

impl<T, U> RemAssign<U> for Watched<T>
where T: RemAssign<U> + ?Sized,

source§

fn rem_assign(&mut self, rhs: U)

Performs the %= operation. Read more
source§

impl<'a, T, U> Shl<U> for &'a Watched<T>
where T: ?Sized, &'a T: Shl<U>,

§

type Output = <&'a T as Shl<U>>::Output

The resulting type after applying the << operator.
source§

fn shl(self, other: U) -> <&'a T as Shl<U>>::Output

Performs the << operation. Read more
source§

impl<T, U> ShlAssign<U> for Watched<T>
where T: ShlAssign<U> + ?Sized,

source§

fn shl_assign(&mut self, rhs: U)

Performs the <<= operation. Read more
source§

impl<'a, T, U> Shr<U> for &'a Watched<T>
where T: ?Sized, &'a T: Shr<U>,

§

type Output = <&'a T as Shr<U>>::Output

The resulting type after applying the >> operator.
source§

fn shr(self, other: U) -> <&'a T as Shr<U>>::Output

Performs the >> operation. Read more
source§

impl<T, U> ShrAssign<U> for Watched<T>
where T: ShrAssign<U> + ?Sized,

source§

fn shr_assign(&mut self, rhs: U)

Performs the >>= operation. Read more
source§

impl<'a, T, U> Sub<U> for &'a Watched<T>
where T: ?Sized, &'a T: Sub<U>,

§

type Output = <&'a T as Sub<U>>::Output

The resulting type after applying the - operator.
source§

fn sub(self, other: U) -> <&'a T as Sub<U>>::Output

Performs the - operation. Read more
source§

impl<T, U> SubAssign<U> for Watched<T>
where T: SubAssign<U> + ?Sized,

source§

fn sub_assign(&mut self, rhs: U)

Performs the -= operation. Read more
source§

impl<'a, T> WatchedValueCore<'static, DefaultOwner> for &'a Watched<T>
where T: ?Sized,

§

type Value = &'a T

source§

fn get(self, ctx: WatchArg<'_, 'static, DefaultOwner>) -> Self::Value

source§

fn get_unwatched(self) -> Self::Value

source§

fn map<F, U>(self, map_fn: F) -> impl WatchedValueCore<'ctx, O, Value = U>
where Self: Sized, F: FnOnce(Self::Value) -> U,

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> 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.