Skip to main content

tokio/io/uring/
utils.rs

1use std::os::fd::{AsRawFd, OwnedFd, RawFd};
2use std::os::unix::ffi::OsStrExt;
3use std::sync::Arc;
4use std::{ffi::CString, io, path::Path};
5
6pub(crate) type ArcFd = Arc<dyn AsRawFd + Send + Sync + 'static>;
7
8/// Raw file descriptor trait for io-uring operations.
9///
10/// `Arc<dyn AsRawFd>` does not satisfy `AsRawFd` because the blanket impl
11/// for `Arc<T>` requires `T: Sized`. This trait bridges that gap so both
12/// `OwnedFd` and `ArcFd` can be used generically with `Op::read_at`.
13pub(crate) trait UringFd: Send + Sync + 'static {
14    fn as_raw_fd(&self) -> RawFd;
15}
16
17impl UringFd for OwnedFd {
18    fn as_raw_fd(&self) -> RawFd {
19        AsRawFd::as_raw_fd(self)
20    }
21}
22
23impl UringFd for ArcFd {
24    fn as_raw_fd(&self) -> RawFd {
25        (**self).as_raw_fd()
26    }
27}
28
29pub(crate) fn cstr(p: &Path) -> io::Result<CString> {
30    Ok(CString::new(p.as_os_str().as_bytes())?)
31}