-
-
Save zshipko/b5e91613621b9b71e2f70dc2ca1552d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)), | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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