]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/ext/io.rs
std: Push process stdio setup in std::sys
[rust.git] / src / libstd / sys / unix / ext / io.rs
1 // Copyright 2015 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 os::raw;
18 use sys;
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 = raw::c_int;
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 { *self.as_inner().socket().as_inner() }
97 }
98 #[stable(feature = "rust1", since = "1.0.0")]
99 impl AsRawFd for net::TcpListener {
100     fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
101 }
102 #[stable(feature = "rust1", since = "1.0.0")]
103 impl AsRawFd for net::UdpSocket {
104     fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
105 }
106
107 #[stable(feature = "from_raw_os", since = "1.1.0")]
108 impl FromRawFd for net::TcpStream {
109     unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
110         let socket = sys::net::Socket::from_inner(fd);
111         net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(socket))
112     }
113 }
114 #[stable(feature = "from_raw_os", since = "1.1.0")]
115 impl FromRawFd for net::TcpListener {
116     unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
117         let socket = sys::net::Socket::from_inner(fd);
118         net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(socket))
119     }
120 }
121 #[stable(feature = "from_raw_os", since = "1.1.0")]
122 impl FromRawFd for net::UdpSocket {
123     unsafe fn from_raw_fd(fd: RawFd) -> net::UdpSocket {
124         let socket = sys::net::Socket::from_inner(fd);
125         net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(socket))
126     }
127 }
128
129 #[stable(feature = "into_raw_os", since = "1.4.0")]
130 impl IntoRawFd for net::TcpStream {
131     fn into_raw_fd(self) -> RawFd {
132         self.into_inner().into_socket().into_inner()
133     }
134 }
135 #[stable(feature = "into_raw_os", since = "1.4.0")]
136 impl IntoRawFd for net::TcpListener {
137     fn into_raw_fd(self) -> RawFd {
138         self.into_inner().into_socket().into_inner()
139     }
140 }
141 #[stable(feature = "into_raw_os", since = "1.4.0")]
142 impl IntoRawFd for net::UdpSocket {
143     fn into_raw_fd(self) -> RawFd {
144         self.into_inner().into_socket().into_inner()
145     }
146 }