Struct Promise

Source
pub struct Promise { /* private fields */ }
Expand description

Promises act as an async “primitive” that does not carry data.

Implementations§

Source§

impl Promise

Source

pub fn new<F: FnOnce(NotifyHandle) + 'static>(f: F) -> Promise

Examples found in repository?
examples/sleep.rs (lines 13-19)
12fn sleep_ms(c: &mut Yieldable, ms: u64) {
13    let p = Promise::new(move |h| {
14        let h = h.into_sendable();
15        thread::spawn(move || {
16            thread::sleep(Duration::from_millis(ms));
17            h.notify();
18        });
19    });
20    c.yield_now(&p);
21}
22
23fn main() {
24    let mut sched = Scheduler::new_default();
25    let state = sched.get_state();
26    let state2 = state.clone();
27
28    sched.run_value_promise_to_end(state.prepare_coroutine(move |c| {
29        let done_count: Rc<Cell<usize>> = Rc::new(Cell::new(0));
30        let done_count_2 = done_count.clone();
31
32        let p = Promise::new(move |handle| {
33            // TODO: Implement Promise::all
34
35            let handle = Rc::new(Cell::new(Some(handle)));
36            let handle2 = handle.clone();
37
38            state2.start_coroutine(move |c| {
39                println!("Begin 1");
40                sleep_ms(c, 500);
41                println!("End 1");
42                done_count.set(done_count.get() + 1);
43                if done_count.get() == 2 {
44                    handle.replace(None).unwrap().notify();
45                }
46            });
47
48            state2.start_coroutine(move |c| {
49                println!("Begin 2");
50                sleep_ms(c, 500);
51                println!("End 2");
52                done_count_2.set(done_count_2.get() + 1);
53                if done_count_2.get() == 2 {
54                    handle2.replace(None).unwrap().notify();
55                }
56            });
57        });
58
59        c.yield_now(&p);
60    })).unwrap();
61}
Source

pub fn new_started() -> Promise

Source

pub fn is_started(&self) -> bool

Source

pub fn build_begin(&self) -> PromiseBegin

Auto Trait Implementations§

§

impl !Freeze for Promise

§

impl !RefUnwindSafe for Promise

§

impl !Send for Promise

§

impl !Sync for Promise

§

impl Unpin for Promise

§

impl !UnwindSafe for Promise

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