pub struct Promise<T: 'static = ()>(/* private fields */);
Expand description
Represents an ongoing operation which produces a result.
Implementations§
Source§impl<T: Send + Sync + 'static> Promise<T>
impl<T: Send + Sync + 'static> Promise<T>
Sourcepub fn new() -> (Self, impl FnOnce(T) + Send)
pub fn new() -> (Self, impl FnOnce(T) + Send)
Creates a new lightweight promise and an associated resolve function.
§Example
let (promise, resolve) = Promise::<i32>::new();
Task::spawn(|| {
Task::delay(Duration::from_secs(1));
resolve(10);
})
.unwrap();
println!(
"n = {}",
select! {
n = promise.done() => n,
}
);
Sourcepub fn done(&self) -> impl Selectable<&T> + '_
pub fn done(&self) -> impl Selectable<&T> + '_
A Selectable
event which occurs when the promise is resolved.
Sourcepub fn spawn(f: impl FnOnce() -> T + Send + 'static) -> Self
pub fn spawn(f: impl FnOnce() -> T + Send + 'static) -> Self
Spawns a task to run the given function and returns a Promise
that
resolves with the result when it returns. Panics on failure; see
Promise::try_spawn()
.
Sourcepub fn try_spawn(f: impl FnOnce() -> T + Send + 'static) -> Result<Self, Error>
pub fn try_spawn(f: impl FnOnce() -> T + Send + 'static) -> Result<Self, Error>
Spawns a task to run the given function and returns a Promise
that
resolves with the result when it returns.
Sourcepub fn then<U: Send + Sync + 'static>(
&self,
f: impl FnOnce(&T) -> U + Send + 'static,
) -> Promise<U>
pub fn then<U: Send + Sync + 'static>( &self, f: impl FnOnce(&T) -> U + Send + 'static, ) -> Promise<U>
Spawns a new promise which, upon the completion of self
, runs f
.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Promise<T>
impl<T = ()> !RefUnwindSafe for Promise<T>
impl<T> Send for Promise<T>where
T: Send,
impl<T> Sync for Promise<T>where
T: Send,
impl<T> Unpin for Promise<T>
impl<T = ()> !UnwindSafe for Promise<T>
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