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>
impl<T: MaybeSend + 'static, E: MaybeSend + 'static> Command<T, E>
Sourcepub fn refreshes(self, key: impl Into<String>) -> Self
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.
Sourcepub fn run(
&self,
future: impl Future<Output = Result<T, E>> + MaybeSend + 'static,
)
pub fn run( &self, future: impl Future<Output = Result<T, E>> + MaybeSend + 'static, )
Runs future, forgetting how the last run ended.
Sourcepub fn is_pending(&self) -> bool
pub fn is_pending(&self) -> bool
Whether a run is in progress.
Sourcepub fn result(&self) -> Option<Ref<'_, Result<T, E>>>
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.
Sourcepub fn output(&self) -> Option<Ref<'_, T>>
pub fn output(&self) -> Option<Ref<'_, T>>
What the last run returned, if it succeeded. See result.
Sourcepub fn take_result(&self) -> Option<Result<T, E>>
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§
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>
impl<T, E> Move for Command<T, E>
impl<T, E> Unpin for Command<T, E>
impl<T, E> UnsafeUnpin for Command<T, E>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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