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 AsyncWrite {
fn write(&mut self, buf: &[u8]) -> impl Future<Output = io::Result<usize>>;
}
pub(crate) struct AsyncWriter<'a, T: AsFd + Unpin> {
pub(crate) fd: T,
pub(crate) io: ReactorIo,
pub(crate) buf: &'a [u8],
pub(crate) seekable: bool,
}
impl<T: AsFd + Unpin> Future for AsyncWriter<'_, 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::Write::new(
types::Fd(this.fd.as_fd().as_raw_fd()),
this.buf.as_ptr(),
this.buf.len() as _,
)
.offset(if this.seekable { u64::MAX } else { 0 })
.build(),
cx.waker().clone(),
)
})
.map(|x| x.map(|x| x as _))
}
}