pub trait FileExt {
// Required methods
fn seek_read(&self, buf: &mut [u8], offset: u64) -> Result<usize>;
fn seek_write(&self, buf: &[u8], offset: u64) -> Result<usize>;
// Provided method
fn seek_read_buf(&self, buf: BorrowedCursor<'_>, offset: u64) -> Result<()> { ... }
}
Expand description
Windows-specific extensions to fs::File
.
Required Methods§
1.15.0 · Sourcefn seek_read(&self, buf: &mut [u8], offset: u64) -> Result<usize>
fn seek_read(&self, buf: &mut [u8], offset: u64) -> Result<usize>
Seeks to a given position and reads a number of bytes.
Returns the number of bytes read.
The offset is relative to the start of the file and thus independent from the current cursor. The current cursor is affected by this function, it is set to the end of the read.
Reading beyond the end of the file will always return with a length of 0.
Note that similar to File::read
, it is not an error to return with a
short read. When returning from such a short read, the file pointer is
still updated.
§Examples
1.15.0 · Sourcefn seek_write(&self, buf: &[u8], offset: u64) -> Result<usize>
fn seek_write(&self, buf: &[u8], offset: u64) -> Result<usize>
Seeks to a given position and writes a number of bytes.
Returns the number of bytes written.
The offset is relative to the start of the file and thus independent from the current cursor. The current cursor is affected by this function, it is set to the end of the write.
When writing beyond the end of the file, the file is appropriately extended and the intermediate bytes are set to zero.
Note that similar to File::write
, it is not an error to return a
short write. When returning from such a short write, the file pointer
is still updated.
§Examples
Provided Methods§
Sourcefn seek_read_buf(&self, buf: BorrowedCursor<'_>, offset: u64) -> Result<()>
🔬This is a nightly-only experimental API. (read_buf_at
#140771)
fn seek_read_buf(&self, buf: BorrowedCursor<'_>, offset: u64) -> Result<()>
read_buf_at
#140771)Seeks to a given position and reads some bytes into the buffer.
This is equivalent to the seek_read
method, except that it is passed
a BorrowedCursor
rather than &mut [u8]
to allow use with uninitialized buffers. The
new data will be appended to any existing contents of buf
.
Reading beyond the end of the file will always succeed without reading any bytes.
§Examples
#![feature(core_io_borrowed_buf)]
#![feature(read_buf_at)]
use std::io;
use std::io::BorrowedBuf;
use std::fs::File;
use std::mem::MaybeUninit;
use std::os::windows::prelude::*;
fn main() -> io::Result<()> {
let mut file = File::open("pi.txt")?;
// Read some bytes starting from offset 2
let mut buf: [MaybeUninit<u8>; 10] = [MaybeUninit::uninit(); 10];
let mut buf = BorrowedBuf::from(buf.as_mut_slice());
file.seek_read_buf(buf.unfilled(), 2)?;
assert!(buf.filled().starts_with(b"1"));
Ok(())
}