13 个版本

0.2.9 2024 年 5 月 20 日
0.2.8 2024 年 3 月 25 日
0.2.7 2024 年 1 月 8 日
0.2.6 2023 年 3 月 14 日
0.1.1 2021 年 7 月 30 日

#165并发

Download history 65/week @ 2024-04-22 42/week @ 2024-04-29 52/week @ 2024-05-06 67/week @ 2024-05-13 495/week @ 2024-05-20 290/week @ 2024-05-27 59/week @ 2024-06-03 92/week @ 2024-06-10 42/week @ 2024-06-17 20/week @ 2024-06-24 78/week @ 2024-07-01 71/week @ 2024-07-08 43/week @ 2024-07-15 27/week @ 2024-07-22 66/week @ 2024-07-29 70/week @ 2024-08-05

每月 216 次下载
2 个 Crates 中使用 (通过 assets_manager)

MIT/Apache

43KB
1K SLoC

Sync File

Crates.io Docs.rs Minimum rustc version

可并发读取的文件。

std::fs::FileSync,但从中并发读取会导致竞争条件,因为操作系统有一个单独的光标,该光标被多个线程使用。

SyncFile 通过使用特定于平台的扩展来进行位置 I/O,以解决这个问题,因此文件的光标不会被共享。

示例

use std::io::Read;
use sync_file::SyncFile;

/// Reads a file byte by byte.
/// Don't do this in real code !
fn read_all<R: Read>(mut file: R) -> std::io::Result<Vec<u8>> {
    let mut result = Vec::new();
    let mut buf = [0];

    while file.read(&mut buf)? != 0 {
        result.extend(&buf);
    }

    Ok(result)
}

// Open a file
let f = SyncFile::open("hello.txt")?;
let f_clone = f.clone();

// Read it concurrently
let thread = std::thread::spawn(move || read_all(f_clone));
let res1 = read_all(f)?;
let res2 = thread.join().unwrap()?;

// Both clones read the whole content
// This would not work with `std::fs::File`
assert_eq!(res1, b"Hello World!\n");
assert_eq!(res2, b"Hello World!\n");

许可证

根据以下任一项许可:

任选其一。

贡献

除非您明确表示,否则您提交给作品以包含在内的任何贡献,根据 Apache-2.0 许可证定义,应双许可如上所述,不附加任何额外条款或条件。

依赖关系

~175KB