From c146f48f0b4f1fbecb1bed3d5a32222506d50704 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Tue, 26 Nov 2019 16:00:26 -0800 Subject: [PATCH] fs: impl AsRawFd / AsRawHandle for File (#1827) This provides the ability to get the raw OS handle for a `File`. The `Into*` variant cannot be provided as `File` needs to maintain ownership of the `File`. The actual handle may have been moved to a background thread. --- tokio/src/fs/file.rs | 14 ++++++++++++++ tokio/tests/support/mock_file.rs | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/tokio/src/fs/file.rs b/tokio/src/fs/file.rs index af7be5863c9..c45582729ac 100644 --- a/tokio/src/fs/file.rs +++ b/tokio/src/fs/file.rs @@ -600,3 +600,17 @@ impl fmt::Debug for File { .finish() } } + +#[cfg(unix)] +impl std::os::unix::io::AsRawFd for File { + fn as_raw_fd(&self) -> std::os::unix::io::RawFd { + self.std.as_raw_fd() + } +} + +#[cfg(windows)] +impl std::os::windows::io::AsRawHandle for File { + fn as_raw_handle(&self) -> std::os::windows::io::RawHandle { + self.std.as_raw_handle() + } +} diff --git a/tokio/tests/support/mock_file.rs b/tokio/tests/support/mock_file.rs index 7f3beee8415..44aa7b3f715 100644 --- a/tokio/tests/support/mock_file.rs +++ b/tokio/tests/support/mock_file.rs @@ -263,3 +263,17 @@ impl fmt::Debug for File { fmt.debug_struct("mock::File").finish() } } + +#[cfg(unix)] +impl std::os::unix::io::AsRawFd for File { + fn as_raw_fd(&self) -> std::os::unix::io::RawFd { + unimplemented!(); + } +} + +#[cfg(windows)] +impl std::os::windows::io::AsRawHandle for File { + fn as_raw_handle(&self) -> std::os::windows::io::RawHandle { + unimplemented!(); + } +}