|
| 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 | +} |
0 commit comments