Struct RandFile

Source
pub struct RandFile<T> { /* private fields */ }
Expand description

The file object that supports random access. Since in D4 file, we actually use a random access file mode, which means all the read and write needs to provide the address in file. And this is the object that provides the low level random access interface.

At the same time, this RandFile object is synchronized, which means we guarantee the thread safety that each block of data is written to file correctly (without overlaps).

The rand file provides a offset-based file access API and data can be read and write from the specified address in blocks. But rand file itself doesn’t tracking the block size and it’s the upper layer’s responsibility to determine the correct block beginning.

Implementations§

Source§

impl<T> RandFile<T>

Source

pub fn clone_inner(&self) -> Result<T>
where T: Clone,

Source

pub fn lock(&mut self, update_fn: Box<dyn FnOnce() + Send>) -> Result<Self>

Lock the current IO object and derive a fresh token This will prevent any object that holds earlier token from locking this file again. However, the freshly returned token can be cloned.

Source§

impl<T: Read + Write + Seek> RandFile<T>

Source

pub fn for_read_write(inner: T) -> Self

The convenient helper function to create a read-write random file

  • inner: The underlying implementation for this backend
Source§

impl<T: Write + Seek> RandFile<T>

Source

pub fn append_block(&mut self, buf: &[u8]) -> Result<u64>

Append a block to the random accessing file the return value is the relative address compare to the last accessed block.

  • buf: The data buffer that needs to be write
  • returns: The absolute address of the block that has been written to the file.
Source

pub fn update_block(&mut self, offset: u64, buf: &[u8]) -> Result<()>

Update a data block with the given data buffer.

  • offset: The offset of the data block
  • buf: The data buffer to write
Source

pub fn reserve_block(&mut self, size: usize) -> Result<u64>

Reserve some space in the rand file. This is useful when we want to reserve a data block for future use. This is very useful for some volatile data (for example the directory block), etc. And later, we are able to use update_block function to keep the reserved block up-to-dated

Source§

impl<T: Read + Seek> RandFile<T>

Source

pub fn size(&mut self) -> Result<u64>

Source

pub fn read_block(&mut self, addr: u64, buf: &mut [u8]) -> Result<usize>

Read a block from the random accessing file the size of the buffer slice is equal to the number of bytes that is requesting But there might not be enough bytes available for read, thus we always return the actual number of bytes is loaded

Trait Implementations§

Source§

impl<T> Clone for RandFile<T>

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Drop for RandFile<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for RandFile<T>

§

impl<T> RefUnwindSafe for RandFile<T>

§

impl<T> Send for RandFile<T>
where T: Send,

§

impl<T> Sync for RandFile<T>
where T: Send,

§

impl<T> Unpin for RandFile<T>

§

impl<T> UnwindSafe for RandFile<T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.