Skip to main content

Promise

Struct Promise 

Source
pub struct Promise<T>
where T: Send + 'static,
{ /* private fields */ }
Expand description

A promise that waits for the reception of a single value, presumably from some async task.

A Promise starts out waiting for a value. Each time you call a member method it will check if that value is ready. Once ready, the Promise will store the value until you drop the Promise.

Example:

let promise = Promise::spawn_thread("slow_operation", move || something_slow());

// Then in the game loop or immediate mode GUI code:
if let Some(result) = promise.ready() {
    // Use/show result
} else {
    // Show a loading screen
}

If you enable the tokio feature you can use poll-promise with the tokio runtime to run async tasks using [Promise::spawn_async], [Promise::spawn_local], and [Promise::spawn_blocking].

Implementations§

Source§

impl<T> Promise<T>
where T: Send + 'static,

Source

pub fn new() -> (Sender<T>, Promise<T>)

Create a Promise and a corresponding Sender.

Put the promised value into the sender when it is ready. If you drop the Sender without putting a value into it, it will cause a panic when polling the Promise.

See also [Self::spawn_blocking], [Self::spawn_async], [Self::spawn_local], and Self::spawn_thread.

Source

pub fn from_ready(value: T) -> Promise<T>

Create a promise that already has the result.

Source

pub fn spawn_thread<F>(thread_name: impl Into<String>, f: F) -> Promise<T>
where F: FnOnce() -> T + Send + 'static,

Spawn a blocking closure in a background thread.

The first argument is the name of the thread you spawn, passed to std::thread::Builder::name. It shows up in panic messages.

This is a convenience method, using Self::new and std::thread::Builder.

If you are compiling with the “tokio” or “web” features, you should use [Self::spawn_blocking] or [Self::spawn_async] instead.

let promise = Promise::spawn_thread("slow_operation", move || something_slow());
Source

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

Polls the promise and either returns a reference to the data, or None if still pending.

Panics if the connected Sender was dropped before a value was sent.

Source

pub fn ready_mut(&mut self) -> Option<&mut T>

Polls the promise and either returns a mutable reference to the data, or None if still pending.

Panics if the connected Sender was dropped before a value was sent.

Source

pub fn try_take(self) -> Result<T, Promise<T>>

Returns either the completed promise object or the promise itself if it is not completed yet.

Panics if the connected Sender was dropped before a value was sent.

Source

pub fn block_until_ready(&self) -> &T

Block execution until ready, then returns a reference to the value.

Panics if the connected Sender was dropped before a value was sent.

Source

pub fn block_until_ready_mut(&mut self) -> &mut T

Block execution until ready, then returns a mutable reference to the value.

Panics if the connected Sender was dropped before a value was sent.

Source

pub fn block_and_take(self) -> T

Block execution until ready, then returns the promised value and consumes the Promise.

Panics if the connected Sender was dropped before a value was sent.

Source

pub fn poll(&self) -> Poll<&T>

Returns either a reference to the ready value std::task::Poll::Ready or std::task::Poll::Pending.

Panics if the connected Sender was dropped before a value was sent.

Source

pub fn poll_mut(&mut self) -> Poll<&mut T>

Returns either a mut reference to the ready value in a std::task::Poll::Ready or a std::task::Poll::Pending.

Panics if the connected Sender was dropped before a value was sent.

Source

pub fn task_type(&self) -> TaskType

Returns the type of task this promise is running. See TaskType.

Auto Trait Implementations§

§

impl<T> !Freeze for Promise<T>

§

impl<T> !RefUnwindSafe for Promise<T>

§

impl<T> Send for Promise<T>

§

impl<T> !Sync for Promise<T>

§

impl<T> Unpin for Promise<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Promise<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Promise<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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. 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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more