Skip to content

Instantly share code, notes, and snippets.

@SethDusek
Last active June 16, 2019 17:18
Show Gist options
  • Save SethDusek/c9d7f6e4dc5e79b623282d48dbf50ed9 to your computer and use it in GitHub Desktop.
Save SethDusek/c9d7f6e4dc5e79b623282d48dbf50ed9 to your computer and use it in GitHub Desktop.
#![feature(await_macro)]
#![feature(futures_api)]
#![feature(async_await)]
use std::time::{Instant, Duration};
use std::future::Future;
use std::task::{Waker, Context, Poll};
use std::pin::Pin;
static VTABLE: std::task::RawWakerVTable = std::task::RawWakerVTable {
wake: wake as unsafe fn(*const ()),
drop: drop as unsafe fn(*const ()),
clone: clone
};
fn wake(_a: *const ()) {
println!("I was woken up!");
}
fn drop(_a: *const ()) {
println!("Im being dropped!");
}
fn clone(_a: *const ()) -> std::task::RawWaker {
unimplemented!()
}
struct Timer {
start: Instant,
time: Duration
}
impl Future for Timer {
type Output = ();
fn poll(self: Pin<&mut Self>, lw: &Waker) -> Poll<()> {
if self.start.elapsed() >= self.time {
Poll::Ready(())
}
else { lw.wake(); Poll::Pending }
}
}
async fn sleep_something() {
Timer::new(1).await
}
fn main() {
let mut fut = sleep_something();
let mut context = Context::from_waker(unsafe { Waker::from_raw(std::task::RawWaker::new(std::ptr::null(), &VTABLE)) });
while let Poll::Pending = Pin::new(&mut fut).poll(&mut context) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment