1 //! Unix-specific extensions to general I/O primitives
3 #![stable(feature = "rust1", since = "1.0.0")]
9 use sys_common::{AsInner, FromInner, IntoInner};
12 /// Raw file descriptors.
13 #[stable(feature = "rust1", since = "1.0.0")]
14 pub type RawFd = raw::c_int;
16 /// A trait to extract the raw unix file descriptor from an underlying
19 /// This is only available on unix platforms and must be imported in order
20 /// to call the method. Windows platforms have a corresponding `AsRawHandle`
21 /// and `AsRawSocket` set of traits.
22 #[stable(feature = "rust1", since = "1.0.0")]
24 /// Extracts the raw file descriptor.
26 /// This method does **not** pass ownership of the raw file descriptor
27 /// to the caller. The descriptor is only guaranteed to be valid while
28 /// the original object has not yet been destroyed.
29 #[stable(feature = "rust1", since = "1.0.0")]
30 fn as_raw_fd(&self) -> RawFd;
33 /// A trait to express the ability to construct an object from a raw file
35 #[stable(feature = "from_raw_os", since = "1.1.0")]
37 /// Constructs a new instance of `Self` from the given raw file
40 /// This function **consumes ownership** of the specified file
41 /// descriptor. The returned object will take responsibility for closing
42 /// it when the object goes out of scope.
44 /// This function is also unsafe as the primitives currently returned
45 /// have the contract that they are the sole owner of the file
46 /// descriptor they are wrapping. Usage of this function could
47 /// accidentally allow violating this contract which can cause memory
48 /// unsafety in code that relies on it being true.
49 #[stable(feature = "from_raw_os", since = "1.1.0")]
50 unsafe fn from_raw_fd(fd: RawFd) -> Self;
53 /// A trait to express the ability to consume an object and acquire ownership of
54 /// its raw file descriptor.
55 #[stable(feature = "into_raw_os", since = "1.4.0")]
57 /// Consumes this object, returning the raw underlying file descriptor.
59 /// This function **transfers ownership** of the underlying file descriptor
60 /// to the caller. Callers are then the unique owners of the file descriptor
61 /// and must close the descriptor once it's no longer needed.
62 #[stable(feature = "into_raw_os", since = "1.4.0")]
63 fn into_raw_fd(self) -> RawFd;
66 #[stable(feature = "rust1", since = "1.0.0")]
67 impl AsRawFd for fs::File {
68 fn as_raw_fd(&self) -> RawFd {
69 self.as_inner().fd().raw()
72 #[stable(feature = "from_raw_os", since = "1.1.0")]
73 impl FromRawFd for fs::File {
74 unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
75 fs::File::from_inner(sys::fs::File::from_inner(fd))
78 #[stable(feature = "into_raw_os", since = "1.4.0")]
79 impl IntoRawFd for fs::File {
80 fn into_raw_fd(self) -> RawFd {
81 self.into_inner().into_fd().into_raw()
85 #[stable(feature = "asraw_stdio", since = "1.21.0")]
86 impl AsRawFd for io::Stdin {
87 fn as_raw_fd(&self) -> RawFd { libc::STDIN_FILENO }
90 #[stable(feature = "asraw_stdio", since = "1.21.0")]
91 impl AsRawFd for io::Stdout {
92 fn as_raw_fd(&self) -> RawFd { libc::STDOUT_FILENO }
95 #[stable(feature = "asraw_stdio", since = "1.21.0")]
96 impl AsRawFd for io::Stderr {
97 fn as_raw_fd(&self) -> RawFd { libc::STDERR_FILENO }