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>(f: &F, dest_fd: i32) -> Result<FileDescriptor, Error>where
F: AsRawFileDescriptor,
pub unsafe fn dup2<F>(f: &F, dest_fd: i32) -> Result<FileDescriptor, Error>where
F: AsRawFileDescriptor,
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>(f: F) -> FileDescriptor ⓘwhere
F: IntoRawFileDescriptor,
pub fn new<F>(f: F) -> FileDescriptor ⓘwhere
F: IntoRawFileDescriptor,
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>(f: &F) -> Result<FileDescriptor, Error>where
F: AsRawFileDescriptor,
pub fn dup<F>(f: &F) -> Result<FileDescriptor, Error>where
F: AsRawFileDescriptor,
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<FileDescriptor, Error>
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.
Sourcepub fn as_stdio(&self) -> Result<Stdio, Error>
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.
Sourcepub fn as_file(&self) -> Result<File, Error>
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.
Sourcepub fn set_non_blocking(&mut self, non_blocking: bool) -> Result<(), Error>
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.
Sourcepub fn redirect_stdio<F>(
f: &F,
stdio: StdioDescriptor,
) -> Result<FileDescriptor, Error>where
F: AsRawFileDescriptor,
pub fn redirect_stdio<F>(
f: &F,
stdio: StdioDescriptor,
) -> Result<FileDescriptor, Error>where
F: AsRawFileDescriptor,
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
impl AsFd for FileDescriptor
Source§fn as_fd(&self) -> BorrowedFd<'_>
fn as_fd(&self) -> BorrowedFd<'_>
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: i32) -> FileDescriptor ⓘ
unsafe fn from_raw_fd(fd: i32) -> FileDescriptor ⓘ
Self from the given raw file
descriptor. Read moreSource§impl IntoRawFd for FileDescriptor
impl IntoRawFd for FileDescriptor
Source§fn into_raw_fd(self) -> i32
fn into_raw_fd(self) -> i32
Source§impl Read for FileDescriptor
impl Read for FileDescriptor
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
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, Error>
fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
Source§fn flush(&mut self) -> Result<(), Error>
fn flush(&mut self) -> Result<(), Error>
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)Auto Trait Implementations§
impl Freeze for FileDescriptor
impl RefUnwindSafe for FileDescriptor
impl Send for FileDescriptor
impl Sync for FileDescriptor
impl Unpin for FileDescriptor
impl UnwindSafe for FileDescriptor
Blanket Implementations§
Source§impl<T> AsFilelike for Twhere
T: AsFd,
impl<T> AsFilelike for Twhere
T: AsFd,
Source§fn as_filelike(&self) -> BorrowedFd<'_>
fn as_filelike(&self) -> BorrowedFd<'_>
Source§fn as_filelike_view<Target>(&self) -> FilelikeView<'_, Target>where
Target: FilelikeViewType,
fn as_filelike_view<Target>(&self) -> FilelikeView<'_, Target>where
Target: FilelikeViewType,
&Target. Read moreSource§impl<T> AsRawFileDescriptor for Twhere
T: AsRawFd,
impl<T> AsRawFileDescriptor for Twhere
T: AsRawFd,
fn as_raw_file_descriptor(&self) -> i32
Source§impl<T> AsRawFilelike for Twhere
T: AsRawFd,
impl<T> AsRawFilelike for Twhere
T: AsRawFd,
Source§fn as_raw_filelike(&self) -> i32
fn as_raw_filelike(&self) -> i32
Source§impl<T> AsRawSocketDescriptor for Twhere
T: AsRawFd,
impl<T> AsRawSocketDescriptor for Twhere
T: AsRawFd,
fn as_socket_descriptor(&self) -> i32
Source§impl<T> AsRawSocketlike for Twhere
T: AsRawFd,
impl<T> AsRawSocketlike for Twhere
T: AsRawFd,
Source§fn as_raw_socketlike(&self) -> i32
fn as_raw_socketlike(&self) -> i32
Source§impl<T> AsSocketlike for Twhere
T: AsFd,
impl<T> AsSocketlike for Twhere
T: AsFd,
Source§fn as_socketlike(&self) -> BorrowedFd<'_>
fn as_socketlike(&self) -> BorrowedFd<'_>
Source§fn as_socketlike_view<Target>(&self) -> SocketlikeView<'_, Target>where
Target: SocketlikeViewType,
fn as_socketlike_view<Target>(&self) -> SocketlikeView<'_, Target>where
Target: SocketlikeViewType,
&Target. Read moreSource§impl<T> AsSource for Twhere
T: AsFd,
impl<T> AsSource for Twhere
T: AsFd,
Source§fn source(&self) -> BorrowedFd<'_>
fn source(&self) -> BorrowedFd<'_>
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> FromRawFileDescriptor for Twhere
T: FromRawFd,
impl<T> FromRawFileDescriptor for Twhere
T: FromRawFd,
unsafe fn from_raw_file_descriptor(fd: i32) -> T
Source§impl<T> FromRawFilelike for Twhere
T: FromRawFd,
impl<T> FromRawFilelike for Twhere
T: FromRawFd,
Source§unsafe fn from_raw_filelike(raw: i32) -> T
unsafe fn from_raw_filelike(raw: i32) -> T
Self from the raw value. Read moreSource§impl<T> FromRawSocketDescriptor for Twhere
T: FromRawFd,
impl<T> FromRawSocketDescriptor for Twhere
T: FromRawFd,
unsafe fn from_socket_descriptor(fd: i32) -> T
Source§impl<T> FromRawSocketlike for Twhere
T: FromRawFd,
impl<T> FromRawSocketlike for Twhere
T: FromRawFd,
Source§unsafe fn from_raw_socketlike(raw: i32) -> T
unsafe fn from_raw_socketlike(raw: i32) -> T
Self from the raw value. Read more