-
Notifications
You must be signed in to change notification settings - Fork 339
Implement I/O-safe traits on types #1036
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -415,6 +415,8 @@ impl From<std::fs::File> for File { | |
cfg_unix! { | ||
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}; | ||
|
||
|
||
|
||
notgull marked this conversation as resolved.
Show resolved
Hide resolved
|
||
impl AsRawFd for File { | ||
fn as_raw_fd(&self) -> RawFd { | ||
self.file.as_raw_fd() | ||
|
@@ -432,10 +434,36 @@ cfg_unix! { | |
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_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 { | ||
let file = val.file.clone(); | ||
drop(val); | ||
Arc::try_unwrap(file) | ||
.expect(ARC_TRY_UNWRAP_EXPECT) | ||
.into() | ||
notgull marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
} | ||
|
||
cfg_windows! { | ||
|
@@ -458,10 +486,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. | ||
|
@@ -974,3 +1028,5 @@ mod tests { | |
}) | ||
} | ||
} | ||
|
||
const ARC_TRY_UNWRAP_EXPECT: &str = "cannot acquire ownership of the file handle after drop"; | ||
notgull marked this conversation as resolved.
Show resolved
Hide resolved
|
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.