1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use futures::prelude::*;

use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;

pub struct NextTick {
    started: bool,
    notify: Arc<AtomicBool>
}

impl Future for NextTick {
    type Item = ();
    type Error = !;

    fn poll(
        &mut self
    ) -> Result<Async<()>, !> {
        if self.notify.load(Ordering::Relaxed) == true {
            return Ok(Async::Ready(()));
        }

        if !self.started {
            self.started = true;
            let notify = self.notify.clone();
            let task = ::executor::current_task();

            ::raw::schedule(move || {
                notify.store(true, Ordering::Relaxed);
                ::executor::run_once_next_tick(&task);
            });
        }

        Ok(Async::NotReady)
    }
}

impl NextTick {
    pub fn new() -> NextTick {
        NextTick {
            started: false,
            notify: Arc::new(AtomicBool::new(false))
        }
    }
}