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

feat(fs): support from_std #219

Merged
merged 1 commit into from
Dec 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions monoio/src/fs/file.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, RawFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawHandle, RawHandle};
use std::{io, path::Path};
#[cfg(unix)]
use std::os::{
fd::IntoRawFd,
unix::io::{AsRawFd, RawFd},
};
use std::{fs::File as StdFile, io, path::Path};

use crate::{
buf::{IoBuf, IoBufMut},
Expand Down Expand Up @@ -53,6 +56,7 @@ use crate::{
/// Ok(())
/// }
/// ```
#[derive(Debug)]
pub struct File {
/// Open file descriptor
fd: SharedFd,
Expand Down Expand Up @@ -120,6 +124,23 @@ impl File {
File { fd }
}

/// Converts a [`std::fs::File`] to a [`monoio::fs::File`](File).
///
/// # Examples
///
/// ```no_run
/// // This line could block. It is not recommended to do this on the monoio
/// // runtime.
/// let std_file = std::fs::File::open("foo.txt").unwrap();
/// let file = monoio::fs::File::from_std(std_file);
/// ```
#[cfg(unix)]
pub fn from_std(std: StdFile) -> io::Result<File> {
Ok(File {
fd: SharedFd::new(std.into_raw_fd())?,
})
}

/// Read some bytes at the specified offset from the file into the specified
/// buffer, returning how many bytes were read.
///
Expand Down
Loading