| Crates.io | sync_file |
| lib.rs | sync_file |
| version | 0.3.1 |
| created_at | 2021-07-21 23:26:10.635961+00 |
| updated_at | 2025-09-10 10:54:26.525012+00 |
| description | Files that can be read concurrently |
| homepage | |
| repository | https://github.com/a1phyr/sync_file |
| max_upload_size | |
| id | 425716 |
| size | 48,852 |
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.
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");
Licensed under either of
at your option.
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.