mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-08 00:00:13 +02:00
fs: switch to our own DirEntryExt trait (#2921)
This commit is contained in:
@@ -383,7 +383,7 @@ impl OpenOptions {
|
|||||||
Ok(File::from_std(std))
|
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)]
|
#[cfg(unix)]
|
||||||
pub(super) fn as_inner_mut(&mut self) -> &mut std::fs::OpenOptions {
|
pub(super) fn as_inner_mut(&mut self) -> &mut std::fs::OpenOptions {
|
||||||
&mut self.0
|
&mut self.0
|
||||||
|
|||||||
@@ -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 {}
|
||||||
|
}
|
||||||
@@ -8,3 +8,6 @@ pub use self::open_options_ext::OpenOptionsExt;
|
|||||||
|
|
||||||
mod dir_builder_ext;
|
mod dir_builder_ext;
|
||||||
pub use self::dir_builder_ext::DirBuilderExt;
|
pub use self::dir_builder_ext::DirBuilderExt;
|
||||||
|
|
||||||
|
mod dir_entry_ext;
|
||||||
|
pub use self::dir_entry_ext::DirEntryExt;
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
use crate::fs::open_options::OpenOptions;
|
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`].
|
/// Unix-specific extensions to [`fs::OpenOptions`].
|
||||||
///
|
///
|
||||||
/// This mirrors the definition of [`std::os::unix::fs::OpenOptionsExt`].
|
/// This mirrors the definition of [`std::os::unix::fs::OpenOptionsExt`].
|
||||||
///
|
///
|
||||||
///
|
|
||||||
/// [`fs::OpenOptions`]: crate::fs::OpenOptions
|
/// [`fs::OpenOptions`]: crate::fs::OpenOptions
|
||||||
/// [`std::os::unix::fs::OpenOptionsExt`]: std::os::unix::fs::OpenOptionsExt
|
/// [`std::os::unix::fs::OpenOptionsExt`]: std::os::unix::fs::OpenOptionsExt
|
||||||
pub trait OpenOptionsExt: sealed::Sealed {
|
pub trait OpenOptionsExt: sealed::Sealed {
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ use std::ffi::OsString;
|
|||||||
use std::fs::{FileType, Metadata};
|
use std::fs::{FileType, Metadata};
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::io;
|
use std::io;
|
||||||
#[cfg(unix)]
|
|
||||||
use std::os::unix::fs::DirEntryExt;
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@@ -233,11 +231,10 @@ impl DirEntry {
|
|||||||
let std = self.0.clone();
|
let std = self.0.clone();
|
||||||
asyncify(move || std.file_type()).await
|
asyncify(move || std.file_type()).await
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(unix)]
|
/// Returns a reference to the underlying `std::fs::DirEntry`
|
||||||
impl DirEntryExt for DirEntry {
|
#[cfg(unix)]
|
||||||
fn ino(&self) -> u64 {
|
pub(super) fn as_inner(&self) -> &std::fs::DirEntry {
|
||||||
self.0.ino()
|
&self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user