Skip to main content

State

Struct State 

Source
pub struct State<'s, T: 'static> { /* private fields */ }
Expand description

A guard over one use_state slot.

Derefs to T, so *count += 1 works. The value lives in the store, so nothing is written back on drop; the only thing drop does is request a repaint if the value was mutably dereferenced.

Implementations§

Source§

impl<'s, T: 'static> State<'s, T>

Source

pub fn into_handle(self) -> Handle<'s, T>

Consume the guard and return a Copy Handle to the same slot.

This releases the guard’s borrow. There is deliberately no handle(&self): using a Handle while the guard is alive would double-borrow the same RefCell and panic.

Source

pub fn bind(&mut self) -> &mut T

Hand &mut T to a widget that writes into it directly.

Unlike &mut *state this does not mark the state dirty, so a bound widget does not ask for a repaint on every single frame. That is safe because the widget only changes the value in response to input, and input makes egui repaint anyway. This is what the bind prop of TextEdit, Checkbox, Slider and ComboBox expects.

Source

pub fn update_later(&self, f: impl FnOnce(&mut T) + 'static)

Queue a write for the end of the pass and request a repaint.

This is the way out of “the loop borrows the state, so the handler inside it cannot”: todos.update_later(move |t| t.remove(i)). The closure is 'static, so captures need move.

Calling this while the guard is alive is fine: the write happens long after the guard is gone.

Trait Implementations§

Source§

impl<T: 'static> Deref for State<'_, T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T: 'static> DerefMut for State<'_, T>

Source§

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

Mutably dereferences the value.
Source§

impl<T: 'static> Drop for State<'_, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'s, T> Freeze for State<'s, T>

§

impl<'s, T> !RefUnwindSafe for State<'s, T>

§

impl<'s, T> !Send for State<'s, T>

§

impl<'s, T> !Sync for State<'s, T>

§

impl<'s, T> Unpin for State<'s, T>

§

impl<'s, T> UnsafeUnpin for State<'s, T>

§

impl<'s, T> !UnwindSafe for State<'s, 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

§

fn simd_from(_simd: S, value: T) -> T

§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

§

fn simd_into(self, simd: S) -> T

Source§

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

Source§

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

Source§

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

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,