Skip to content

Instantly share code, notes, and snippets.

@zshipko

zshipko/lwt.rs Secret

Created February 13, 2023 14:43
Show Gist options
  • Save zshipko/b5e91613621b9b71e2f70dc2ca1552d3 to your computer and use it in GitHub Desktop.
Save zshipko/b5e91613621b9b71e2f70dc2ca1552d3 to your computer and use it in GitHub Desktop.
ocaml::import! {
fn lwt_poll(x: ocaml::Value) -> Option<ocaml::Value>;
}
pub struct Lwt<'a, T> {
value: ocaml::Value,
rt: &'a ocaml::Runtime,
_t: std::marker::PhantomData<T>,
}
impl<'a, T: ocaml::FromValue + Unpin> std::future::Future for Lwt<'a, T> {
type Output = Result<T, ocaml::Error>;
fn poll(
self: std::pin::Pin<&mut Self>,
_ctx: &mut std::task::Context,
) -> std::task::Poll<Self::Output> {
let p = std::pin::Pin::get_mut(self);
let x = unsafe { lwt_poll(&p.rt, p.value.clone()) };
match x {
Ok(Some(x)) => std::task::Poll::Ready(Ok(ocaml::FromValue::from_value(x))),
Ok(None) => std::task::Poll::Pending,
Err(e) => std::task::Poll::Ready(Err(e)),
}
}
}
let () =
Callback.register "lwt_poll" (Lwt.poll
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment