use super::{ocall, ResourceId};
use std::{
future::Future,
pin::Pin,
task::{Context, Poll},
};
pub struct Receiver {
res_id: ResourceId,
}
pub struct RxNext<'a> {
ch: &'a Receiver,
}
impl Receiver {
pub const fn new(res_id: ResourceId) -> Self {
Self { res_id }
}
pub fn next(&self) -> RxNext {
RxNext { ch: self }
}
}
impl Future for RxNext<'_> {
type Output = Option<Vec<u8>>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
use crate::env::OcallError;
let waker_id = crate::env::tasks::intern_waker(cx.waker().clone());
match ocall::poll(waker_id, self.ch.res_id.0) {
Ok(msg) => Poll::Ready(Some(msg)),
Err(OcallError::EndOfFile) => Poll::Ready(None), Err(OcallError::Pending) => Poll::Pending,
Err(err) => panic!("unexpected error: {:?}", err),
}
}
}
pub fn input_messages() -> &'static Receiver {
static MSG_RX: Receiver = Receiver::new(ResourceId(0));
&MSG_RX
}