Skip to main content

DirectIoFile

Struct DirectIoFile 

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

Wrapper data structure for direct/unbuffered/uncached I/O.

Depending on OS, this will use direct I/O, unbuffered, uncaches passthrough file reads/writes, bypassing as much of OS machinery as possible.

NOTE: There are major alignment requirements described here: https://learn.microsoft.com/en-us/windows/win32/fileio/file-buffering#alignment-and-file-access-requirements https://man7.org/linux/man-pages/man2/open.2.html

Implementations§

Source§

impl DirectIoFile

Source

pub fn open<P>(options: OpenOptions, path: P) -> Result<Self>
where P: AsRef<Path>,

Open a file with basic open options at the specified path for direct/unbuffered I/O for reads and writes.

options allows configuring things like read/write/create/truncate, but custom options will be overridden internally.

This is especially important on Windows to prevent huge memory usage.

Source

pub fn len(&self) -> Result<u64>

Get file size, which is the size of the whole device for a block device (see Self::is_block_device())

Source

pub fn is_block_device(&self) -> Result<bool>

Returns Ok(true) if the file is a block device.

On Windows these are disks, volumes and partitions opened with paths like \\.\PhysicalDrive0 or \\.\C:. A block device has a fixed size, it can’t be allocated or resized.

Source

pub fn is_empty(&self) -> Result<bool>

Returns Ok(true) if the file is empty

Source

pub fn set_len(&self, len: u64) -> Result<()>

Truncates or extends the underlying file, updating the size of this file to become len.

Note if len is larger than the previous file size, it will result in a sparse file. To allocate space on disk, use a crate like fs4 with Self::file(), which may be followed by this method to truncate the file if the new file size is smaller than the previous (allocation doesn’t truncate the file).

Source

pub fn file(&self) -> &File ⓘ

The file direct I/O happens on, for operations this type doesn’t wrap, like locking or allocation of disk space.

Reads and writes through it bypass the alignment handling of this type.

Source

pub fn read_at( &self, buffer: OwnedPages, len: u32, offset: u64, ) -> Result<OwnedAlignedBuffer<Align1>, IoBufError<OwnedPages>>

Read len bytes at offset into buffer, replacing its contents.

Whole pages that cover the requested bytes are read into the buffer, which grows if it doesn’t have enough capacity. Existing contents are discarded, so they are never copied. The returned buffer contains exactly the requested bytes, which start at offset % OwnedPages::ALIGN from the beginning of the data region of its page-aligned allocation, see [OwnedAlignedBuffer::into_empty()] for reusing it for the next read.

All pages that cover the requested bytes must exist in the file. The buffer is returned back on error too.

Source

pub fn read_pages_at( &self, buffer: OwnedPages, num_pages: u32, page_offset: u64, ) -> Result<OwnedPages, IoBufError<OwnedPages>>

Read num_pages pages at page_offset (in pages) into buffer, replacing its contents.

The buffer grows if it doesn’t have enough capacity. Existing contents are discarded, so they are never copied. All pages must exist in the file. The buffer is returned back on error too.

Source

pub fn write_at<B>(&self, buffer: B, offset: u64) -> Result<B, IoBufError<B>>
where B: AlignedBuffer,

Write the contents of buffer at offset.

The contents must be as unaligned in memory as offset is in the file, both must have the same offset within a page, which is the case for a buffer returned by Self::read_at() at the same offset and for page-aligned buffers written at page-aligned offsets, otherwise io::ErrorKind::InvalidInput is returned. Whole pages are written directly from the buffer. Partially written pages at the beginning and the end are read from the file first, so they must exist in the file, and are written from a page on the stack, serialized with other such writes. The buffer is returned back on error too.

Trait Implementations§

Source§

impl Debug for DirectIoFile

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for DirectIoFile

§

impl !RefUnwindSafe for DirectIoFile

§

impl Forget for DirectIoFile

§

impl Move for DirectIoFile

§

impl Send for DirectIoFile
where I32NotAllOnes: Send, Atomic<u8>: Send,

§

impl Sync for DirectIoFile
where I32NotAllOnes: Sync, Mutex<RawMutex, ()>: Sync,

§

impl Unpin for DirectIoFile
where I32NotAllOnes: Unpin, Align1<u8>: Unpin,

§

impl UnsafeUnpin for DirectIoFile
where I32NotAllOnes: UnsafeUnpin, Align1<u8>: UnsafeUnpin,

§

impl UnwindSafe for DirectIoFile
where I32NotAllOnes: UnwindSafe, Align1<u8>: UnwindSafe,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.