use io_uring::{opcode, types}; use std::fmt; use std::io::{self, Error}; use std::os::fd::OwnedFd;
/// Trait for buffers that can be used with io-uring read operations. pub(crate) trait ReadBuffer: Send + 'static { /// Prepare the buffer for a read operation. /// Returns a pointer and length for the io-uring SQE. fn uring_read_prepare(&mutself, max_len: usize) -> (*mut u8, u32);
/// Complete a read of `n` bytes. /// /// # Safety /// /// The caller must ensure the kernel wrote exactly `n` bytes /// into the buffer at the pointer returned by `uring_read_prepare`. unsafefn uring_read_complete(&mutself, n: u32);
}
impl ReadBuffer for Vec<u8> { fn uring_read_prepare(&mutself, max_len: usize) -> (*mut u8, u32) {
assert!(self.spare_capacity_mut().len() >= max_len); let ptr = self.spare_capacity_mut().as_mut_ptr().cast();
(ptr, max_len as u32)
}
unsafefn uring_read_complete(&mutself, n: u32) { // SAFETY: the kernel wrote `n` bytes into spare capacity starting // at the old self.len(), so self.len() + n bytes are now initialized. unsafe { self.set_len(self.len() + n as usize) };
}
}
impl<B, F> Op<Read<B, F>> where
B: ReadBuffer + fmt::Debug,
F: UringFd,
Read<B, F>: Cancellable,
{ /// Submit a read operation via io-uring. /// /// `max_len` is the maximum number of bytes to read. /// `offset` is the file offset; use `u64::MAX` for the current cursor. pub(crate) fn read_at(fd: F, mut buf: B, max_len: usize, offset: u64) -> Self { let (ptr, len) = buf.uring_read_prepare(max_len);
let sqe = opcode::Read::new(types::Fd(UringFd::as_raw_fd(&fd)), ptr, len)
.offset(offset)
.build();
// SAFETY: `fd` and `buf`, which owns the heap buffer, are moved into `Read`, // which is held by the `Op` for the entire duration of the io-uring operation. // The buffer pointer remains valid because Vec heap data doesn't move. unsafe { Op::new(sqe, Read { fd, buf }) }
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.17 Sekunden
(vorverarbeitet am 2026-08-25)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.