Skip to main content

Command

Struct Command 

Source
pub struct Command<T, E> { /* private fields */ }
Expand description

An async mutation: run it with a future, and see whether it’s pending and how it ended.

Create one with command (or Default), keep it where the views can use it and run it as often as needed. Clones share its state. A run continues when the command is dropped. See the module docs.

#[derive(Default)]
struct Editor {
    text: String,
    save: Command<(), String>,
}

fn ui(ui: &mut egui::Ui, editor: &mut Editor) {
    ui.text_edit_multiline(&mut editor.text);
    if ui.add_enabled(!editor.save.is_pending(), egui::Button::new("Save")).clicked() {
        editor.save.run(save(editor.text.clone()));
    }
    match editor.save.take_result() {
        Some(Ok(())) => { /* close the editor */ }
        Some(Err(error)) => { /* show the error in a toast */ }
        None => {}
    }
}

Implementations§

Source§

impl<T: MaybeSend + 'static, E: MaybeSend + 'static> Command<T, E>

Source

pub fn refreshes(self, key: impl Into<String>) -> Self

Once a run succeeded, invalidates the dependency key, like Nav::invalidate: the queries that depend on it refetch, and the loads that depend on it rerun. Call it once per dependency; a failed run invalidates nothing. See Keys, dependencies and refreshes.

Source

pub fn run( &self, future: impl Future<Output = Result<T, E>> + MaybeSend + 'static, )

Runs future, forgetting how the last run ended.

Source

pub fn is_pending(&self) -> bool

Whether a run is in progress.

Source

pub fn result(&self) -> Option<Ref<'_, Result<T, E>>>

How the last run ended, until the next one starts or it is taken.

Borrows the command: drop it before running the command again.

Source

pub fn output(&self) -> Option<Ref<'_, T>>

What the last run returned, if it succeeded. See result.

Source

pub fn error(&self) -> Option<Ref<'_, E>>

Why the last run failed, if it did. See result.

Source

pub fn take_result(&self) -> Option<Result<T, E>>

How the last run ended, taken: to act on it once (close a dialog, navigate to what it created).

Trait Implementations§

Source§

impl<T, E> Clone for Command<T, E>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, E: Debug> Debug for Command<T, E>

Source§

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

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

impl<T, E> Default for Command<T, E>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T, E> !RefUnwindSafe for Command<T, E>

§

impl<T, E> !Send for Command<T, E>

§

impl<T, E> !Sync for Command<T, E>

§

impl<T, E> !UnwindSafe for Command<T, E>

§

impl<T, E> Forget for Command<T, E>

§

impl<T, E> Freeze for Command<T, E>
where *const RcInner<RefCell<State<T, E>>> is !null: Freeze, PhantomData<RcInner<RefCell<State<T, E>>>>: Freeze, *const u8 is !null: Freeze, PhantomData<String>: Freeze,

§

impl<T, E> Move for Command<T, E>

§

impl<T, E> Unpin for Command<T, E>
where Rc<RefCell<State<T, E>>>: Unpin, *const u8 is !null: Unpin,

§

impl<T, E> UnsafeUnpin for Command<T, E>
where *const RcInner<RefCell<State<T, E>>> is !null: UnsafeUnpin, PhantomData<RcInner<RefCell<State<T, E>>>>: UnsafeUnpin, *const u8 is !null: UnsafeUnpin, PhantomData<String>: UnsafeUnpin,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

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

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromRef<S> for S

Source§

fn from_ref(state: &S) -> &S

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<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

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

Source§

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

Source§

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

Source§

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.