]> git.proxmox.com Git - rustc.git/blob - vendor/net2/src/lib.rs
New upstream version 1.46.0~beta.2+dfsg1
[rustc.git] / vendor / net2 / src / lib.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 //! Extensions to `std::net` networking types.
12 //!
13 //! This crate implements a number of extensions to the standard `std::net`
14 //! networking types, hopefully being slated for inclusion into the standard
15 //! library in the future. The goal of this crate is to expose all sorts of
16 //! cross-platform and platform-specific configuration options of UDP/TCP
17 //! sockets. System APIs are wrapped with as thin a layer as possible instead of
18 //! bundling multiple actions into one API call.
19 //!
20 //! More information about the design of this crate can be found in the
21 //! [associated rfc][rfc]
22 //!
23 //! [rfc]: https://github.com/rust-lang/rfcs/pull/1158
24 //!
25 //! # Examples
26 //!
27 //! ```no_run
28 //! use net2::TcpBuilder;
29 //!
30 //! let tcp = TcpBuilder::new_v4().unwrap();
31 //! tcp.reuse_address(true).unwrap()
32 //! .only_v6(false).unwrap();
33 //!
34 //! let mut stream = tcp.connect("127.0.0.1:80").unwrap();
35 //!
36 //! // use `stream` as a TcpStream
37 //! ```
38
39 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
40 html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
41 html_root_url = "https://doc.rust-lang.org/net2-rs")]
42 #![deny(missing_docs, warnings)]
43
44 // Silence warnings about deprecated try!() usage
45 #![allow(deprecated)]
46
47 #![cfg_attr(target_os = "wasi", feature(wasi_ext))]
48
49 #[cfg(any(target_os = "redox", target_os = "wasi", unix))] extern crate libc;
50
51 #[cfg(windows)] extern crate winapi;
52
53 #[macro_use] extern crate cfg_if;
54
55 use std::io;
56 use std::ops::Neg;
57 use std::net::{ToSocketAddrs, SocketAddr};
58
59 use utils::{One, NetInt};
60
61 mod tcp;
62 mod udp;
63 mod socket;
64 mod ext;
65 mod utils;
66
67 #[cfg(target_os="redox")] #[path = "sys/redox/mod.rs"] mod sys;
68 #[cfg(unix)] #[path = "sys/unix/mod.rs"] mod sys;
69 #[cfg(windows)] #[path = "sys/windows/mod.rs"] mod sys;
70 #[cfg(target_os = "wasi")] #[path = "sys/wasi/mod.rs"] mod sys;
71 #[cfg(all(unix, not(any(target_os = "solaris", target_os = "illumos"))))] pub mod unix;
72
73 pub use tcp::TcpBuilder;
74 pub use udp::UdpBuilder;
75 pub use ext::{TcpStreamExt, TcpListenerExt, UdpSocketExt};
76
77 fn one_addr<T: ToSocketAddrs>(tsa: T) -> io::Result<SocketAddr> {
78 let mut addrs = try!(tsa.to_socket_addrs());
79 let addr = match addrs.next() {
80 Some(addr) => addr,
81 None => return Err(io::Error::new(io::ErrorKind::Other,
82 "no socket addresses could be resolved"))
83 };
84 if addrs.next().is_none() {
85 Ok(addr)
86 } else {
87 Err(io::Error::new(io::ErrorKind::Other,
88 "more than one address resolved"))
89 }
90 }
91
92 fn cvt<T: One + PartialEq + Neg<Output=T>>(t: T) -> io::Result<T> {
93 let one: T = T::one();
94 if t == -one {
95 Err(io::Error::last_os_error())
96 } else {
97 Ok(t)
98 }
99 }
100
101 #[cfg(windows)]
102 fn cvt_win<T: PartialEq + utils::Zero>(t: T) -> io::Result<T> {
103 if t == T::zero() {
104 Err(io::Error::last_os_error())
105 } else {
106 Ok(t)
107 }
108 }
109
110 fn hton<I: NetInt>(i: I) -> I { i.to_be() }
111
112 fn ntoh<I: NetInt>(i: I) -> I { I::from_be(i) }
113
114 trait AsInner {
115 type Inner;
116 fn as_inner(&self) -> &Self::Inner;
117 }
118
119 trait FromInner {
120 type Inner;
121 fn from_inner(inner: Self::Inner) -> Self;
122 }
123
124 trait IntoInner {
125 type Inner;
126 fn into_inner(self) -> Self::Inner;
127 }