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,
impl<T> Promise<T>where
T: Send + 'static,
Sourcepub fn new() -> (Sender<T>, Promise<T>)
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.
Sourcepub fn from_ready(value: T) -> Promise<T>
pub fn from_ready(value: T) -> Promise<T>
Create a promise that already has the result.
Sourcepub fn spawn_thread<F>(thread_name: impl Into<String>, f: F) -> Promise<T>
pub fn spawn_thread<F>(thread_name: impl Into<String>, f: F) -> Promise<T>
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());Sourcepub fn try_take(self) -> Result<T, Promise<T>>
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.
Sourcepub fn block_until_ready(&self) -> &T
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.
Sourcepub fn block_until_ready_mut(&mut self) -> &mut T
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.
Sourcepub fn block_and_take(self) -> T
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.
Sourcepub fn poll(&self) -> Poll<&T>
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.
Sourcepub fn poll_mut(&mut self) -> Poll<&mut T>
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.