Enum Async

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

Fields

§value: Option<T>
§

Success

The operation completed successfully with a result value.

Fields

§value: T
§

Fail

The operation failed. Contains an error and optionally the previous value.

Fields

§value: Option<T>

Implementations§

Source§

impl<T: Clone> Async<T>

Source

pub fn is_complete(&self) -> bool

Returns true if the operation has completed (either successfully or with an error).

Source

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.

Source

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.

Source

pub fn is_uninitialized(&self) -> bool

Returns true if the operation has not been started.

Source

pub fn is_loading(&self) -> bool

Returns true if the operation is currently in progress.

Source

pub fn is_success(&self) -> bool

Returns true if the operation completed successfully.

Source

pub fn is_fail(&self) -> bool

Returns true if the operation failed.

Source

pub fn is_fail_with_error(&self) -> bool

Returns true if the operation failed with a general error.

Source

pub fn is_fail_with_none(&self) -> bool

Returns true if the operation failed because it returned None.

Source

pub fn is_fail_with_canceled(&self) -> bool

Returns true if the operation failed because it was cancelled.

Source

pub fn is_fail_with_timeout(&self) -> bool

Returns true if the operation failed because it timed out.

Source

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 returns Some(value)
  • Loading variant with a retained value returns Some(value)
  • Fail variant with a retained value returns Some(value)
  • Otherwise returns None
Source

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.

Source

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.

Source

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.

Source

pub fn loading(value: Option<T>) -> Self

Creates a new Async in the Loading state.

Optionally includes a retained value from a previous operation.

Source

pub fn success(value: T) -> Self

Creates a new Async in the Success state with the provided value.

Source

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.

Source

pub fn fail_with_cancelled(value: Option<T>) -> Self

Creates a new Async in the Fail state with a cancellation error.

Source

pub fn fail_with_timeout(value: Option<T>) -> Self

Creates a new Async in the Fail state with a timeout error.

Source

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.

Source

pub fn fail_with_none(value: Option<T>) -> Self

Creates a new Async in the Fail state with a None error.

Trait Implementations§

Source§

impl<T: Clone + Clone> Clone for Async<T>

Source§

fn clone(&self) -> Async<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Clone> Debug for Async<T>

Source§

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

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

impl<T: Clone> Default for Async<T>

Source§

fn default() -> Async<T>

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

impl<T: Hash + Clone> Hash for Async<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: PartialEq + Clone> PartialEq for Async<T>

Source§

fn eq(&self, other: &Async<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq + Clone> Eq for Async<T>

Source§

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> 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> 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> ExecutionResult<T> for T
where T: Clone,

Source§

fn into_async(self) -> Async<T>

Converts the implementor into an Async<T> representation. 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> 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 = 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.