7 个稳定版本
2.76.0 |
|
---|---|
2.7.1 | 2024 年 4 月 17 日 |
2.5.0 | 2024 年 3 月 22 日 |
2.4.1 | 2024 年 2 月 10 日 |
#240 in 文件系统
用于 scandir_rs
73KB
2K SLoC
scandir
Rust crate 命名为 scandir
,可通过 cargo
安装。在 Linux 上比标准库快 1.5 - 2.9 倍,在 Windows 上快 1.5 - 5.4 倍(参见 性能测试)。
如果您只对目录统计感兴趣,可以使用 Count
。
scandir_rs
包含以下类
Count
用于确定目录的统计信息。Walk
用于获取目录条目的名称。Scandir
用于获取目录条目的详细信息。
有关 API,请参阅
示例
Collect
示例
use scandir::Count;
// collect() starts the worker thread and waits until it has finished. The line below is blocking.
println!(Count::new("/usr")?.collect()?);
// Get extended statistics
println!(Count::new("/usr", return_type=ReturnType.Ext)?.collect()?);
相同的,但使用类实例在后台异步执行
use scandir::Count;
let mut instance = Count::new("/usr", return_type=ReturnType.Ext);
instance.start(); // Start scanning the directory
...
let values = instance.results(); // Returns the current statistics. Can be read at any time
...
if instance.busy() { // Check if the task is still running.
...
instance.stop(); // If you want to cancel the task
...
instance.join(); // Wait for the instance to finish.
let mut instance = Count::new(&path)?;
instance.start()?;
loop {
if !instance.busy() {
break;
}
// Do something
thread::sleep(Duration::from_millis(10));
}
// collect() immediately returns because the worker thread has already finished.
let statistics = instance.collect()?;
Walk
示例
use scandir::Walk;
// Get basic file tree
println!(Walk::new("/usr")?.collect()?);
// Get file tree with extended file type identification. This is slower.
println!(Walk::new("/usr", return_type=ReturnType.Ext)?.collect()?);
如果您想要中间结果,例如,您想向用户显示进度,请使用以下示例。
let mut instance = Walk::new(&path, None)?;
instance.start()?;
loop {
if !instance.busy() {
break;
}
let new_results = instance.results(true);
// Do something
thread::sleep(Duration::from_millis(10));
}
// collect() immediately returns because the worker thread has already finished.
let results = instance.collect()?;
Scandir
示例
use scandir::Scandir;
// Get basic file metadata
println!(Scandir::new("/usr")?.collect()?);
// Get extended file metadata
println!(Scandir::new("/usr", return_type=ReturnType.Ext, None)?.collect()?);
如果您想要中间结果,例如,您想向用户显示进度,请使用以下示例。
let mut instance = Scandir::new(&path, None)?;
instance.start()?;
loop {
if !instance.busy() {
break;
}
let new_results = instance.results(true);
// Do something
thread::sleep(Duration::from_millis(10));
}
// collect() immediately returns because the worker thread has already finished.
let results = instance.collect()?;
依赖项
~2.4–3.5MB
~72K SLoC