fs: add safe impl From<OwnedFd> for File (#8266)

This impl was missing to be able to create a tokio::fs::File directly
from an OwnedFd, which can be done in a safe way.  Going through RawFd
required unsafe for the same operation.

And same for Windows using a OwnedHandle instead.
This commit is contained in:
linkmauve
2026-07-10 20:02:38 +02:00
committed by GitHub
parent bfd4ddf597
commit c793a631f7
2 changed files with 18 additions and 1 deletions
+14 -1
View File
@@ -897,6 +897,13 @@ impl fmt::Debug for File {
} }
} }
#[cfg(unix)]
impl From<std::os::fd::OwnedFd> for File {
fn from(fd: std::os::fd::OwnedFd) -> Self {
Self::from_std(StdFile::from(fd))
}
}
#[cfg(unix)] #[cfg(unix)]
impl std::os::unix::io::AsRawFd for File { impl std::os::unix::io::AsRawFd for File {
fn as_raw_fd(&self) -> std::os::unix::io::RawFd { fn as_raw_fd(&self) -> std::os::unix::io::RawFd {
@@ -923,7 +930,13 @@ impl std::os::unix::io::FromRawFd for File {
} }
cfg_windows! { cfg_windows! {
use crate::os::windows::io::{AsRawHandle, FromRawHandle, RawHandle, AsHandle, BorrowedHandle}; use crate::os::windows::io::{AsRawHandle, FromRawHandle, RawHandle, AsHandle, BorrowedHandle, OwnedHandle};
impl From<OwnedHandle> for File {
fn from(handle: OwnedHandle) -> Self {
Self::from_std(StdFile::from(handle))
}
}
impl AsRawHandle for File { impl AsRawHandle for File {
fn as_raw_handle(&self) -> RawHandle { fn as_raw_handle(&self) -> RawHandle {
+4
View File
@@ -38,6 +38,10 @@ mock! {
pub fn try_clone(&self) -> io::Result<Self>; pub fn try_clone(&self) -> io::Result<Self>;
} }
#[cfg(windows)] #[cfg(windows)]
impl From<std::os::windows::io::OwnedHandle> for File {
fn from(handle: std::os::windows::io::OwnedHandle) -> Self;
}
#[cfg(windows)]
impl std::os::windows::io::AsRawHandle for File { impl std::os::windows::io::AsRawHandle for File {
fn as_raw_handle(&self) -> std::os::windows::io::RawHandle; fn as_raw_handle(&self) -> std::os::windows::io::RawHandle;
} }