Skip to main content

OpenFileDescription

Struct OpenFileDescription 

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

State of a file opened for reading and/or writing

Implementations§

Source§

impl OpenFileDescription

Source

pub fn is_readable(&self) -> bool

Returns true if you can read from this open file description.

Source

pub fn is_writable(&self) -> bool

Returns true if you can write to this open file description.

Source

pub fn is_nonblocking(&self) -> bool

Returns true if this open file description is in non-blocking mode.

Source

pub fn set_nonblocking(&mut self, is_nonblocking: bool)

Sets whether this open file description is in non-blocking mode.

Source

pub fn is_ready_for_reading(&self) -> bool

Returns true if a read operation on this open file description would not block.

Source

pub fn is_ready_for_writing(&self) -> bool

Returns true if a write operation on this open file description would not block.

Source

pub fn read(&mut self, buffer: &mut [u8]) -> Result<usize, Errno>

Reads from this open file description.

Returns the number of bytes successfully read.

This function does not support blocking read. If the file is not ready for reading, it returns Err(Errno::EAGAIN). Use poll_read for polling support.

Source

pub fn poll_read<F>( &mut self, buffer: &mut [u8], get_waker: F, ) -> Poll<Result<usize, Errno>>
where F: FnMut() -> Weak<Cell<Option<Waker>>>,

Polls for the result of reading from this open file description.

The get_waker parameter is a function that returns a weak reference to the waker of the current task. It is used to register the waker for pending read operations on files like FIFOs. The function is called only when the read operation would block, so it can be used to avoid unnecessary allocations of wakers when the operation can complete immediately. Since the waker is passed as a weak reference, the caller must ensure that there is a strong reference to the waker that lives at least until the file body wakes it up, otherwise the weak reference may become invalid and the task may not be woken up correctly. The waker is wrapped in Cell<Option<Waker>> to allow it to be shared among multiple wake conditions and to allow it to be taken by the first condition that wakes the task.

The returned Poll indicates whether the read operation has completed or is still pending. If it is Poll::Ready, the contained Result indicates whether the read was successful and how many bytes were read, or if it failed with an error. If it is Poll::Pending, it means a waker has been registered and the caller should wait until it is woken up, when this method should be called again.

Source

pub fn write(&mut self, buffer: &[u8]) -> Result<usize, Errno>

Writes to this open file description.

Returns the number of bytes successfully written.

Source

pub fn poll_write<F>( &mut self, buffer: &[u8], get_waker: F, ) -> Poll<Result<usize, Errno>>
where F: FnMut() -> Weak<Cell<Option<Waker>>>,

Polls for the result of writing to this open file description.

The get_waker parameter is a function that returns a weak reference to the waker of the current task. It is used to register the waker for pending write operations on files like FIFOs. The function is called only when the write operation would block, so it can be used to avoid unnecessary allocations of wakers when the operation can complete immediately. Since the waker is passed as a weak reference, the caller must ensure that there is a strong reference to the waker that lives at least until the file body wakes it up, otherwise the weak reference may become invalid and the task may not be woken up correctly. The waker is wrapped in Cell<Option<Waker>> to allow it to be shared among multiple wake conditions and to allow it to be taken by the first condition that wakes the task.

The returned Poll indicates whether the write operation has completed or is still pending. If it is Poll::Ready, the contained Result indicates whether the write was successful and how many bytes were written, or if it failed with an error. If it is Poll::Pending, it means a waker has been registered and the caller should wait until it is woken up, when this method should be called again.

Source

pub fn poll_write_full<F>( &mut self, buffer: &[u8], bytes_written: &mut usize, get_waker: F, ) -> Poll<Result<usize, Errno>>
where F: FnMut() -> Weak<Cell<Option<Waker>>>,

Drives a write(2)-equivalent loop on this open file description.

This helper encapsulates the POSIX rule that a single blocking write(2) call to a pipe must, in the absence of a signal, transfer the entire buffer even when the buffer is larger than PIPE_BUF. For a blocking FIFO it repeatedly calls poll_write, advancing bytes_written until either the buffer is exhausted (returns Ready(Ok(*bytes_written))), no more bytes fit and the next call would block (returns Pending after registering the waker), or an error is reported (returns Ready(Err(_)) if no bytes have been transferred yet, or Ready(Ok(*bytes_written)) otherwise).

For non-FIFO files or non-blocking file descriptors it returns after a single poll_write call without looping; this matches POSIX write(2) semantics for those cases.

bytes_written is updated in place so that the caller can resume the operation across multiple polls without losing the running total when a signal interrupts the operation. This function will panic if bytes_written exceeds the length of buffer. get_waker has the same contract as for poll_write.

Source

pub fn seek(&mut self, position: SeekFrom) -> Result<usize, Errno>

Moves the file offset and returns the new offset.

Source

pub fn inode(&self) -> &Rc<RefCell<Inode>>

Returns the i-node this open file description is operating on.

Trait Implementations§

Source§

impl Clone for OpenFileDescription

Source§

fn clone(&self) -> OpenFileDescription

Returns a duplicate 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 Debug for OpenFileDescription

Source§

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

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

impl Drop for OpenFileDescription

Source§

fn drop(&mut self)

Executes the destructor for this type. 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> 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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.