diff --git a/src/fs/file.rs b/src/fs/file.rs index 733bc655..64a6a0d2 100644 --- a/src/fs/file.rs +++ b/src/fs/file.rs @@ -1,6 +1,7 @@ use crate::buf::fixed::FixedBuf; use crate::buf::{BoundedBuf, BoundedBufMut, IoBuf, IoBufMut, Slice}; use crate::fs::OpenOptions; +use crate::io::cmd::UnsubmittedCmd; use crate::io::SharedFd; use crate::runtime::driver::op::{Op, Submit}; @@ -11,6 +12,7 @@ use std::fmt; use std::io; use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; use std::path::Path; +use std::pin::Pin; /// A reference to an open file on the filesystem. /// @@ -879,6 +881,11 @@ impl File { pub async fn close(mut self) -> io::Result<()> { self.fd.close().await } + + #[allow(missing_docs)] + pub fn cmd(&self, op: u32, cmd: [u8; 16], data: Pin>) -> UnsubmittedCmd { + UnsubmittedCmd::cmd(&self.fd, op, cmd, data) + } } impl FromRawFd for File { diff --git a/src/io/cmd.rs b/src/io/cmd.rs new file mode 100644 index 00000000..d8445efe --- /dev/null +++ b/src/io/cmd.rs @@ -0,0 +1,45 @@ +use std::marker::PhantomData; +use std::pin::Pin; + +use io_uring::cqueue::Entry; +use io_uring::{opcode, types}; + +use crate::io::SharedFd; + +use crate::{OneshotOutputTransform, UnsubmittedOneshot}; + +/// An unsubmitted read operation. +pub type UnsubmittedCmd = UnsubmittedOneshot, CmdTransform>; + +impl UnsubmittedCmd { + pub(crate) fn cmd(fd: &SharedFd, op: u32, cmd: [u8; 16], data: Pin>) -> Self { + Self::new( + CmdData { data }, + CmdTransform { + _phantom: PhantomData, + }, + opcode::UringCmd16::new(types::Fd(fd.raw_fd()), op) + .cmd(cmd) + .build(), + ) + } +} + +#[allow(missing_docs)] +pub struct CmdData { + data: Pin>, +} + +#[allow(missing_docs)] +pub struct CmdTransform { + _phantom: PhantomData, +} + +impl OneshotOutputTransform for CmdTransform { + type Output = (i32, Pin>); + type StoredData = CmdData; + + fn transform_oneshot_output(self, data: Self::StoredData, cqe: Entry) -> Self::Output { + (cqe.result(), data.data) + } +} diff --git a/src/io/mod.rs b/src/io/mod.rs index b4e8dd2a..07fd944b 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -56,3 +56,5 @@ pub(crate) mod writev; mod writev_all; pub(crate) use writev_all::writev_at_all; + +pub(crate) mod cmd; diff --git a/src/lib.rs b/src/lib.rs index 5d5417fd..f1177f8e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -79,6 +79,7 @@ pub mod buf; pub mod fs; pub mod net; +pub use io::cmd::*; pub use io::read::*; pub use io::readv::*; pub use io::write::*;