Debian Codegraph @

src/lib.rs

raw symbols SCIP
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 = "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(unix)] #[path = "sys/unix/mod.rs"] mod sys;
68 #[cfg(windows)] #[path = "sys/windows/mod.rs"] mod sys;
69 #[cfg(target_os = "wasi")] #[path = "sys/wasi/mod.rs"] mod sys;
70 #[cfg(all(unix, not(any(target_os = "solaris", target_os = "illumos"))))] pub mod unix;
71
72 pub use tcp::TcpBuilder;
73 pub use udp::UdpBuilder;
74 pub use ext::{TcpStreamExt, TcpListenerExt, UdpSocketExt};
75
76 fn one_addr<T: ToSocketAddrs>(tsa: T) -> io::Result<SocketAddr> {
77 let mut addrs = try!(tsa.to_socket_addrs());
78 let addr = match addrs.next() {
79 Some(addr) => addr,
80 None => return Err(io::Error::new(io::ErrorKind::Other,
81 "no socket addresses could be resolved"))
82 };
83 if addrs.next().is_none() {
84 Ok(addr)
85 } else {
86 Err(io::Error::new(io::ErrorKind::Other,
87 "more than one address resolved"))
88 }
89 }
90
91 fn cvt<T: One + PartialEq + Neg<Output=T>>(t: T) -> io::Result<T> {
92 let one: T = T::one();
93 if t == -one {
94 Err(io::Error::last_os_error())
95 } else {
96 Ok(t)
97 }
98 }
99
100 #[cfg(windows)]
101 fn cvt_win<T: PartialEq + utils::Zero>(t: T) -> io::Result<T> {
102 if t == T::zero() {
103 Err(io::Error::last_os_error())
104 } else {
105 Ok(t)
106 }
107 }
108
109 fn hton<I: NetInt>(i: I) -> I { i.to_be() }
110
111 fn ntoh<I: NetInt>(i: I) -> I { I::from_be(i) }
112
113 trait AsInner {
114 type Inner;
115 fn as_inner(&self) -> &Self::Inner;
116 }
117
118 trait FromInner {
119 type Inner;
120 fn from_inner(inner: Self::Inner) -> Self;
121 }
122
123 trait IntoInner {
124 type Inner;
125 fn into_inner(self) -> Self::Inner;
126 }