Skip to content

Commit

Permalink
enhancement: implement AsRaw{Fd,Handle} for tokio::fs::File (tokio-rs…
Browse files Browse the repository at this point in the history
…#1364)

Moved file.rs to file/mod.rs to get around mod visibility issue.
  • Loading branch information
Xinkai committed Oct 7, 2019
1 parent aefaef3 commit 968453c
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 6 deletions.
12 changes: 7 additions & 5 deletions tokio-fs/src/file.rs → tokio-fs/src/file/mod.rs
Expand Up @@ -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
Expand Down Expand Up @@ -117,8 +119,8 @@ impl File {
/// # }
/// ```
pub async fn open<P>(path: P) -> io::Result<File>
where
P: AsRef<Path>,
where
P: AsRef<Path>,
{
let path = path.as_ref().to_owned();
let std = asyncify(|| sys::File::open(path)).await?;
Expand Down Expand Up @@ -155,8 +157,8 @@ impl File {
/// # }
/// ```
pub async fn create<P>(path: P) -> io::Result<File>
where
P: AsRef<Path>,
where
P: AsRef<Path>,
{
let path = path.as_ref().to_owned();
let std_file = asyncify(move || sys::File::create(path)).await?;
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions tokio-fs/src/file/os/mod.rs
@@ -0,0 +1,5 @@
#[cfg(unix)]
mod unix;

#[cfg(windows)]
mod windows;
10 changes: 10 additions & 0 deletions 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()
}
}
1 change: 1 addition & 0 deletions tokio-fs/src/file/os/unix/mod.rs
@@ -0,0 +1 @@
mod as_raw_fd;
10 changes: 10 additions & 0 deletions 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()
}
}
1 change: 1 addition & 0 deletions tokio-fs/src/file/os/windows/mod.rs
@@ -0,0 +1 @@
mod as_raw_handle;
20 changes: 20 additions & 0 deletions tokio-fs/tests/file.rs
Expand Up @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion tokio-fs/tests/file_mocked.rs
Expand Up @@ -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;

Expand Down
12 changes: 12 additions & 0 deletions tokio-fs/tests/sys/file.rs
Expand Up @@ -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<Mutex<Shared>>,
Expand Down Expand Up @@ -196,6 +198,16 @@ impl File {
pub fn try_clone(&self) -> io::Result<Self> {
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 {
Expand Down

0 comments on commit 968453c

Please sign in to comment.