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
impl FileDescriptor
Sourcepub unsafe fn dup2<F: AsRawFileDescriptor>(
f: &F,
dest_fd: RawFd,
) -> Result<Self>
pub unsafe fn dup2<F: AsRawFileDescriptor>( f: &F, dest_fd: RawFd, ) -> Result<Self>
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
impl FileDescriptor
Sourcepub fn new<F: IntoRawFileDescriptor>(f: F) -> Self
pub fn new<F: IntoRawFileDescriptor>(f: F) -> Self
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.
Sourcepub fn dup<F: AsRawFileDescriptor>(f: &F) -> Result<Self>
pub fn dup<F: AsRawFileDescriptor>(f: &F) -> Result<Self>
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.
Sourcepub fn try_clone(&self) -> Result<Self>
pub fn try_clone(&self) -> Result<Self>
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.
Sourcepub fn as_stdio(&self) -> Result<Stdio>
pub fn as_stdio(&self) -> Result<Stdio>
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.
Sourcepub fn set_non_blocking(&mut self, non_blocking: bool) -> Result<()>
pub fn set_non_blocking(&mut self, non_blocking: bool) -> Result<()>
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.
Sourcepub fn redirect_stdio<F: AsRawFileDescriptor>(
f: &F,
stdio: StdioDescriptor,
) -> Result<Self>
pub fn redirect_stdio<F: AsRawFileDescriptor>( f: &F, stdio: StdioDescriptor, ) -> Result<Self>
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 AsRawFd for FileDescriptor
impl AsRawFd for FileDescriptor
Source§impl Debug for FileDescriptor
impl Debug for FileDescriptor
Source§impl FromRawFd for FileDescriptor
impl FromRawFd for FileDescriptor
Source§unsafe fn from_raw_fd(fd: RawFd) -> Self
unsafe fn from_raw_fd(fd: RawFd) -> Self
Self
from the given raw file
descriptor. Read moreSource§impl IntoRawFd for FileDescriptor
impl IntoRawFd for FileDescriptor
Source§fn into_raw_fd(self) -> RawFd
fn into_raw_fd(self) -> RawFd
Source§impl Read for FileDescriptor
impl Read for FileDescriptor
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moreSource§impl Write for FileDescriptor
impl Write for FileDescriptor
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)