Skip to main content

Resource

Struct Resource 

Source
pub struct Resource<T> { /* private fields */ }
Expand description

A reactive handle to an async-loaded value.

Clone is cheap (internally Rc-backed via Signal); all clones share the same underlying state, so providing a Resource to a subtree and updating it from a task updates every consumer.

Implementations§

Source§

impl<T: Clone + 'static> Resource<T>

Source

pub fn new(loader: impl FnOnce() -> Result<T, String>) -> Self

Creates a resource in the Loading state, then runs loader synchronously and stores the result. Useful for “load on creation” cases where the loader is already resolved (e.g. a cached value or a blocking fetch driven by the caller’s executor).

Source

pub fn ready(value: T) -> Self

Creates a resource already in the Ready state.

Source

pub fn error(message: impl Into<String>) -> Self

Creates a resource already in the Error state.

Source

pub fn load_blocking(&self, loader: impl FnOnce() -> Result<T, String>)

Runs loader and updates the shared state to Ready/Error. Safe to call from any thread/task that can reach this Resource (e.g. the continuation of an awaited future).

Source

pub fn set_result(&self, result: Result<T, String>)

Updates the state directly (e.g. from an already-resolved future).

Async callers should prefer Resource::load_async, whose spawn closure uses Resource::is_current to commit a result only if it hasn’t been superseded by a newer load — that is the cancellation guard.

Source

pub fn generation(&self) -> u64

The current generation counter. Incremented on every reload/async (re)load, so async tasks can detect that they’ve been superseded.

Source

pub fn is_current(&self, gen: u64) -> bool

True only if the resource is still on generation gen — i.e. no newer reload/load_async has superseded the load that started at gen. A task that captured gen = resource.generation() before awaiting its future checks resource.is_current(gen) after the await: if false, a newer load has started and the result must be dropped (cancellation).

Source

pub fn load_async<F, Fut>(&self, spawn: F, loader: Fut)
where F: FnOnce(Resource<T>, u64, Fut), Fut: Future<Output = Result<T, String>> + 'static,

Spawns an async loader and bridges its result back into this resource.

The core is runtime-free, so the caller supplies a spawn function that drives a Future<Output = Result<T, String>> to completion on whatever executor the backend provides (the DOM uses wasm_bindgen_futures, a native app uses tokio/smol, tests use a oneshot channel). spawn is given the future plus a clone of this Resource and the generation it started with; once the future resolves the result is committed only if the resource is still on that generation (cancellation on reload).

The resource is immediately put into the Loading state and its generation is bumped so any in-flight load from a previous generation is invalidated. Calling load_async again on the same resource (or any clone) cancels the prior in-flight future.

Source

pub fn reload(&self)

Resets the resource back to Loading (e.g. to re-trigger a fetch).

Bumps the generation so any in-flight async load is cancelled (its eventual result will be discarded by Resource::is_current).

Source

pub fn state(&self) -> ResourceState<T>

The current state.

Source

pub fn ready_value(&self) -> Option<T>

Convenience accessor: the ready value, or None while loading/errored.

Source

pub fn is_loading(&self) -> bool

true while the loader has not completed.

Source

pub fn is_ready(&self) -> bool

true once the loader has succeeded.

Source

pub fn is_error(&self) -> bool

true if the loader failed.

Source

pub fn signal(&self) -> Signal<ResourceState<T>>

The underlying state signal, so views can subscribe to changes.

Trait Implementations§

Source§

impl<T> Clone for Resource<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Resource<T>

§

impl<T> !Send for Resource<T>

§

impl<T> !Sync for Resource<T>

§

impl<T> !UnwindSafe for Resource<T>

§

impl<T> Freeze for Resource<T>

§

impl<T> Unpin for Resource<T>

§

impl<T> UnsafeUnpin for Resource<T>

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