1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Unix-specific extensions to general I/O primitives
13 #![stable(feature = "rust1", since = "1.0.0")]
19 use sys_common::{self, AsInner, FromInner, IntoInner};
21 /// Raw file descriptors.
22 #[stable(feature = "rust1", since = "1.0.0")]
23 pub type RawFd = usize;
25 /// A trait to extract the raw unix file descriptor from an underlying
28 /// This is only available on unix platforms and must be imported in order
29 /// to call the method. Windows platforms have a corresponding `AsRawHandle`
30 /// and `AsRawSocket` set of traits.
31 #[stable(feature = "rust1", since = "1.0.0")]
33 /// Extracts the raw file descriptor.
35 /// This method does **not** pass ownership of the raw file descriptor
36 /// to the caller. The descriptor is only guaranteed to be valid while
37 /// the original object has not yet been destroyed.
38 #[stable(feature = "rust1", since = "1.0.0")]
39 fn as_raw_fd(&self) -> RawFd;
42 /// A trait to express the ability to construct an object from a raw file
44 #[stable(feature = "from_raw_os", since = "1.1.0")]
46 /// Constructs a new instances of `Self` from the given raw file
49 /// This function **consumes ownership** of the specified file
50 /// descriptor. The returned object will take responsibility for closing
51 /// it when the object goes out of scope.
53 /// This function is also unsafe as the primitives currently returned
54 /// have the contract that they are the sole owner of the file
55 /// descriptor they are wrapping. Usage of this function could
56 /// accidentally allow violating this contract which can cause memory
57 /// unsafety in code that relies on it being true.
58 #[stable(feature = "from_raw_os", since = "1.1.0")]
59 unsafe fn from_raw_fd(fd: RawFd) -> Self;
62 /// A trait to express the ability to consume an object and acquire ownership of
63 /// its raw file descriptor.
64 #[stable(feature = "into_raw_os", since = "1.4.0")]
66 /// Consumes this object, returning the raw underlying file descriptor.
68 /// This function **transfers ownership** of the underlying file descriptor
69 /// to the caller. Callers are then the unique owners of the file descriptor
70 /// and must close the descriptor once it's no longer needed.
71 #[stable(feature = "into_raw_os", since = "1.4.0")]
72 fn into_raw_fd(self) -> RawFd;
75 #[stable(feature = "rust1", since = "1.0.0")]
76 impl AsRawFd for fs::File {
77 fn as_raw_fd(&self) -> RawFd {
78 self.as_inner().fd().raw()
81 #[stable(feature = "from_raw_os", since = "1.1.0")]
82 impl FromRawFd for fs::File {
83 unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
84 fs::File::from_inner(sys::fs::File::from_inner(fd))
87 #[stable(feature = "into_raw_os", since = "1.4.0")]
88 impl IntoRawFd for fs::File {
89 fn into_raw_fd(self) -> RawFd {
90 self.into_inner().into_fd().into_raw()
94 #[stable(feature = "rust1", since = "1.0.0")]
95 impl AsRawFd for net::TcpStream {
96 fn as_raw_fd(&self) -> RawFd {
97 self.as_inner().as_inner().fd().raw()
100 #[stable(feature = "rust1", since = "1.0.0")]
101 impl AsRawFd for net::TcpListener {
102 fn as_raw_fd(&self) -> RawFd {
103 self.as_inner().as_inner().fd().raw()
106 #[stable(feature = "rust1", since = "1.0.0")]
107 impl AsRawFd for net::UdpSocket {
108 fn as_raw_fd(&self) -> RawFd {
109 self.as_inner().as_inner().fd().raw()
113 #[stable(feature = "asraw_stdio", since = "1.21.0")]
114 impl AsRawFd for io::Stdin {
115 fn as_raw_fd(&self) -> RawFd { 0 }
118 #[stable(feature = "asraw_stdio", since = "1.21.0")]
119 impl AsRawFd for io::Stdout {
120 fn as_raw_fd(&self) -> RawFd { 1 }
123 #[stable(feature = "asraw_stdio", since = "1.21.0")]
124 impl AsRawFd for io::Stderr {
125 fn as_raw_fd(&self) -> RawFd { 2 }
128 #[stable(feature = "from_raw_os", since = "1.1.0")]
129 impl FromRawFd for net::TcpStream {
130 unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
131 let file = sys::fs::File::from_inner(fd);
132 net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(file))
135 #[stable(feature = "from_raw_os", since = "1.1.0")]
136 impl FromRawFd for net::TcpListener {
137 unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
138 let file = sys::fs::File::from_inner(fd);
139 net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(file))
142 #[stable(feature = "from_raw_os", since = "1.1.0")]
143 impl FromRawFd for net::UdpSocket {
144 unsafe fn from_raw_fd(fd: RawFd) -> net::UdpSocket {
145 let file = sys::fs::File::from_inner(fd);
146 net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(file))
150 #[stable(feature = "into_raw_os", since = "1.4.0")]
151 impl IntoRawFd for net::TcpStream {
152 fn into_raw_fd(self) -> RawFd {
153 self.into_inner().into_inner().into_fd().into_raw()
156 #[stable(feature = "into_raw_os", since = "1.4.0")]
157 impl IntoRawFd for net::TcpListener {
158 fn into_raw_fd(self) -> RawFd {
159 self.into_inner().into_inner().into_fd().into_raw()
162 #[stable(feature = "into_raw_os", since = "1.4.0")]
163 impl IntoRawFd for net::UdpSocket {
164 fn into_raw_fd(self) -> RawFd {
165 self.into_inner().into_inner().into_fd().into_raw()