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

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.

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.

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.

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.

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.

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.

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

Extracts the raw file descriptor. Read more

Formats the value using the given formatter. Read more

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

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

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

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

🔬 This is a nightly-only experimental API. (can_vector)

Determines if this Reader has an efficient read_vectored implementation. Read more

Read all bytes until EOF in this source, placing them into buf. Read more

Read all bytes until EOF in this source, appending them to buf. Read more

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

🔬 This is a nightly-only experimental API. (read_buf)

Pull some bytes from this source into the specified buffer. Read more

🔬 This is a nightly-only experimental API. (read_buf)

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

Creates a “by reference” adaptor for this instance of Read. Read more

Transforms this Read instance to an Iterator over its bytes. Read more

Creates an adapter which will chain this stream with another. Read more

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

Write a buffer into this writer, returning how many bytes were written. Read more

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more

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

🔬 This is a nightly-only experimental API. (can_vector)

Determines if this Writer has an efficient write_vectored implementation. Read more

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

🔬 This is a nightly-only experimental API. (write_all_vectored)

Attempts to write multiple buffers into this writer. Read more

Writes a formatted string into this writer, returning any error encountered. Read more

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.