Skip to content
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

fs: implement AsRaw{Fd,Handle} for tokio::fs::File (#1364) #1640

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 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
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;
9 changes: 9 additions & 0 deletions tokio-fs/src/file/os/unix/as_raw_fd.rs
@@ -0,0 +1,9 @@
use std::os::unix::io::{AsRawFd, RawFd};

use crate::File;

impl AsRawFd for File {
fn as_raw_fd(&self) -> RawFd {
self.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;
9 changes: 9 additions & 0 deletions tokio-fs/src/file/os/windows/as_raw_handle.rs
@@ -0,0 +1,9 @@
use std::os::windows::io::{AsRawHandle, RawHandle};

use crate::File;

impl AsRawHandle for File {
fn as_raw_handle(&self) -> RawHandle {
self.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
15 changes: 14 additions & 1 deletion tokio-fs/tests/sys/file.rs
Expand Up @@ -4,9 +4,12 @@ use std::fs::{Metadata, Permissions};
use std::io;
use std::io::prelude::*;
use std::io::SeekFrom;
#[cfg(unix)]
use std::os::unix::io::RawFd;
#[cfg(windows)]
use std::os::windows::io::RawHandle;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};

pub struct File {
shared: Arc<Mutex<Shared>>,
}
Expand Down Expand Up @@ -196,6 +199,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