Skip to content

File read_at, non-mutable platform independent version #47229

Closed
@PSeitz

Description

@PSeitz

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.

#[cfg(any(windows))]
fn load_exact_bytes_at(buffer: &mut Vec<u8>, file: &File, offset: u64) {
    use std::os::windows::fs::FileExt;
    let mut data_read = 0;
    while data_read < buffer.len() {
        data_read += file.seek_read(buffer, offset).unwrap();
    }
}

#[cfg(any(unix))]
fn load_exact_bytes_at(buffer: &mut Vec<u8>, file: &File, offset: u64) {
    use std::os::unix::fs::FileExt;
    let mut data_read = 0;
    while data_read < buffer.len() {
        data_read += file.read_at(buffer, offset).unwrap();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions