2015-04-15 23:21:13 -07:00
|
|
|
//! Unix-specific extensions to general I/O primitives
|
|
|
|
|
|
|
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::fs;
|
|
|
|
|
use crate::os::raw;
|
|
|
|
|
use crate::sys;
|
|
|
|
|
use crate::io;
|
|
|
|
|
use crate::sys_common::{AsInner, FromInner, IntoInner};
|
2015-04-15 23:21:13 -07:00
|
|
|
|
|
|
|
|
/// Raw file descriptors.
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
pub type RawFd = raw::c_int;
|
|
|
|
|
|
|
|
|
|
/// A trait to extract the raw unix file descriptor from an underlying
|
|
|
|
|
/// object.
|
|
|
|
|
///
|
|
|
|
|
/// This is only available on unix platforms and must be imported in order
|
|
|
|
|
/// to call the method. Windows platforms have a corresponding `AsRawHandle`
|
|
|
|
|
/// and `AsRawSocket` set of traits.
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
pub trait AsRawFd {
|
|
|
|
|
/// Extracts the raw file descriptor.
|
|
|
|
|
///
|
|
|
|
|
/// This method does **not** pass ownership of the raw file descriptor
|
2015-12-30 19:31:28 +02:00
|
|
|
/// to the caller. The descriptor is only guaranteed to be valid while
|
2015-04-15 23:21:13 -07:00
|
|
|
/// the original object has not yet been destroyed.
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
fn as_raw_fd(&self) -> RawFd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A trait to express the ability to construct an object from a raw file
|
|
|
|
|
/// descriptor.
|
2015-05-05 20:25:14 -04:00
|
|
|
#[stable(feature = "from_raw_os", since = "1.1.0")]
|
2015-04-15 23:21:13 -07:00
|
|
|
pub trait FromRawFd {
|
2016-12-04 00:48:11 -08:00
|
|
|
/// Constructs a new instance of `Self` from the given raw file
|
2015-04-15 23:21:13 -07:00
|
|
|
/// descriptor.
|
|
|
|
|
///
|
|
|
|
|
/// This function **consumes ownership** of the specified file
|
|
|
|
|
/// descriptor. The returned object will take responsibility for closing
|
|
|
|
|
/// it when the object goes out of scope.
|
|
|
|
|
///
|
|
|
|
|
/// This function is also unsafe as the primitives currently returned
|
|
|
|
|
/// have the contract that they are the sole owner of the file
|
|
|
|
|
/// descriptor they are wrapping. Usage of this function could
|
|
|
|
|
/// accidentally allow violating this contract which can cause memory
|
|
|
|
|
/// unsafety in code that relies on it being true.
|
2015-05-05 20:25:14 -04:00
|
|
|
#[stable(feature = "from_raw_os", since = "1.1.0")]
|
2015-04-15 23:21:13 -07:00
|
|
|
unsafe fn from_raw_fd(fd: RawFd) -> Self;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-15 23:31:24 -07:00
|
|
|
/// A trait to express the ability to consume an object and acquire ownership of
|
|
|
|
|
/// its raw file descriptor.
|
2015-09-10 13:26:44 -07:00
|
|
|
#[stable(feature = "into_raw_os", since = "1.4.0")]
|
2015-07-15 23:31:24 -07:00
|
|
|
pub trait IntoRawFd {
|
|
|
|
|
/// Consumes this object, returning the raw underlying file descriptor.
|
|
|
|
|
///
|
|
|
|
|
/// This function **transfers ownership** of the underlying file descriptor
|
|
|
|
|
/// to the caller. Callers are then the unique owners of the file descriptor
|
|
|
|
|
/// and must close the descriptor once it's no longer needed.
|
2015-09-10 13:26:44 -07:00
|
|
|
#[stable(feature = "into_raw_os", since = "1.4.0")]
|
2015-07-15 23:31:24 -07:00
|
|
|
fn into_raw_fd(self) -> RawFd;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-15 23:21:13 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
|
impl AsRawFd for fs::File {
|
|
|
|
|
fn as_raw_fd(&self) -> RawFd {
|
|
|
|
|
self.as_inner().fd().raw()
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-05-05 20:25:14 -04:00
|
|
|
#[stable(feature = "from_raw_os", since = "1.1.0")]
|
2015-04-15 23:21:13 -07:00
|
|
|
impl FromRawFd for fs::File {
|
|
|
|
|
unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
|
2015-05-05 16:35:15 -07:00
|
|
|
fs::File::from_inner(sys::fs::File::from_inner(fd))
|
2015-04-15 23:21:13 -07:00
|
|
|
}
|
|
|
|
|
}
|
2015-09-10 13:26:44 -07:00
|
|
|
#[stable(feature = "into_raw_os", since = "1.4.0")]
|
2015-07-15 23:31:24 -07:00
|
|
|
impl IntoRawFd for fs::File {
|
|
|
|
|
fn into_raw_fd(self) -> RawFd {
|
|
|
|
|
self.into_inner().into_fd().into_raw()
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-15 23:21:13 -07:00
|
|
|
|
2017-07-25 16:44:25 -07:00
|
|
|
#[stable(feature = "asraw_stdio", since = "1.21.0")]
|
2017-07-24 14:44:16 -07:00
|
|
|
impl AsRawFd for io::Stdin {
|
|
|
|
|
fn as_raw_fd(&self) -> RawFd { libc::STDIN_FILENO }
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-25 16:44:25 -07:00
|
|
|
#[stable(feature = "asraw_stdio", since = "1.21.0")]
|
2017-07-24 14:44:16 -07:00
|
|
|
impl AsRawFd for io::Stdout {
|
|
|
|
|
fn as_raw_fd(&self) -> RawFd { libc::STDOUT_FILENO }
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-25 16:44:25 -07:00
|
|
|
#[stable(feature = "asraw_stdio", since = "1.21.0")]
|
2017-07-24 14:44:16 -07:00
|
|
|
impl AsRawFd for io::Stderr {
|
|
|
|
|
fn as_raw_fd(&self) -> RawFd { libc::STDERR_FILENO }
|
|
|
|
|
}
|