pub enum DataResource<T> {
Empty,
Loading,
Loaded(T),
Failed(String),
}Expand description
Represents the lifecycle of async-loaded data.
This type captures the four states data can be in:
Empty: No data requested yetLoading: Data is being fetchedLoaded(T): Data successfully loadedFailed(String): Loading failed with error message
Variants§
Empty
No data requested yet
Loading
Data is being fetched
Loaded(T)
Data successfully loaded
Failed(String)
Loading failed with error message
Implementations§
Source§impl<T> DataResource<T>
impl<T> DataResource<T>
Sourcepub fn is_loading(&self) -> bool
pub fn is_loading(&self) -> bool
Returns true if this is Loading.
Sourcepub fn data(&self) -> Option<&T>
pub fn data(&self) -> Option<&T>
Returns a reference to the loaded data, or None if not loaded.
Sourcepub fn data_mut(&mut self) -> Option<&mut T>
pub fn data_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the loaded data, or None if not loaded.
Sourcepub fn map<U>(self, f: impl FnOnce(T) -> U) -> DataResource<U>
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> DataResource<U>
Maps the loaded value using the provided function.
If Loaded(t), returns Loaded(f(t)). Otherwise returns the same state.
Sourcepub fn map_ref<U>(&self, f: impl FnOnce(&T) -> U) -> DataResource<U>
pub fn map_ref<U>(&self, f: impl FnOnce(&T) -> U) -> DataResource<U>
Maps a reference to the loaded value.
Sourcepub fn and_then<U>(
self,
f: impl FnOnce(T) -> DataResource<U>,
) -> DataResource<U>
pub fn and_then<U>( self, f: impl FnOnce(T) -> DataResource<U>, ) -> DataResource<U>
Applies a function that returns a DataResource to the loaded value.
Useful for chaining dependent async operations.
Sourcepub fn unwrap_or_else(self, f: impl FnOnce() -> T) -> T
pub fn unwrap_or_else(self, f: impl FnOnce() -> T) -> T
Returns the loaded value or computes a default.
Sourcepub fn as_ref(&self) -> DataResource<&T>
pub fn as_ref(&self) -> DataResource<&T>
Converts from &DataResource<T> to DataResource<&T>.
Sourcepub fn is_settled(&self) -> bool
pub fn is_settled(&self) -> bool
Returns true if there’s either data or an error (not empty or loading).
Sourcepub fn is_pending(&self) -> bool
pub fn is_pending(&self) -> bool
Returns true if there’s no data yet (empty or loading).
Sourcepub fn start_loading(&mut self) -> boolwhere
T: Clone,
pub fn start_loading(&mut self) -> boolwhere
T: Clone,
Transitions to Loading state. Returns true if state actually changed.
Useful in reducers to start a fetch:
if resource.start_loading() {
// dispatch effect to fetch
}Trait Implementations§
Source§impl<T: Clone> Clone for DataResource<T>
impl<T: Clone> Clone for DataResource<T>
Source§fn clone(&self) -> DataResource<T>
fn clone(&self) -> DataResource<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Debug> Debug for DataResource<T>
impl<T: Debug> Debug for DataResource<T>
Source§impl<T> Default for DataResource<T>
impl<T> Default for DataResource<T>
Source§fn default() -> DataResource<T>
fn default() -> DataResource<T>
Source§impl<T: Hash> Hash for DataResource<T>
impl<T: Hash> Hash for DataResource<T>
Source§impl<T: PartialEq> PartialEq for DataResource<T>
impl<T: PartialEq> PartialEq for DataResource<T>
impl<T: Eq> Eq for DataResource<T>
impl<T> StructuralPartialEq for DataResource<T>
Auto Trait Implementations§
impl<T> Freeze for DataResource<T>where
T: Freeze,
impl<T> RefUnwindSafe for DataResource<T>where
T: RefUnwindSafe,
impl<T> Send for DataResource<T>where
T: Send,
impl<T> Sync for DataResource<T>where
T: Sync,
impl<T> Unpin for DataResource<T>where
T: Unpin,
impl<T> UnsafeUnpin for DataResource<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for DataResource<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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.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