]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/windows/ext.rs
1d63da813c9837a201d574cf4bc646fbb320a456
[rust.git] / src / libstd / sys / windows / ext.rs
1 // Copyright 2014 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 //! Experimental extensions to `std` for Windows.
12 //!
13 //! For now, this module is limited to extracting handles, file
14 //! descriptors, and sockets, but its functionality will grow over
15 //! time.
16
17 #![unstable(feature = "std_misc")]
18
19 pub use sys_common::wtf8::{Wtf8Buf, EncodeWide};
20
21 use ffi::{OsStr, OsString};
22 use fs::{self, OpenOptions};
23 use libc;
24 use net;
25 use sys::os_str::Buf;
26 use sys_common::{AsInner, FromInner, AsInnerMut};
27
28 use old_io;
29
30 /// Raw HANDLEs.
31 pub type Handle = libc::HANDLE;
32
33 /// Raw SOCKETs.
34 pub type Socket = libc::SOCKET;
35
36 /// Extract raw handles.
37 pub trait AsRawHandle {
38     /// Extract the raw handle, without taking any ownership.
39     fn as_raw_handle(&self) -> Handle;
40 }
41
42 #[allow(deprecated)]
43 impl AsRawHandle for old_io::fs::File {
44     fn as_raw_handle(&self) -> Handle {
45         self.as_inner().handle()
46     }
47 }
48
49 impl AsRawHandle for fs::File {
50     fn as_raw_handle(&self) -> Handle {
51         self.as_inner().handle().raw()
52     }
53 }
54
55 impl AsRawHandle for old_io::pipe::PipeStream {
56     fn as_raw_handle(&self) -> Handle {
57         self.as_inner().handle()
58     }
59 }
60
61 #[allow(deprecated)]
62 impl AsRawHandle for old_io::net::pipe::UnixStream {
63     fn as_raw_handle(&self) -> Handle {
64         self.as_inner().handle()
65     }
66 }
67
68 #[allow(deprecated)]
69 impl AsRawHandle for old_io::net::pipe::UnixListener {
70     fn as_raw_handle(&self) -> Handle {
71         self.as_inner().handle()
72     }
73 }
74
75 #[allow(deprecated)]
76 impl AsRawHandle for old_io::net::pipe::UnixAcceptor {
77     fn as_raw_handle(&self) -> Handle {
78         self.as_inner().handle()
79     }
80 }
81
82 /// Extract raw sockets.
83 pub trait AsRawSocket {
84     fn as_raw_socket(&self) -> Socket;
85 }
86
87 #[allow(deprecated)]
88 impl AsRawSocket for old_io::net::tcp::TcpStream {
89     fn as_raw_socket(&self) -> Socket {
90         self.as_inner().fd()
91     }
92 }
93
94 #[allow(deprecated)]
95 impl AsRawSocket for old_io::net::tcp::TcpListener {
96     fn as_raw_socket(&self) -> Socket {
97         self.as_inner().socket()
98     }
99 }
100
101 #[allow(deprecated)]
102 impl AsRawSocket for old_io::net::tcp::TcpAcceptor {
103     fn as_raw_socket(&self) -> Socket {
104         self.as_inner().socket()
105     }
106 }
107
108 #[allow(deprecated)]
109 impl AsRawSocket for old_io::net::udp::UdpSocket {
110     fn as_raw_socket(&self) -> Socket {
111         self.as_inner().fd()
112     }
113 }
114
115 impl AsRawSocket for net::TcpStream {
116     fn as_raw_socket(&self) -> Socket { *self.as_inner().socket().as_inner() }
117 }
118 impl AsRawSocket for net::TcpListener {
119     fn as_raw_socket(&self) -> Socket { *self.as_inner().socket().as_inner() }
120 }
121 impl AsRawSocket for net::UdpSocket {
122     fn as_raw_socket(&self) -> Socket { *self.as_inner().socket().as_inner() }
123 }
124
125 /// Windows-specific extensions to `OsString`.
126 pub trait OsStringExt {
127     /// Create an `OsString` from a potentially ill-formed UTF-16 slice of 16-bit code units.
128     ///
129     /// This is lossless: calling `.encode_wide()` on the resulting string
130     /// will always return the original code units.
131     fn from_wide(wide: &[u16]) -> Self;
132 }
133
134 impl OsStringExt for OsString {
135     fn from_wide(wide: &[u16]) -> OsString {
136         FromInner::from_inner(Buf { inner: Wtf8Buf::from_wide(wide) })
137     }
138 }
139
140 /// Windows-specific extensions to `OsStr`.
141 pub trait OsStrExt {
142     /// Re-encode an `OsStr` as a wide character sequence,
143     /// i.e. potentially ill-formed UTF-16.
144     ///
145     /// This is lossless. Note that the encoding does not include a final null.
146     fn encode_wide(&self) -> EncodeWide;
147 }
148
149 impl OsStrExt for OsStr {
150     fn encode_wide(&self) -> EncodeWide {
151         self.as_inner().inner.encode_wide()
152     }
153 }
154
155 // Windows-specific extensions to `OpenOptions`
156 pub trait OpenOptionsExt {
157     /// Override the `dwDesiredAccess` argument to the call to `CreateFile` with
158     /// the specified value.
159     fn desired_access(&mut self, access: i32) -> &mut Self;
160
161     /// Override the `dwCreationDisposition` argument to the call to
162     /// `CreateFile` with the specified value.
163     ///
164     /// This will override any values of the standard `create` flags, for
165     /// example.
166     fn creation_disposition(&mut self, val: i32) -> &mut Self;
167
168     /// Override the `dwFlagsAndAttributes` argument to the call to
169     /// `CreateFile` with the specified value.
170     ///
171     /// This will override any values of the standard flags on the `OpenOptions`
172     /// structure.
173     fn flags_and_attributes(&mut self, val: i32) -> &mut Self;
174
175     /// Override the `dwShareMode` argument to the call to `CreateFile` with the
176     /// specified value.
177     ///
178     /// This will override any values of the standard flags on the `OpenOptions`
179     /// structure.
180     fn share_mode(&mut self, val: i32) -> &mut Self;
181 }
182
183 impl OpenOptionsExt for OpenOptions {
184     fn desired_access(&mut self, access: i32) -> &mut OpenOptions {
185         self.as_inner_mut().desired_access(access); self
186     }
187     fn creation_disposition(&mut self, access: i32) -> &mut OpenOptions {
188         self.as_inner_mut().creation_disposition(access); self
189     }
190     fn flags_and_attributes(&mut self, access: i32) -> &mut OpenOptions {
191         self.as_inner_mut().flags_and_attributes(access); self
192     }
193     fn share_mode(&mut self, access: i32) -> &mut OpenOptions {
194         self.as_inner_mut().share_mode(access); self
195     }
196 }
197
198 /// A prelude for conveniently writing platform-specific code.
199 ///
200 /// Includes all extension traits, and some important type definitions.
201 pub mod prelude {
202     #[doc(no_inline)]
203     pub use super::{Socket, Handle, AsRawSocket, AsRawHandle};
204     #[doc(no_inline)]
205     pub use super::{OsStrExt, OsStringExt};
206     #[doc(no_inline)]
207     pub use super::OpenOptionsExt;
208 }