Skip to content

Commit 1e66164

Browse files
committed
libs: add std::os::windows module
The new `std::os::windows` module exposes several extension traits for extracting file descriptors, sockets, and handles from `std::io` types.
1 parent af0c446 commit 1e66164

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
lines changed

src/libstd/os.rs

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ use vec::Vec;
6262
#[cfg(unix)] use c_str::ToCStr;
6363
#[cfg(unix)] use libc::c_char;
6464

65+
#[cfg(unix)]
66+
pub use sys::ext as unix;
67+
#[cfg(windows)]
68+
pub use sys::ext as windows;
69+
6570
/// Get the number of cores available
6671
pub fn num_cpus() -> uint {
6772
unsafe {

src/libstd/sys/windows/ext.rs

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
#![experimental]
18+
19+
use sys_common::AsInner;
20+
use libc;
21+
22+
use io;
23+
24+
/// Raw HANDLEs.
25+
pub type Handle = libc::HANDLE;
26+
27+
/// Raw SOCKETs.
28+
pub type Socket = libc::SOCKET;
29+
30+
/// Extract raw handles.
31+
pub trait AsRawHandle {
32+
/// Extract the raw handle, without taking any ownership.
33+
fn as_raw_handle(&self) -> Handle;
34+
}
35+
36+
impl AsRawHandle for io::fs::File {
37+
fn as_raw_handle(&self) -> Handle {
38+
self.as_inner().handle()
39+
}
40+
}
41+
42+
impl AsRawHandle for io::pipe::PipeStream {
43+
fn as_raw_handle(&self) -> Handle {
44+
self.as_inner().handle()
45+
}
46+
}
47+
48+
impl AsRawHandle for io::net::pipe::UnixStream {
49+
fn as_raw_handle(&self) -> Handle {
50+
self.as_inner().handle()
51+
}
52+
}
53+
54+
impl AsRawHandle for io::net::pipe::UnixListener {
55+
fn as_raw_handle(&self) -> Handle {
56+
self.as_inner().handle()
57+
}
58+
}
59+
60+
impl AsRawHandle for io::net::pipe::UnixAcceptor {
61+
fn as_raw_handle(&self) -> Handle {
62+
self.as_inner().handle()
63+
}
64+
}
65+
66+
/// Extract raw sockets.
67+
pub trait AsRawSocket {
68+
fn as_raw_socket(&self) -> Socket;
69+
}
70+
71+
impl AsRawSocket for io::net::tcp::TcpStream {
72+
fn as_raw_socket(&self) -> Socket {
73+
self.as_inner().fd()
74+
}
75+
}
76+
77+
impl AsRawSocket for io::net::tcp::TcpListener {
78+
fn as_raw_socket(&self) -> Socket {
79+
self.as_inner().fd()
80+
}
81+
}
82+
83+
impl AsRawSocket for io::net::tcp::TcpAcceptor {
84+
fn as_raw_socket(&self) -> Socket {
85+
self.as_inner().fd()
86+
}
87+
}
88+
89+
impl AsRawSocket for io::net::udp::UdpSocket {
90+
fn as_raw_socket(&self) -> Socket {
91+
self.as_inner().fd()
92+
}
93+
}
94+
95+
/// A prelude for conveniently writing platform-specific code.
96+
///
97+
/// Includes all extension traits, and some important type definitions.
98+
pub mod prelude {
99+
pub use super::{Socket, Handle, AsRawSocket, AsRawHandle};
100+
}

src/libstd/sys/windows/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ macro_rules! helper_init( (static $name:ident: Helper<$m:ty>) => (
3434
) )
3535

3636
pub mod c;
37+
pub mod ext;
3738
pub mod fs;
3839
pub mod os;
3940
pub mod tcp;

src/libstd/sys/windows/pipe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ impl UnixAcceptor {
735735
}
736736

737737
pub fn handle(&self) -> libc::HANDLE {
738-
self.event.ref0
738+
self.listener.handle()
739739
}
740740
}
741741

0 commit comments

Comments
 (0)