pub struct Task<T, E = String> { /* private fields */ }Expand description
Observable state of a background operation: a Signal holding a
TaskState, plus a generation counter that keeps stale workers from
overwriting newer results.
start moves to Loading and returns a TaskHandle for
the worker; refresh does the same but keeps the last
result visible. Starting again, reset, or setting a result
directly retires every earlier handle, whose later reports are ignored.
For async work, TaskHandle::run wraps a future that you spawn on any
executor; dropping that future cancels the run.
let thumbnails: Task<Arc<Vec<Image>>> = Task::named("thumbnails");
let handle = thumbnails.start();
std::thread::spawn(move || {
for (index, path) in paths.iter().enumerate() {
handle.set_progress(index as f32 / paths.len() as f32);
// ...
}
handle.complete(load_all(&paths).map(Arc::new));
});Writes need no Clone or PartialEq on T or E. Observers run on the
reporting thread, as with any Signal. Wrap large results in Arc so
state stays cheap, or read with with.
Implementations§
Source§impl<T, E> Task<T, E>
impl<T, E> Task<T, E>
pub fn new() -> Task<T, E>
pub fn named(name: impl Into<Arc<str>>) -> Task<T, E>
Sourcepub fn start(&self) -> TaskHandle<T, E>
pub fn start(&self) -> TaskHandle<T, E>
Enter Loading with indeterminate progress and return the handle the
worker reports through. Retires earlier handles and drops any result.
Sourcepub fn refresh(&self) -> TaskHandle<T, E>
pub fn refresh(&self) -> TaskHandle<T, E>
Like start, but keep the current result as
Loading { previous } so the UI can show it until the new one lands.
Cancelling the run restores it as Ready.
Sourcepub fn finish(&self, value: T) -> bool
pub fn finish(&self, value: T) -> bool
Store a result directly, retiring every outstanding handle.
Sourcepub fn fail(&self, error: E) -> bool
pub fn fail(&self, error: E) -> bool
Store a failure directly, retiring every outstanding handle.
pub fn complete(&self, result: Result<T, E>) -> bool
pub fn state(&self) -> TaskState<T, E>
Sourcepub fn with<R>(&self, read: impl FnOnce(&TaskState<T, E>) -> R) -> R
pub fn with<R>(&self, read: impl FnOnce(&TaskState<T, E>) -> R) -> R
Read the state by reference without cloning a result.
pub fn is_loading(&self) -> bool
pub fn progress(&self) -> Option<f32>
Sourcepub fn signal(&self) -> &Signal<TaskState<T, E>>
pub fn signal(&self) -> &Signal<TaskState<T, E>>
The underlying signal, for APIs that take a Signal.
pub fn source_id(&self) -> SourceId
pub fn version(&self) -> u64
pub fn subscribe(&self, observer: Observer) -> Subscription
pub fn select<U>( &self, select: impl Fn(&TaskState<T, E>) -> U + Send + Sync + 'static, ) -> Selector<Signal<TaskState<T, E>>, TaskState<T, E>, U>
pub fn select_named<U>( &self, name: impl Into<Arc<str>>, select: impl Fn(&TaskState<T, E>) -> U + Send + Sync + 'static, ) -> Selector<Signal<TaskState<T, E>>, TaskState<T, E>, U>
Trait Implementations§
Source§impl<T, E> Observable<TaskState<T, E>> for Task<T, E>
impl<T, E> Observable<TaskState<T, E>> for Task<T, E>
fn source_id(&self) -> SourceId
fn source_name(&self) -> Arc<str> ⓘ
fn get(&self) -> TaskState<T, E>
Source§fn read(&self, reader: &mut dyn FnMut(&TaskState<T, E>))
fn read(&self, reader: &mut dyn FnMut(&TaskState<T, E>))
reader once with a borrow of the current value. Read moreSource§fn value_version(&self) -> Option<u64>
fn value_version(&self) -> Option<u64>
None if unknown. Read morefn subscribe(&self, observer: Observer) -> Subscription
Auto Trait Implementations§
impl<T, E> Forget for Task<T, E>
impl<T, E> Freeze for Task<T, E>
impl<T, E> Move for Task<T, E>
impl<T, E> RefUnwindSafe for Task<T, E>
impl<T, E> Send for Task<T, E>
impl<T, E> Sync for Task<T, E>
impl<T, E> Unpin for Task<T, E>
impl<T, E> UnsafeUnpin for Task<T, E>
impl<T, E> UnwindSafe for Task<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> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.