pub struct OpenFileDescription { /* private fields */ }Expand description
State of a file opened for reading and/or writing
Implementations§
Source§impl OpenFileDescription
impl OpenFileDescription
Sourcepub fn is_readable(&self) -> bool
pub fn is_readable(&self) -> bool
Returns true if you can read from this open file description.
Sourcepub fn is_writable(&self) -> bool
pub fn is_writable(&self) -> bool
Returns true if you can write to this open file description.
Sourcepub fn is_nonblocking(&self) -> bool
pub fn is_nonblocking(&self) -> bool
Returns true if this open file description is in non-blocking mode.
Sourcepub fn set_nonblocking(&mut self, is_nonblocking: bool)
pub fn set_nonblocking(&mut self, is_nonblocking: bool)
Sets whether this open file description is in non-blocking mode.
Sourcepub fn is_ready_for_reading(&self) -> bool
pub fn is_ready_for_reading(&self) -> bool
Returns true if a read operation on this open file description would not block.
Sourcepub fn is_ready_for_writing(&self) -> bool
pub fn is_ready_for_writing(&self) -> bool
Returns true if a write operation on this open file description would not block.
Sourcepub fn read(&mut self, buffer: &mut [u8]) -> Result<usize, Errno>
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.
Sourcepub fn poll_read<F>(
&mut self,
buffer: &mut [u8],
get_waker: F,
) -> Poll<Result<usize, Errno>>
pub fn poll_read<F>( &mut self, buffer: &mut [u8], get_waker: F, ) -> Poll<Result<usize, Errno>>
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.
Sourcepub fn write(&mut self, buffer: &[u8]) -> Result<usize, Errno>
pub fn write(&mut self, buffer: &[u8]) -> Result<usize, Errno>
Writes to this open file description.
Returns the number of bytes successfully written.
Sourcepub fn poll_write<F>(
&mut self,
buffer: &[u8],
get_waker: F,
) -> Poll<Result<usize, Errno>>
pub fn poll_write<F>( &mut self, buffer: &[u8], get_waker: F, ) -> Poll<Result<usize, Errno>>
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.
Sourcepub fn poll_write_full<F>(
&mut self,
buffer: &[u8],
bytes_written: &mut usize,
get_waker: F,
) -> Poll<Result<usize, Errno>>
pub fn poll_write_full<F>( &mut self, buffer: &[u8], bytes_written: &mut usize, get_waker: F, ) -> Poll<Result<usize, Errno>>
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.
Trait Implementations§
Source§impl Clone for OpenFileDescription
impl Clone for OpenFileDescription
Source§fn clone(&self) -> OpenFileDescription
fn clone(&self) -> OpenFileDescription
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for OpenFileDescription
impl Debug for OpenFileDescription
Auto Trait Implementations§
impl Freeze for OpenFileDescription
impl !RefUnwindSafe for OpenFileDescription
impl !Send for OpenFileDescription
impl !Sync for OpenFileDescription
impl Unpin for OpenFileDescription
impl UnsafeUnpin for OpenFileDescription
impl !UnwindSafe for OpenFileDescription
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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