Skip to main content

tokio/io/uring/
write.rs

1use crate::runtime::driver::op::{CancelData, Cancellable, Completable, CqeResult, Op};
2use crate::util::as_ref::OwnedBuf;
3
4use io_uring::{opcode, types};
5use std::io::{self, Error};
6use std::os::fd::{AsRawFd, OwnedFd};
7
8#[derive(Debug)]
9pub(crate) struct Write {
10    buf: OwnedBuf,
11    fd: OwnedFd,
12}
13
14impl Completable for Write {
15    type Output = (io::Result<u32>, OwnedBuf, OwnedFd);
16    fn complete(self, cqe: CqeResult) -> Self::Output {
17        (cqe.result, self.buf, self.fd)
18    }
19
20    fn complete_with_error(self, err: Error) -> Self::Output {
21        (Err(err), self.buf, self.fd)
22    }
23}
24
25impl Cancellable for Write {
26    fn cancel(self) -> CancelData {
27        CancelData::Write(self)
28    }
29}
30
31impl Op<Write> {
32    /// Issue a write that starts at `buf_offset` within `buf` and writes some bytes
33    /// into `file` at `file_offset`.
34    pub(crate) fn write_at(
35        fd: OwnedFd,
36        buf: OwnedBuf,
37        buf_offset: usize,
38        file_offset: u64,
39    ) -> io::Result<Self> {
40        // There is a cap on how many bytes we can write in a single uring write operation.
41        // ref: https://github.com/axboe/liburing/discussions/497
42        let len = u32::try_from(buf.as_ref().len() - buf_offset).unwrap_or(u32::MAX);
43
44        let ptr = buf.as_ref()[buf_offset..buf_offset + len as usize].as_ptr();
45
46        let sqe = opcode::Write::new(types::Fd(fd.as_raw_fd()), ptr, len)
47            .offset(file_offset)
48            .build();
49
50        // SAFETY: parameters of the entry, such as `fd` and `buf`, are valid
51        // until this operation completes.
52        let op = unsafe { Op::new(sqe, Write { buf, fd }) };
53        Ok(op)
54    }
55}