use std::{
future::Future,
io,
os::fd::{AsFd, AsRawFd},
pin::Pin,
task::{Context, Poll},
};
use io_uring::{opcode, types};
use crate::reactor::ReactorIo;
pub trait AsyncRead {
fn read(&mut self, buf: &mut [u8]) -> impl Future<Output = io::Result<usize>>;
}
pub(crate) struct AsyncReader<'a, T: AsFd + Unpin> {
pub(crate) fd: T,
pub(crate) io: ReactorIo,
pub(crate) buf: &'a mut [u8],
pub(crate) seekable: bool,
}
impl<T: AsFd + Unpin> Future for AsyncReader<'_, T> {
type Output = io::Result<usize>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = unsafe { self.get_unchecked_mut() };
this.io
.submit_or_get_result(|| {
(
opcode::Read::new(
types::Fd(this.fd.as_fd().as_raw_fd()),
this.buf.as_mut_ptr(),
this.buf.len() as _,
)
.offset(if this.seekable { u64::MAX } else { 0 })
.build(),
cx.waker().clone(),
)
})
.map(|x| x.map(|x| x as _))
}
}