fs: switch to our own DirEntryExt trait (#2921)

This commit is contained in:
Taiki Endo
2020-10-08 02:30:25 +09:00
committed by GitHub
parent 601a3ef93f
commit c248167173
5 changed files with 53 additions and 10 deletions
+1 -1
View File
@@ -383,7 +383,7 @@ impl OpenOptions {
Ok(File::from_std(std))
}
/// Returns a mutable reference to the the underlying std::fs::OpenOptions
/// Returns a mutable reference to the underlying `std::fs::OpenOptions`
#[cfg(unix)]
pub(super) fn as_inner_mut(&mut self) -> &mut std::fs::OpenOptions {
&mut self.0
+44
View File
@@ -0,0 +1,44 @@
use crate::fs::DirEntry;
use std::os::unix::fs::DirEntryExt as _;
/// Unix-specific extension methods for [`fs::DirEntry`].
///
/// This mirrors the definition of [`std::os::unix::fs::DirEntryExt`].
///
/// [`fs::DirEntry`]: crate::fs::DirEntry
/// [`std::os::unix::fs::DirEntryExt`]: std::os::unix::fs::DirEntryExt
pub trait DirEntryExt: sealed::Sealed {
/// Returns the underlying `d_ino` field in the contained `dirent`
/// structure.
///
/// # Examples
///
/// ```
/// use tokio::fs;
/// use tokio::fs::os::unix::DirEntryExt;
///
/// # #[tokio::main]
/// # async fn main() -> std::io::Result<()> {
/// let mut entries = fs::read_dir(".").await?;
/// while let Some(entry) = entries.next_entry().await? {
/// // Here, `entry` is a `DirEntry`.
/// println!("{:?}: {}", entry.file_name(), entry.ino());
/// }
/// # Ok(())
/// # }
/// ```
fn ino(&self) -> u64;
}
impl DirEntryExt for DirEntry {
fn ino(&self) -> u64 {
self.as_inner().ino()
}
}
impl sealed::Sealed for DirEntry {}
pub(crate) mod sealed {
#[doc(hidden)]
pub trait Sealed {}
}
+3
View File
@@ -8,3 +8,6 @@ pub use self::open_options_ext::OpenOptionsExt;
mod dir_builder_ext;
pub use self::dir_builder_ext::DirBuilderExt;
mod dir_entry_ext;
pub use self::dir_entry_ext::DirEntryExt;
+1 -2
View File
@@ -1,11 +1,10 @@
use crate::fs::open_options::OpenOptions;
use std::os::unix::fs::OpenOptionsExt as StdOpenOptionsExt;
use std::os::unix::fs::OpenOptionsExt as _;
/// Unix-specific extensions to [`fs::OpenOptions`].
///
/// This mirrors the definition of [`std::os::unix::fs::OpenOptionsExt`].
///
///
/// [`fs::OpenOptions`]: crate::fs::OpenOptions
/// [`std::os::unix::fs::OpenOptionsExt`]: std::os::unix::fs::OpenOptionsExt
pub trait OpenOptionsExt: sealed::Sealed {
+4 -7
View File
@@ -4,8 +4,6 @@ use std::ffi::OsString;
use std::fs::{FileType, Metadata};
use std::future::Future;
use std::io;
#[cfg(unix)]
use std::os::unix::fs::DirEntryExt;
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::sync::Arc;
@@ -233,11 +231,10 @@ impl DirEntry {
let std = self.0.clone();
asyncify(move || std.file_type()).await
}
}
#[cfg(unix)]
impl DirEntryExt for DirEntry {
fn ino(&self) -> u64 {
self.0.ino()
/// Returns a reference to the underlying `std::fs::DirEntry`
#[cfg(unix)]
pub(super) fn as_inner(&self) -> &std::fs::DirEntry {
&self.0
}
}