pub enum Async<T: Clone> {
Uninitialized,
Loading {
value: Option<T>,
},
Success {
value: T,
},
Fail {
error: AsyncError,
value: Option<T>,
},
}
Expand description
Represents the state of an asynchronous operation with its possible outcomes.
Async<T>
is a generic enum that encapsulates the different states an asynchronous
operation can be in, including uninitialized, loading, success, and failure states.
It provides a uniform way to represent and handle asynchronous state in a reactive application.
The type parameter T
represents the successful result type of the operation.
Variants§
Uninitialized
The initial state before any operation has been attempted.
Loading
The operation is in progress. May optionally contain the previous value.
Success
The operation completed successfully with a result value.
Fields
value: T
Fail
The operation failed. Contains an error and optionally the previous value.
Implementations§
Source§impl<T: Clone> Async<T>
impl<T: Clone> Async<T>
Sourcepub fn is_complete(&self) -> bool
pub fn is_complete(&self) -> bool
Returns true if the operation has completed (either successfully or with an error).
Sourcepub fn should_load(&self) -> bool
pub fn should_load(&self) -> bool
Returns true if the operation should be (re)loaded.
This is typically true when the state is either uninitialized or in a failed state.
Sourcepub fn is_incomplete(&self) -> bool
pub fn is_incomplete(&self) -> bool
Returns true if the operation has not yet completed.
This is true when the state is either uninitialized or currently loading.
Sourcepub fn is_uninitialized(&self) -> bool
pub fn is_uninitialized(&self) -> bool
Returns true if the operation has not been started.
Sourcepub fn is_loading(&self) -> bool
pub fn is_loading(&self) -> bool
Returns true if the operation is currently in progress.
Sourcepub fn is_success(&self) -> bool
pub fn is_success(&self) -> bool
Returns true if the operation completed successfully.
Sourcepub fn is_fail_with_error(&self) -> bool
pub fn is_fail_with_error(&self) -> bool
Returns true if the operation failed with a general error.
Sourcepub fn is_fail_with_none(&self) -> bool
pub fn is_fail_with_none(&self) -> bool
Returns true if the operation failed because it returned None.
Sourcepub fn is_fail_with_canceled(&self) -> bool
pub fn is_fail_with_canceled(&self) -> bool
Returns true if the operation failed because it was cancelled.
Sourcepub fn is_fail_with_timeout(&self) -> bool
pub fn is_fail_with_timeout(&self) -> bool
Returns true if the operation failed because it timed out.
Sourcepub fn value(self) -> Option<T>
pub fn value(self) -> Option<T>
Consumes the Async
and returns the contained value if available.
This method extracts the value from any variant that might contain it:
Success
variant returnsSome(value)
Loading
variant with a retained value returnsSome(value)
Fail
variant with a retained value returnsSome(value)
- Otherwise returns
None
Sourcepub fn value_ref(&self) -> Option<&T>
pub fn value_ref(&self) -> Option<&T>
Returns a reference to the contained value if available.
Similar to value()
but returns a reference instead of consuming the Async
.
Sourcepub fn value_ref_clone(self: &Async<T>) -> Option<T>
pub fn value_ref_clone(self: &Async<T>) -> Option<T>
Returns a clone of the contained value if available.
This method is similar to value_ref()
but returns a clone of the value
rather than a reference.
Sourcepub fn set_retain_value(self, value: Option<T>) -> Self
pub fn set_retain_value(self, value: Option<T>) -> Self
Sets or updates the retained value in Loading
or Fail
states.
This method is useful when you want to update the retained value without changing the state itself.
Sourcepub fn loading(value: Option<T>) -> Self
pub fn loading(value: Option<T>) -> Self
Creates a new Async
in the Loading
state.
Optionally includes a retained value from a previous operation.
Sourcepub fn success(value: T) -> Self
pub fn success(value: T) -> Self
Creates a new Async
in the Success
state with the provided value.
Sourcepub fn fail(error: AsyncError, value: Option<T>) -> Self
pub fn fail(error: AsyncError, value: Option<T>) -> Self
Creates a new Async
in the Fail
state with the provided error and optional retained value.
Sourcepub fn fail_with_cancelled(value: Option<T>) -> Self
pub fn fail_with_cancelled(value: Option<T>) -> Self
Creates a new Async
in the Fail
state with a cancellation error.
Sourcepub fn fail_with_timeout(value: Option<T>) -> Self
pub fn fail_with_timeout(value: Option<T>) -> Self
Creates a new Async
in the Fail
state with a timeout error.
Sourcepub fn fail_with_message(message: impl Into<String>, value: Option<T>) -> Self
pub fn fail_with_message(message: impl Into<String>, value: Option<T>) -> Self
Creates a new Async
in the Fail
state with a general error message.
Sourcepub fn fail_with_none(value: Option<T>) -> Self
pub fn fail_with_none(value: Option<T>) -> Self
Creates a new Async
in the Fail
state with a None error.
Trait Implementations§
impl<T: Eq + Clone> Eq for Async<T>
impl<T: Clone> StructuralPartialEq for Async<T>
Auto Trait Implementations§
impl<T> Freeze for Async<T>where
T: Freeze,
impl<T> RefUnwindSafe for Async<T>where
T: RefUnwindSafe,
impl<T> Send for Async<T>where
T: Send,
impl<T> Sync for Async<T>where
T: Sync,
impl<T> Unpin for Async<T>where
T: Unpin,
impl<T> UnwindSafe for Async<T>where
T: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> ExecutionResult<T> for Twhere
T: Clone,
impl<T> ExecutionResult<T> for Twhere
T: Clone,
Source§fn into_async(self) -> Async<T>
fn into_async(self) -> Async<T>
Async<T>
representation. Read more