[][src]Struct filedescriptor::FileDescriptor

pub struct FileDescriptor { /* fields omitted */ }

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};
use std::io::Write;

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

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

Implementations

impl FileDescriptor[src]

pub fn new<F: IntoRawFileDescriptor>(f: F) -> Self[src]

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.

pub fn dup<F: AsRawFileDescriptor>(f: &F) -> Result<Self>[src]

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.

pub fn try_clone(&self) -> Result<Self>[src]

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.

pub fn as_stdio(&self) -> Result<Stdio>[src]

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.

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

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.

Trait Implementations

impl AsRawFd for FileDescriptor[src]

impl Debug for FileDescriptor[src]

impl FromRawFd for FileDescriptor[src]

impl IntoRawFd for FileDescriptor[src]

impl Read for FileDescriptor[src]

impl Write for FileDescriptor[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> AsRawFileDescriptor for T where
    T: AsRawFd
[src]

impl<T> AsRawSocketDescriptor for T where
    T: AsRawFd
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> FromRawFileDescriptor for T where
    T: FromRawFd
[src]

impl<T> FromRawSocketDescriptor for T where
    T: FromRawFd
[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> IntoRawFileDescriptor for T where
    T: IntoRawFd
[src]

impl<T> IntoRawSocketDescriptor for T where
    T: IntoRawFd
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.