Skip to main content

Resource

Struct Resource 

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

A value loaded on a background thread from inputs read on the main thread. When the signals source reads change, a new load starts and the one running is cancelled: only the newest result is ever shown, so search-as-you-type can’t show an older query’s rows. The last value stays readable while a new one loads.

A Copy handle, owned like a Signal: when its owner goes, the running load is cancelled and its result dropped.

Implementations§

Source§

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

Source

pub fn new<S, E>( source: impl Fn() -> S + 'static, fetch: impl Fn(S, &Cancel) -> Result<T, E> + Send + Sync + 'static, ) -> Self
where S: Send + 'static, E: Display,

Loads with fetch(source()) on a background thread, again whenever the signals source reads change. Call from the main thread.

use ferrokit::*;

let query = Signal::new(String::new());
let matches = Resource::new(move || query.get(), |q: String, cancel: &Cancel| {
    let words = std::fs::read_to_string("/usr/share/dict/words").map_err(|e| e.to_string())?;
    let mut found = Vec::new();
    for w in words.lines().filter(|w| w.starts_with(&q)) {
        if cancel.is_cancelled() {
            break; // a newer query started; this result won't be shown
        }
        found.push(w.to_string());
    }
    Ok::<_, String>(found)
});
let status = text(move || match matches.state() {
    Load::Loading => "Searching…".into(),
    Load::Ready(words) => format!("{} words", words.len()),
    Load::Failed(e) => format!("Couldn't search: {e}"),
});
Source§

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

Source

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

The latest loaded value (kept while a newer load runs), subscribing the running effect. None until the first load succeeds.

Source

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

The latest value, without subscribing (for event handlers).

Source

pub fn with<R>(&self, f: impl FnOnce(Option<&T>) -> R) -> R

Reads the latest value through f without copying it.

Source

pub fn loading(&self) -> bool

Whether a load is running.

Source

pub fn error(&self) -> Option<String>

The error of the latest finished load, if it failed.

Source

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

Loading, the latest value, or the latest error: whichever the most recent finished load produced (Loading only before the first).

Source

pub fn refetch(&self)

Loads again with the same inputs (the running load is cancelled).

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

impl<T> Copy for Resource<T>

Auto Trait Implementations§

§

impl<T> !Send for Resource<T>

§

impl<T> !Sync for Resource<T>

§

impl<T> Forget for Resource<T>

§

impl<T> Freeze for Resource<T>
where Signal<Option<T>>: Freeze,

§

impl<T> Move for Resource<T>

§

impl<T> RefUnwindSafe for Resource<T>

§

impl<T> Unpin for Resource<T>
where Signal<Option<T>>: Unpin,

§

impl<T> UnsafeUnpin for Resource<T>

§

impl<T> UnwindSafe 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> AutoreleaseSafe for T
where T: ?Sized,

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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.