]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/redox/ext/io.rs
Auto merge of #43484 - estebank:point-to-return, r=arielb1
[rust.git] / src / libstd / sys / redox / ext / io.rs
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.
4 //
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.
10
11 //! Unix-specific extensions to general I/O primitives
12
13 #![stable(feature = "rust1", since = "1.0.0")]
14
15 use fs;
16 use net;
17 use sys;
18 use io;
19 use sys_common::{self, AsInner, FromInner, IntoInner};
20
21 /// Raw file descriptors.
22 #[stable(feature = "rust1", since = "1.0.0")]
23 pub type RawFd = usize;
24
25 /// A trait to extract the raw unix file descriptor from an underlying
26 /// object.
27 ///
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")]
32 pub trait AsRawFd {
33     /// Extracts the raw file descriptor.
34     ///
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;
40 }
41
42 /// A trait to express the ability to construct an object from a raw file
43 /// descriptor.
44 #[stable(feature = "from_raw_os", since = "1.1.0")]
45 pub trait FromRawFd {
46     /// Constructs a new instances of `Self` from the given raw file
47     /// descriptor.
48     ///
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.
52     ///
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;
60 }
61
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")]
65 pub trait IntoRawFd {
66     /// Consumes this object, returning the raw underlying file descriptor.
67     ///
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;
73 }
74
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()
79     }
80 }
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))
85     }
86 }
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()
91     }
92 }
93
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()
98     }
99 }
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()
104     }
105 }
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()
110     }
111 }
112
113 #[stable(feature = "asraw_stdio", since = "1.21.0")]
114 impl AsRawFd for io::Stdin {
115     fn as_raw_fd(&self) -> RawFd { 0 }
116 }
117
118 #[stable(feature = "asraw_stdio", since = "1.21.0")]
119 impl AsRawFd for io::Stdout {
120     fn as_raw_fd(&self) -> RawFd { 1 }
121 }
122
123 #[stable(feature = "asraw_stdio", since = "1.21.0")]
124 impl AsRawFd for io::Stderr {
125     fn as_raw_fd(&self) -> RawFd { 2 }
126 }
127
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))
133     }
134 }
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))
140     }
141 }
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))
147     }
148 }
149
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()
154     }
155 }
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()
160     }
161 }
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()
166     }
167 }