Struct FileDescriptor

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

FileDescriptor is a thin wrapper on top of the OwnedHandle type that exposes the ability to Read and Write to the platform RawFileDescriptor.

This is a bit of a contrived example, but demonstrates how to avoid the conditional code that would otherwise be required to deal with calling as_raw_fd and as_raw_handle:

use filedescriptor::{FileDescriptor, FromRawFileDescriptor, Result};
use std::io::Write;

fn get_stdout() -> Result<FileDescriptor> {
  let stdout = std::io::stdout();
  let handle = stdout.lock();
  FileDescriptor::dup(&handle)
}

fn print_something() -> Result<()> {
   get_stdout()?.write(b"hello")?;
   Ok(())
}

Implementations§

Source§

impl FileDescriptor

Source

pub unsafe fn dup2<F>(f: &F, dest_fd: i32) -> Result<FileDescriptor, Error>

Attempt to duplicate the underlying handle from an object that is representable as the system RawFileDescriptor type and assign it to a destination file descriptor. It then returns a FileDescriptor wrapped around the duplicate. Since the duplication requires kernel resources that may not be available, this is a potentially fallible operation. The returned handle has a separate lifetime from the source, but references the same object at the kernel level.

Source§

impl FileDescriptor

Source

pub fn new<F>(f: F) -> FileDescriptor

Create a new descriptor from some object that is convertible into the system RawFileDescriptor type. This consumes the parameter and replaces it with a FileDescriptor instance.

Source

pub fn dup<F>(f: &F) -> Result<FileDescriptor, Error>

Attempt to duplicate the underlying handle from an object that is representable as the system RawFileDescriptor type and return a FileDescriptor wrapped around the duplicate. Since the duplication requires kernel resources that may not be available, this is a potentially fallible operation. The returned handle has a separate lifetime from the source, but references the same object at the kernel level.

Source

pub fn try_clone(&self) -> Result<FileDescriptor, Error>

Attempt to duplicate the underlying handle and return a FileDescriptor wrapped around the duplicate. Since the duplication requires kernel resources that may not be available, this is a potentially fallible operation. The returned handle has a separate lifetime from the source, but references the same object at the kernel level.

Source

pub fn as_stdio(&self) -> Result<Stdio, Error>

A convenience method for creating a std::process::Stdio object to be used for eg: redirecting the stdio streams of a child process. The Stdio is created using a duplicated handle so that the source handle remains alive.

Source

pub fn as_file(&self) -> Result<File, Error>

A convenience method for creating a std::fs::File object. The File is created using a duplicated handle so that the source handle remains alive.

Source

pub fn set_non_blocking(&mut self, non_blocking: bool) -> Result<(), Error>

Attempt to change the non-blocking IO mode of the file descriptor. Not all kinds of file descriptor can be placed in non-blocking mode on all systems, and some file descriptors will claim to be in non-blocking mode but it will have no effect. File descriptors based on sockets are the most portable type that can be successfully made non-blocking.

Source

pub fn redirect_stdio<F>( f: &F, stdio: StdioDescriptor, ) -> Result<FileDescriptor, Error>

Attempt to redirect stdio to the underlying handle and return a FileDescriptor wrapped around the original stdio source. Since the redirection requires kernel resources that may not be available, this is a potentially fallible operation. Supports stdin, stdout, and stderr redirections.

Trait Implementations§

Source§

impl AsFd for FileDescriptor

Source§

fn as_fd(&self) -> BorrowedFd<'_>

Borrows the file descriptor. Read more
Source§

impl AsRawFd for FileDescriptor

Source§

fn as_raw_fd(&self) -> i32

Extracts the raw file descriptor. Read more
Source§

impl Debug for FileDescriptor

Source§

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

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

impl FromRawFd for FileDescriptor

Source§

unsafe fn from_raw_fd(fd: i32) -> FileDescriptor

Constructs a new instance of Self from the given raw file descriptor. Read more
Source§

impl IntoRawFd for FileDescriptor

Source§

fn into_raw_fd(self) -> i32

Consumes this object, returning the raw underlying file descriptor. Read more
Source§

impl Read for FileDescriptor

Source§

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

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · Source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
Source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · Source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes until EOF in this source, placing them into buf. Read more
1.0.0 · Source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until EOF in this source, appending them to buf. Read more
1.6.0 · Source§

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

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

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
Source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Reads the exact number of bytes required to fill cursor. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adaptor for this instance of Read. Read more
1.0.0 · Source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · Source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · Source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
Source§

impl Write for FileDescriptor

Source§

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

Writes a buffer into this writer, returning how many bytes were written. Read more
Source§

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

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

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

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. 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> AsFilelike for T
where T: AsFd,

Source§

fn as_filelike(&self) -> BorrowedFd<'_>

Borrows the reference. Read more
Source§

fn as_filelike_view<Target>(&self) -> FilelikeView<'_, Target>
where Target: FilelikeViewType,

Return a borrowing view of a resource which dereferences to a &Target. Read more
Source§

impl<T> AsRawFileDescriptor for T
where T: AsRawFd,

Source§

impl<T> AsRawFilelike for T
where T: AsRawFd,

Source§

fn as_raw_filelike(&self) -> i32

Returns the raw value.
Source§

impl<T> AsRawSocketDescriptor for T
where T: AsRawFd,

Source§

impl<T> AsRawSocketlike for T
where T: AsRawFd,

Source§

fn as_raw_socketlike(&self) -> i32

Returns the raw value.
Source§

impl<T> AsSocketlike for T
where T: AsFd,

Source§

fn as_socketlike(&self) -> BorrowedFd<'_>

Borrows the reference.
Source§

fn as_socketlike_view<Target>(&self) -> SocketlikeView<'_, Target>
where Target: SocketlikeViewType,

Return a borrowing view of a resource which dereferences to a &Target. Read more
Source§

impl<T> AsSource for T
where T: AsFd,

Source§

fn source(&self) -> BorrowedFd<'_>

Returns the borrowed file descriptor.
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> FromRawFileDescriptor for T
where T: FromRawFd,

Source§

impl<T> FromRawFilelike for T
where T: FromRawFd,

Source§

unsafe fn from_raw_filelike(raw: i32) -> T

Constructs Self from the raw value. Read more
Source§

impl<T> FromRawSocketDescriptor for T
where T: FromRawFd,

Source§

impl<T> FromRawSocketlike for T
where T: FromRawFd,

Source§

unsafe fn from_raw_socketlike(raw: i32) -> T

Constructs Self from the raw value. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoRawFileDescriptor for T
where T: IntoRawFd,

Source§

impl<T> IntoRawFilelike for T
where T: IntoRawFd,

Source§

fn into_raw_filelike(self) -> i32

Returns the raw value.
Source§

impl<T> IntoRawSocketDescriptor for T
where T: IntoRawFd,

Source§

impl<T> IntoRawSocketlike for T
where T: IntoRawFd,

Source§

fn into_raw_socketlike(self) -> i32

Returns the raw value.
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more