use crate::BufResult;
use crate::buf::BoundedBufMut;
use crate::io::SharedFd;
use crate::runtime::CONTEXT;
use crate::runtime::driver::op::{Completable, CqeResult, Op};
use std::io;
pub(crate) struct Read<T> {
#[allow(dead_code)]
fd: SharedFd,
pub(crate) buf: T,
}
impl<T: BoundedBufMut> Op<Read<T>> {
pub(crate) fn read_at(fd: &SharedFd, buf: T, offset: u64) -> io::Result<Op<Read<T>>> {
use io_uring::{opcode, types};
CONTEXT.with(|x| {
x.handle()
.expect("Not in a runtime context")
.submit_op(Read { fd: fd.clone(), buf }, |read| {
let ptr = read.buf.stable_mut_ptr();
let len = read.buf.bytes_total();
opcode::Read::new(types::Fd(fd.raw_fd()), ptr, len as _)
.offset(offset as _)
.build()
})
})
}
}
impl<T> Completable for Read<T>
where
T: BoundedBufMut,
{
type Output = BufResult<usize, T>;
fn complete(self, cqe: CqeResult) -> Self::Output {
let res = cqe.result.map(|v| v as usize);
let mut buf = self.buf;
if let Ok(n) = res {
unsafe {
buf.set_init(n);
}
}
(res, buf)
}
}