mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-07 00:00:09 +02:00
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:
+14
-1
@@ -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 {
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user