-
Notifications
You must be signed in to change notification settings - Fork 13.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
File read_at, non-mutable platform independent version #47229
Comments
Assuming that |
@tbu- mentions only WriteFile here: #35704 (comment), but the |
|
I know this is possible, but the unnecessary(?) mut complicates things (a lot in my case, without the plattform specific implementation). |
Here's how to read from a immutable reference to a file:
Your implementation of EDIT: Removed the |
@PSeitz Complicates how? You already have a |
Okay, I thought a extern crate rayon;
use std::io::SeekFrom;
use std::io::prelude::*;
use std::fs::File;
use rayon::prelude::*;
fn main() {
let file = File::open("file").unwrap();
[0,1].par_iter()
.for_each(|p| {
let mut bytes = vec![];
bytes.resize(8, 0);
load_bytes(&mut bytes, &file, *p as u64);
});
}
fn load_bytes(buffer: &mut Vec<u8>, mut file: &File, offset: u64) {
file.seek(SeekFrom::Start(offset)).unwrap();
file.read_exact(buffer).unwrap();
} |
A
The file I/O is racy, yes. Rust does not provide guaranteed protection from "logical races", just from data races. The operating systems' I/O operations are data-race-free even when multiple threads work on the same file, but even a single I/O operation is rarely atomic (e.g., a read might get interrupted). Multiple separate operations (such as the |
Do you mean file or std::fs::File? Do you refer to that read-only scenario? I thought read-only with multiple file handles should be no problem. It would be nice if the API was designed, to protect from races on a single std::fs::File. |
I meant the
A
Yes (though again, just a race, not a data race).
Maybe, but backwards compatibility prevents us from changing this. Can this issue be closed now? It seems the proposed API is no longer needed. If you have any further questions, I'd direct you to http://users.rust-lang.org/ or other forums. |
I have multiple threads reading from a
std::fs::File
handle. The existing Read Trait for File is not so nice in this case because of the mutability&mut File
. I created the non-mutable platform independent version from the plattform specific counterparts like below.It would be nice to have this in the standard library.
The text was updated successfully, but these errors were encountered: