-
Notifications
You must be signed in to change notification settings - Fork 341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement I/O-safe traits on types #1036
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ use crate::prelude::*; | |
use crate::task::{spawn_blocking, Context, Poll, Waker}; | ||
use crate::utils::Context as _; | ||
|
||
const ARC_TRY_UNWRAP_EXPECT: &str = "cannot acquire ownership of the file handle after drop"; | ||
|
||
/// An open file on the filesystem. | ||
/// | ||
/// Depending on what options the file was opened with, this type can be used for reading and/or | ||
|
@@ -415,6 +417,15 @@ impl From<std::fs::File> for File { | |
cfg_unix! { | ||
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; | ||
|
||
impl File { | ||
fn into_std_file(self) -> std::fs::File { | ||
let file = self.file.clone(); | ||
drop(self); | ||
Arc::try_unwrap(file) | ||
.expect(ARC_TRY_UNWRAP_EXPECT) | ||
} | ||
} | ||
|
||
impl AsRawFd for File { | ||
fn as_raw_fd(&self) -> RawFd { | ||
self.file.as_raw_fd() | ||
|
@@ -429,11 +440,29 @@ cfg_unix! { | |
|
||
impl IntoRawFd for File { | ||
fn into_raw_fd(self) -> RawFd { | ||
let file = self.file.clone(); | ||
drop(self); | ||
Arc::try_unwrap(file) | ||
.expect("cannot acquire ownership of the file handle after drop") | ||
.into_raw_fd() | ||
self.into_std_file().into_raw_fd() | ||
} | ||
} | ||
|
||
cfg_io_safety! { | ||
use crate::os::unix::io::{AsFd, BorrowedFd, OwnedFd}; | ||
|
||
impl AsFd for File { | ||
fn as_fd(&self) -> BorrowedFd<'_> { | ||
self.file.as_fd() | ||
} | ||
} | ||
|
||
impl From<OwnedFd> for File { | ||
fn from(fd: OwnedFd) -> Self { | ||
std::fs::File::from(fd).into() | ||
} | ||
} | ||
|
||
impl From<File> for OwnedFd { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was mirroring the previous implementation for |
||
fn from(val: File) -> OwnedFd { | ||
self.into_std_file().into() | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -458,10 +487,36 @@ cfg_windows! { | |
let file = self.file.clone(); | ||
drop(self); | ||
Arc::try_unwrap(file) | ||
.expect("cannot acquire ownership of the file handle after drop") | ||
.expect(ARC_TRY_UNWRAP_EXPECT) | ||
.into_raw_handle() | ||
} | ||
} | ||
|
||
cfg_io_safety! { | ||
use crate::os::windows::io::{AsHandle, BorrowedHandle, OwnedHandle}; | ||
|
||
impl AsHandle for File { | ||
fn as_handle(&self) -> BorrowedHandle<'_> { | ||
self.file.as_handle() | ||
} | ||
} | ||
|
||
impl From<OwnedHandle> for File { | ||
fn from(handle: OwnedHandle) -> Self { | ||
std::fs::File::from(handle).into() | ||
} | ||
} | ||
|
||
impl From<File> for OwnedHandle { | ||
fn from(val: File) -> OwnedHandle { | ||
let file = val.file.clone(); | ||
drop(val); | ||
Arc::try_unwrap(file) | ||
.expect(ARC_TRY_UNWRAP_EXPECT) | ||
.into() | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// An async mutex with non-borrowing lock guards. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -282,6 +282,28 @@ cfg_unix! { | |
self.watcher.into_inner().unwrap().into_raw_fd() | ||
} | ||
} | ||
|
||
cfg_io_safety! { | ||
use crate::os::unix::io::{AsFd, BorrowedFd, OwnedFd}; | ||
|
||
impl AsFd for TcpListener { | ||
fn as_fd(&self) -> BorrowedFd<'_> { | ||
self.watcher.get_ref().as_fd() | ||
} | ||
} | ||
|
||
impl From<OwnedFd> for TcpListener { | ||
fn from(fd: OwnedFd) -> TcpListener { | ||
std::net::TcpListener::from(fd).into() | ||
} | ||
} | ||
|
||
impl From<TcpListener> for OwnedFd { | ||
fn from(listener: TcpListener) -> OwnedFd { | ||
listener.watcher.into_inner().unwrap().into() | ||
} | ||
} | ||
} | ||
} | ||
|
||
cfg_windows! { | ||
|
@@ -306,4 +328,26 @@ cfg_windows! { | |
self.watcher.into_inner().unwrap().into_raw_socket() | ||
} | ||
} | ||
|
||
cfg_io_safety! { | ||
use crate::os::windows::io::{AsSocket, BorrowedSocket, OwnedSocket}; | ||
|
||
impl AsSocket for TcpListener { | ||
fn as_socket(&self) -> BorrowedSocket<'_> { | ||
self.watcher.get_ref().as_socket() | ||
} | ||
} | ||
|
||
impl From<OwnedSocket> for TcpListener { | ||
fn from(fd: OwnedSocket) -> TcpListener { | ||
std::net::TcpListener::from(fd).into() | ||
} | ||
} | ||
|
||
impl From<TcpListener> for OwnedSocket { | ||
fn from(listener: TcpListener) -> OwnedSocket { | ||
listener.watcher.into_inner().unwrap().into() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was mirroring the |
||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn’t support MacOS?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes I wrote only impact
cfg(unix)
andcfg(windows)
, so I included a Unix platform (ubuntu-latest
) and a Windows platform (windows-latest
). If you would like me to also test on MacOS I can.