#read-file #sync #file-sync #file

sync_file

Files that can be read concurrently

14 releases

Uses new Rust 2024

0.3.0 Mar 15, 2025
0.2.9 May 20, 2024
0.2.8 Mar 25, 2024
0.2.6 Mar 14, 2023
0.1.1 Jul 30, 2021

#117 in Concurrency

Download history 8046/week @ 2024-12-04 6643/week @ 2024-12-11 3589/week @ 2024-12-18 161/week @ 2024-12-25 996/week @ 2025-01-01 3384/week @ 2025-01-08 243/week @ 2025-01-15 122/week @ 2025-01-22 143/week @ 2025-01-29 143/week @ 2025-02-05 92/week @ 2025-02-12 83/week @ 2025-02-19 109/week @ 2025-02-26 115/week @ 2025-03-05 322/week @ 2025-03-12 218/week @ 2025-03-19

773 downloads per month
Used in 8 crates (5 directly)

MIT/Apache

45KB
1K SLoC

Sync File

Crates.io Docs.rs Minimum rustc version

Files that can be read concurrently.

std::fs::File is Sync but reading concurrently from it results in race conditions, because the OS has a single cursor which is advanced and used by several threads.

SyncFile solves this problem by using platform-specific extensions to do positional I/O, so the cursor of the file is not shared.

Example

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");

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~180KB