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.
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 //! Experimental extensions to `std` for Windows.
13 //! For now, this module is limited to extracting handles, file
14 //! descriptors, and sockets, but its functionality will grow over
17 #![unstable(feature = "std_misc")]
19 pub use sys_common::wtf8::{Wtf8Buf, EncodeWide};
21 use ffi::{OsStr, OsString};
22 use fs::{self, OpenOptions};
26 use sys_common::{AsInner, FromInner, AsInnerMut};
31 pub type Handle = libc::HANDLE;
34 pub type Socket = libc::SOCKET;
36 /// Extract raw handles.
37 pub trait AsRawHandle {
38 /// Extract the raw handle, without taking any ownership.
39 fn as_raw_handle(&self) -> Handle;
43 impl AsRawHandle for old_io::fs::File {
44 fn as_raw_handle(&self) -> Handle {
45 self.as_inner().handle()
49 impl AsRawHandle for fs::File {
50 fn as_raw_handle(&self) -> Handle {
51 self.as_inner().handle().raw()
55 impl AsRawHandle for old_io::pipe::PipeStream {
56 fn as_raw_handle(&self) -> Handle {
57 self.as_inner().handle()
62 impl AsRawHandle for old_io::net::pipe::UnixStream {
63 fn as_raw_handle(&self) -> Handle {
64 self.as_inner().handle()
69 impl AsRawHandle for old_io::net::pipe::UnixListener {
70 fn as_raw_handle(&self) -> Handle {
71 self.as_inner().handle()
76 impl AsRawHandle for old_io::net::pipe::UnixAcceptor {
77 fn as_raw_handle(&self) -> Handle {
78 self.as_inner().handle()
82 /// Extract raw sockets.
83 pub trait AsRawSocket {
84 fn as_raw_socket(&self) -> Socket;
88 impl AsRawSocket for old_io::net::tcp::TcpStream {
89 fn as_raw_socket(&self) -> Socket {
95 impl AsRawSocket for old_io::net::tcp::TcpListener {
96 fn as_raw_socket(&self) -> Socket {
97 self.as_inner().socket()
102 impl AsRawSocket for old_io::net::tcp::TcpAcceptor {
103 fn as_raw_socket(&self) -> Socket {
104 self.as_inner().socket()
109 impl AsRawSocket for old_io::net::udp::UdpSocket {
110 fn as_raw_socket(&self) -> Socket {
115 impl AsRawSocket for net::TcpStream {
116 fn as_raw_socket(&self) -> Socket { *self.as_inner().socket().as_inner() }
118 impl AsRawSocket for net::TcpListener {
119 fn as_raw_socket(&self) -> Socket { *self.as_inner().socket().as_inner() }
121 impl AsRawSocket for net::UdpSocket {
122 fn as_raw_socket(&self) -> Socket { *self.as_inner().socket().as_inner() }
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.
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;
134 impl OsStringExt for OsString {
135 fn from_wide(wide: &[u16]) -> OsString {
136 FromInner::from_inner(Buf { inner: Wtf8Buf::from_wide(wide) })
140 /// Windows-specific extensions to `OsStr`.
142 /// Re-encode an `OsStr` as a wide character sequence,
143 /// i.e. potentially ill-formed UTF-16.
145 /// This is lossless. Note that the encoding does not include a final null.
146 fn encode_wide(&self) -> EncodeWide;
149 impl OsStrExt for OsStr {
150 fn encode_wide(&self) -> EncodeWide {
151 self.as_inner().inner.encode_wide()
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;
161 /// Override the `dwCreationDisposition` argument to the call to
162 /// `CreateFile` with the specified value.
164 /// This will override any values of the standard `create` flags, for
166 fn creation_disposition(&mut self, val: i32) -> &mut Self;
168 /// Override the `dwFlagsAndAttributes` argument to the call to
169 /// `CreateFile` with the specified value.
171 /// This will override any values of the standard flags on the `OpenOptions`
173 fn flags_and_attributes(&mut self, val: i32) -> &mut Self;
175 /// Override the `dwShareMode` argument to the call to `CreateFile` with the
178 /// This will override any values of the standard flags on the `OpenOptions`
180 fn share_mode(&mut self, val: i32) -> &mut Self;
183 impl OpenOptionsExt for OpenOptions {
184 fn desired_access(&mut self, access: i32) -> &mut OpenOptions {
185 self.as_inner_mut().desired_access(access); self
187 fn creation_disposition(&mut self, access: i32) -> &mut OpenOptions {
188 self.as_inner_mut().creation_disposition(access); self
190 fn flags_and_attributes(&mut self, access: i32) -> &mut OpenOptions {
191 self.as_inner_mut().flags_and_attributes(access); self
193 fn share_mode(&mut self, access: i32) -> &mut OpenOptions {
194 self.as_inner_mut().share_mode(access); self
198 /// A prelude for conveniently writing platform-specific code.
200 /// Includes all extension traits, and some important type definitions.
203 pub use super::{Socket, Handle, AsRawSocket, AsRawHandle};
205 pub use super::{OsStrExt, OsStringExt};
207 pub use super::OpenOptionsExt;