]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/redox/ext/io.rs
implement `AsRawFd` for stdio locks
[rust.git] / src / libstd / sys / redox / ext / io.rs
1 //! Unix-specific extensions to general I/O primitives
2
3 #![stable(feature = "rust1", since = "1.0.0")]
4
5 use crate::fs;
6 use crate::net;
7 use crate::sys;
8 use crate::io;
9 use crate::sys_common::{self, AsInner, FromInner, IntoInner};
10
11 /// Raw file descriptors.
12 #[stable(feature = "rust1", since = "1.0.0")]
13 pub type RawFd = usize;
14
15 /// A trait to extract the raw unix file descriptor from an underlying
16 /// object.
17 ///
18 /// This is only available on unix platforms and must be imported in order
19 /// to call the method. Windows platforms have a corresponding `AsRawHandle`
20 /// and `AsRawSocket` set of traits.
21 #[stable(feature = "rust1", since = "1.0.0")]
22 pub trait AsRawFd {
23     /// Extracts the raw file descriptor.
24     ///
25     /// This method does **not** pass ownership of the raw file descriptor
26     /// to the caller. The descriptor is only guaranteed to be valid while
27     /// the original object has not yet been destroyed.
28     #[stable(feature = "rust1", since = "1.0.0")]
29     fn as_raw_fd(&self) -> RawFd;
30 }
31
32 /// A trait to express the ability to construct an object from a raw file
33 /// descriptor.
34 #[stable(feature = "from_raw_os", since = "1.1.0")]
35 pub trait FromRawFd {
36     /// Constructs a new instances of `Self` from the given raw file
37     /// descriptor.
38     ///
39     /// This function **consumes ownership** of the specified file
40     /// descriptor. The returned object will take responsibility for closing
41     /// it when the object goes out of scope.
42     ///
43     /// This function is also unsafe as the primitives currently returned
44     /// have the contract that they are the sole owner of the file
45     /// descriptor they are wrapping. Usage of this function could
46     /// accidentally allow violating this contract which can cause memory
47     /// unsafety in code that relies on it being true.
48     #[stable(feature = "from_raw_os", since = "1.1.0")]
49     unsafe fn from_raw_fd(fd: RawFd) -> Self;
50 }
51
52 /// A trait to express the ability to consume an object and acquire ownership of
53 /// its raw file descriptor.
54 #[stable(feature = "into_raw_os", since = "1.4.0")]
55 pub trait IntoRawFd {
56     /// Consumes this object, returning the raw underlying file descriptor.
57     ///
58     /// This function **transfers ownership** of the underlying file descriptor
59     /// to the caller. Callers are then the unique owners of the file descriptor
60     /// and must close the descriptor once it's no longer needed.
61     #[stable(feature = "into_raw_os", since = "1.4.0")]
62     fn into_raw_fd(self) -> RawFd;
63 }
64
65 #[stable(feature = "rust1", since = "1.0.0")]
66 impl AsRawFd for fs::File {
67     fn as_raw_fd(&self) -> RawFd {
68         self.as_inner().fd().raw()
69     }
70 }
71 #[stable(feature = "from_raw_os", since = "1.1.0")]
72 impl FromRawFd for fs::File {
73     unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
74         fs::File::from_inner(sys::fs::File::from_inner(fd))
75     }
76 }
77 #[stable(feature = "into_raw_os", since = "1.4.0")]
78 impl IntoRawFd for fs::File {
79     fn into_raw_fd(self) -> RawFd {
80         self.into_inner().into_fd().into_raw()
81     }
82 }
83
84 #[stable(feature = "rust1", since = "1.0.0")]
85 impl AsRawFd for net::TcpStream {
86     fn as_raw_fd(&self) -> RawFd {
87         self.as_inner().as_inner().fd().raw()
88     }
89 }
90 #[stable(feature = "rust1", since = "1.0.0")]
91 impl AsRawFd for net::TcpListener {
92     fn as_raw_fd(&self) -> RawFd {
93         self.as_inner().as_inner().fd().raw()
94     }
95 }
96 #[stable(feature = "rust1", since = "1.0.0")]
97 impl AsRawFd for net::UdpSocket {
98     fn as_raw_fd(&self) -> RawFd {
99         self.as_inner().as_inner().fd().raw()
100     }
101 }
102
103 #[stable(feature = "asraw_stdio", since = "1.21.0")]
104 impl AsRawFd for io::Stdin {
105     fn as_raw_fd(&self) -> RawFd { 0 }
106 }
107
108 #[stable(feature = "asraw_stdio", since = "1.21.0")]
109 impl AsRawFd for io::Stdout {
110     fn as_raw_fd(&self) -> RawFd { 1 }
111 }
112
113 #[stable(feature = "asraw_stdio", since = "1.21.0")]
114 impl AsRawFd for io::Stderr {
115     fn as_raw_fd(&self) -> RawFd { 2 }
116 }
117
118 #[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
119 impl<'a> AsRawFd for io::StdinLock<'a> {
120     fn as_raw_fd(&self) -> RawFd { 0 }
121 }
122
123 #[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
124 impl<'a> AsRawFd for io::StdoutLock<'a> {
125     fn as_raw_fd(&self) -> RawFd { 1 }
126 }
127
128 #[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
129 impl<'a> AsRawFd for io::StderrLock<'a> {
130     fn as_raw_fd(&self) -> RawFd { 2 }
131 }
132
133 #[stable(feature = "from_raw_os", since = "1.1.0")]
134 impl FromRawFd for net::TcpStream {
135     unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
136         let file = sys::fs::File::from_inner(fd);
137         net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(file))
138     }
139 }
140 #[stable(feature = "from_raw_os", since = "1.1.0")]
141 impl FromRawFd for net::TcpListener {
142     unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
143         let file = sys::fs::File::from_inner(fd);
144         net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(file))
145     }
146 }
147 #[stable(feature = "from_raw_os", since = "1.1.0")]
148 impl FromRawFd for net::UdpSocket {
149     unsafe fn from_raw_fd(fd: RawFd) -> net::UdpSocket {
150         let file = sys::fs::File::from_inner(fd);
151         net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(file))
152     }
153 }
154
155 #[stable(feature = "into_raw_os", since = "1.4.0")]
156 impl IntoRawFd for net::TcpStream {
157     fn into_raw_fd(self) -> RawFd {
158         self.into_inner().into_inner().into_fd().into_raw()
159     }
160 }
161 #[stable(feature = "into_raw_os", since = "1.4.0")]
162 impl IntoRawFd for net::TcpListener {
163     fn into_raw_fd(self) -> RawFd {
164         self.into_inner().into_inner().into_fd().into_raw()
165     }
166 }
167 #[stable(feature = "into_raw_os", since = "1.4.0")]
168 impl IntoRawFd for net::UdpSocket {
169     fn into_raw_fd(self) -> RawFd {
170         self.into_inner().into_inner().into_fd().into_raw()
171     }
172 }