diff --git a/tokio-fs/src/file.rs b/tokio-fs/src/file/mod.rs similarity index 99% rename from tokio-fs/src/file.rs rename to tokio-fs/src/file/mod.rs index 43cfcd0b768..5fc5296ab33 100644 --- a/tokio-fs/src/file.rs +++ b/tokio-fs/src/file/mod.rs @@ -20,6 +20,8 @@ use std::task::Context; use std::task::Poll; use std::task::Poll::*; +mod os; + /// A reference to an open file on the filesystem. /// /// This is a specialized version of [`std::fs::File`][std] for usage from the @@ -117,8 +119,8 @@ impl File { /// # } /// ``` pub async fn open

(path: P) -> io::Result - where - P: AsRef, + where + P: AsRef, { let path = path.as_ref().to_owned(); let std = asyncify(|| sys::File::open(path)).await?; @@ -155,8 +157,8 @@ impl File { /// # } /// ``` pub async fn create

(path: P) -> io::Result - where - P: AsRef, + where + P: AsRef, { let path = path.as_ref().to_owned(); let std_file = asyncify(move || sys::File::create(path)).await?; @@ -342,7 +344,7 @@ impl File { } else { std.set_len(size) } - .map(|_| 0); // the value is discarded later + .map(|_| 0); // the value is discarded later // Return the result as a seek (Operation::Seek(res), buf) diff --git a/tokio-fs/src/file/os/mod.rs b/tokio-fs/src/file/os/mod.rs new file mode 100644 index 00000000000..5a181bbaf88 --- /dev/null +++ b/tokio-fs/src/file/os/mod.rs @@ -0,0 +1,5 @@ +#[cfg(unix)] +mod unix; + +#[cfg(windows)] +mod windows; diff --git a/tokio-fs/src/file/os/unix/as_raw_fd.rs b/tokio-fs/src/file/os/unix/as_raw_fd.rs new file mode 100644 index 00000000000..5c7eed65dc5 --- /dev/null +++ b/tokio-fs/src/file/os/unix/as_raw_fd.rs @@ -0,0 +1,10 @@ +use std::os::unix::io::{AsRawFd, RawFd}; + +use crate::File; + +impl AsRawFd for File { + fn as_raw_fd(&self) -> RawFd { + let std = self.std.clone(); + std.as_raw_fd() + } +} diff --git a/tokio-fs/src/file/os/unix/mod.rs b/tokio-fs/src/file/os/unix/mod.rs new file mode 100644 index 00000000000..ad75425f710 --- /dev/null +++ b/tokio-fs/src/file/os/unix/mod.rs @@ -0,0 +1 @@ +mod as_raw_fd; diff --git a/tokio-fs/src/file/os/windows/as_raw_handle.rs b/tokio-fs/src/file/os/windows/as_raw_handle.rs new file mode 100644 index 00000000000..cc4b82b1576 --- /dev/null +++ b/tokio-fs/src/file/os/windows/as_raw_handle.rs @@ -0,0 +1,10 @@ +use std::os::windows::io::{AsRawHandle, RawHandle}; + +use crate::File; + +impl AsRawHandle for File { + fn as_raw_handle(&self) -> RawHandle { + let std = self.std.clone(); + std.as_raw_handle() + } +} diff --git a/tokio-fs/src/file/os/windows/mod.rs b/tokio-fs/src/file/os/windows/mod.rs new file mode 100644 index 00000000000..536e1f2e878 --- /dev/null +++ b/tokio-fs/src/file/os/windows/mod.rs @@ -0,0 +1 @@ +mod as_raw_handle; diff --git a/tokio-fs/tests/file.rs b/tokio-fs/tests/file.rs index db26becef13..4451a9bc582 100644 --- a/tokio-fs/tests/file.rs +++ b/tokio-fs/tests/file.rs @@ -44,6 +44,26 @@ async fn basic_write() { assert_eq!(file, HELLO); } +#[tokio::test] +#[cfg(unix)] +async fn unix_fd() { + use std::os::unix::io::AsRawFd; + let tempfile = tempfile(); + + let file = File::create(tempfile.path()).await.unwrap(); + assert!(file.as_raw_fd() as u64 > 0); +} + +#[tokio::test] +#[cfg(windows)] +async fn windows_handle() { + use std::os::windows::io::AsRawHandle; + let tempfile = tempfile(); + + let file = File::create(tempfile.path()).await.unwrap(); + assert!(file.as_raw_handle() as u64 > 0); +} + fn tempfile() -> NamedTempFile { NamedTempFile::new().unwrap() } diff --git a/tokio-fs/tests/file_mocked.rs b/tokio-fs/tests/file_mocked.rs index 422588760a4..64321067f1b 100644 --- a/tokio-fs/tests/file_mocked.rs +++ b/tokio-fs/tests/file_mocked.rs @@ -10,7 +10,7 @@ mod sys { use sys::pool::{self, asyncify}; #[allow(warnings)] -#[path = "../src/file.rs"] +#[path = "../src/file/mod.rs"] mod file; use file::File; diff --git a/tokio-fs/tests/sys/file.rs b/tokio-fs/tests/sys/file.rs index 7f3beee8415..f9da6149caf 100644 --- a/tokio-fs/tests/sys/file.rs +++ b/tokio-fs/tests/sys/file.rs @@ -6,6 +6,8 @@ use std::io::prelude::*; use std::io::SeekFrom; use std::path::PathBuf; use std::sync::{Arc, Mutex}; +#[cfg(unix)] use std::os::unix::io::RawFd; +#[cfg(windows)] use std::os::windows::io::RawHandle; pub struct File { shared: Arc>, @@ -196,6 +198,16 @@ impl File { pub fn try_clone(&self) -> io::Result { unimplemented!(); } + + #[cfg(unix)] + pub fn as_raw_fd(&self) -> RawFd { + unimplemented!() + } + + #[cfg(windows)] + pub fn as_raw_handle(&self) -> RawHandle { + unimplemented!() + } } impl Read for &'_ File {