pub enum LoadingState<T> {
Loading,
Ready(T),
}Expand description
Represents the loading state of an async resource.
Use this as your Query::Output type when the query needs to load
data asynchronously (e.g., file I/O, network requests).
§Example
ⓘ
struct LoadFile { path: PathBuf }
impl Query for LoadFile {
type CacheKey = PathBuf;
type Output = LoadingState<String>;
fn query(&self, ctx: &mut QueryContext) -> Result<Self::Output, QueryError> {
// Check if already loaded
if let Some(content) = ctx.get_loaded(&self.path) {
return Ok(LoadingState::Ready(content));
}
// Spawn background loader
ctx.spawn_loader(self.cache_key(), async {
tokio::fs::read_to_string(&self.path).await.unwrap()
});
Err(QueryError::Suspend)
}
}Variants§
Implementations§
Source§impl<T> LoadingState<T>
impl<T> LoadingState<T>
Sourcepub fn is_loading(&self) -> bool
pub fn is_loading(&self) -> bool
Check if the resource is still loading.
Sourcepub fn into_inner(self) -> Option<T>
pub fn into_inner(self) -> Option<T>
Get the value if ready, None if loading (consuming version).
Sourcepub fn suspend(self) -> Result<T, QueryError>
pub fn suspend(self) -> Result<T, QueryError>
Convert to Result - Loading becomes Err(QueryError::Suspend).
Use this with the ? operator to propagate loading state upward.
§Example
ⓘ
fn query(&self, ctx: &mut QueryContext) -> Result<MyOutput, QueryError> {
let data = ctx.query(LoadData { id: self.id })?.suspend()?;
// `data` is guaranteed to be ready here
Ok(process(data))
}Sourcepub fn suspend_ref(&self) -> Result<&T, QueryError>
pub fn suspend_ref(&self) -> Result<&T, QueryError>
Reference version of suspend.
Sourcepub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> LoadingState<U>
pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> LoadingState<U>
Map the inner value if ready.
Sourcepub fn and_then<U, F: FnOnce(T) -> LoadingState<U>>(
self,
f: F,
) -> LoadingState<U>
pub fn and_then<U, F: FnOnce(T) -> LoadingState<U>>( self, f: F, ) -> LoadingState<U>
Flat map the inner value if ready.
Trait Implementations§
Source§impl<T: Clone> Clone for LoadingState<T>
impl<T: Clone> Clone for LoadingState<T>
Source§fn clone(&self) -> LoadingState<T>
fn clone(&self) -> LoadingState<T>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T: Debug> Debug for LoadingState<T>
impl<T: Debug> Debug for LoadingState<T>
Source§impl<T> Default for LoadingState<T>
impl<T> Default for LoadingState<T>
Source§fn default() -> LoadingState<T>
fn default() -> LoadingState<T>
Returns the “default value” for a type. Read more
Source§impl<T> From<Option<T>> for LoadingState<T>
impl<T> From<Option<T>> for LoadingState<T>
Source§impl<T> From<T> for LoadingState<T>
impl<T> From<T> for LoadingState<T>
Source§impl<T: PartialEq> PartialEq for LoadingState<T>
impl<T: PartialEq> PartialEq for LoadingState<T>
impl<T: Eq> Eq for LoadingState<T>
impl<T> StructuralPartialEq for LoadingState<T>
Auto Trait Implementations§
impl<T> Freeze for LoadingState<T>where
T: Freeze,
impl<T> RefUnwindSafe for LoadingState<T>where
T: RefUnwindSafe,
impl<T> Send for LoadingState<T>where
T: Send,
impl<T> Sync for LoadingState<T>where
T: Sync,
impl<T> Unpin for LoadingState<T>where
T: Unpin,
impl<T> UnwindSafe for LoadingState<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
Mutably borrows from an owned value. Read more
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
Compare self to
key and return true if they are equal.