#file #sync #concurrency

sync_file

Files that can be read concurrently

8 releases

0.2.4 Nov 21, 2022
0.2.3 Aug 26, 2022
0.2.2 Mar 31, 2022
0.2.1 Jan 9, 2022
0.1.1 Jul 30, 2021

#150 in Concurrency

Download history 113/week @ 2022-10-08 106/week @ 2022-10-15 98/week @ 2022-10-22 158/week @ 2022-10-29 174/week @ 2022-11-05 184/week @ 2022-11-12 161/week @ 2022-11-19 165/week @ 2022-11-26 15/week @ 2022-12-03 26/week @ 2022-12-10 33/week @ 2022-12-17 32/week @ 2022-12-24 20/week @ 2022-12-31 23/week @ 2023-01-07 12/week @ 2023-01-14 29/week @ 2023-01-21

95 downloads per month
Used in 2 crates (via assets_manager)

MIT/Apache

29KB
647 lines

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

~18KB