Struct RandomAccessFile

Source
pub struct RandomAccessFile { /* private fields */ }
Expand description

A wrapper for File that provides optimized random access through ReadAt and WriteAt.

  • On Unix the operating system is advised that reads will be in random order (FADV_RANDOM).
  • On Windows the implementation is orders of magnitude faster than ReadAt directly on File.

§Examples

Read the fifth 512-byte sector of a file:

use positioned_io::{RandomAccessFile, ReadAt};

// open a file (note: binding does not need to be mut)
let raf = RandomAccessFile::open("tests/pi.txt")?;

// read up to 512 bytes
let mut buf = [0; 512];
let bytes_read = raf.read_at(2048, &mut buf)?;

Implementations§

Source§

impl RandomAccessFile

Source

pub fn open<P>(path: P) -> Result<RandomAccessFile, Error>
where P: AsRef<Path>,

Opens a file for random access.

Source

pub fn try_new(file: File) -> Result<RandomAccessFile, Error>

Creates a RandomAccessFile wrapper around a File.

Source

pub fn try_into_inner(self) -> Result<File, (RandomAccessFile, Error)>

Tries to unwrap the inner File.

Trait Implementations§

Source§

impl Debug for RandomAccessFile

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl ReadAt for RandomAccessFile

Source§

fn read_at(&self, pos: u64, buf: &mut [u8]) -> Result<usize, Error>

Reads bytes from an offset in this source into a buffer, returning how many bytes were read. Read more
Source§

fn read_exact_at(&self, pos: u64, buf: &mut [u8]) -> Result<(), Error>

Reads the exact number of bytes required to fill buf from an offset. Read more
Source§

impl Size for RandomAccessFile

Source§

fn size(&self) -> Result<Option<u64>, Error>

Get the size of this object, in bytes. Read more
Source§

impl WriteAt for &RandomAccessFile

Source§

fn write_at(&mut self, pos: u64, buf: &[u8]) -> Result<usize, Error>

Writes bytes from a buffer to an offset, returning the number of bytes written. Read more
Source§

fn flush(&mut self) -> Result<(), Error>

Flush this writer, ensuring that any intermediately buffered data reaches its destination. Read more
Source§

fn write_all_at(&mut self, pos: u64, buf: &[u8]) -> Result<(), Error>

Writes a complete buffer at an offset. Read more
Source§

impl WriteAt for RandomAccessFile

Source§

fn write_at(&mut self, pos: u64, buf: &[u8]) -> Result<usize, Error>

Writes bytes from a buffer to an offset, returning the number of bytes written. Read more
Source§

fn flush(&mut self) -> Result<(), Error>

Flush this writer, ensuring that any intermediately buffered data reaches its destination. Read more
Source§

fn write_all_at(&mut self, pos: u64, buf: &[u8]) -> Result<(), Error>

Writes a complete buffer at an offset. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.