8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- #![ doc( html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png" ,
12
- html_favicon_url = "https://doc.rust-lang.org/favicon.ico" ,
13
- html_root_url = "https://doc.rust-lang.org/net2-rs" ) ]
11
+ //! Utilities for handling sockets
12
+ //!
13
+ //! This crate is sort of an evolution of the `net2` crate after seeing the
14
+ //! issues on it over time. The intention of this crate is to provide as direct
15
+ //! as possible access to the system's functionality for sockets as possible. No
16
+ //! extra fluff (e.g. multiple syscalls or builders) provided in this crate. As
17
+ //! a result using this crate can be a little wordy, but it should give you
18
+ //! maximal flexibility over configuration of sockets.
19
+ //!
20
+ //! # Examples
21
+ //!
22
+ //! ```no_run
23
+ //! use socket2::{Socket, Domain, Type};
24
+ //!
25
+ //! // create a TCP listener bound to two addresses
26
+ //! let socket = Socket::new(Domain::ipv4(), Type::stream(), None).unwrap();
27
+ //!
28
+ //! socket.bind(&"127.0.0.1:12345".parse().unwrap()).unwrap();
29
+ //! socket.bind(&"127.0.0.1:12346".parse().unwrap()).unwrap();
30
+ //! socket.listen(128).unwrap();
31
+ //!
32
+ //! let listener = socket.into_tcp_listener();
33
+ //! // ...
34
+ //! ```
35
+
36
+ #![ doc( html_root_url = "https://docs.rs/socket2/0.1" ) ]
37
+ #![ deny( missing_docs) ]
14
38
15
39
#[ cfg( unix) ] extern crate libc;
16
40
#[ cfg( unix) ] #[ macro_use] extern crate cfg_if;
19
43
#[ cfg( windows) ] extern crate winapi;
20
44
#[ cfg( windows) ] extern crate ws2_32;
21
45
22
-
23
46
use utils:: NetInt ;
24
47
25
48
mod socket;
@@ -28,14 +51,63 @@ mod utils;
28
51
#[ cfg( unix) ] #[ path = "sys/unix/mod.rs" ] mod sys;
29
52
#[ cfg( windows) ] #[ path = "sys/windows.rs" ] mod sys;
30
53
54
+ /// Newtype, owned, wrapper around a system socket.
55
+ ///
56
+ /// This type simply wraps an instance of a file descriptor (`c_int`) on Unix
57
+ /// and an instance of `SOCKET` on Windows. This is the main type exported by
58
+ /// this crate and is intended to mirror the raw semantics of sockets on
59
+ /// platforms as closely as possible. Almost all methods correspond to
60
+ /// precisely one libc or OS API call which is essentially just a "Rustic
61
+ /// translation" of what's below.
62
+ ///
63
+ /// # Examples
64
+ ///
65
+ /// ```no_run
66
+ /// use socket2::{Socket, Domain, Type};
67
+ ///
68
+ /// // create a TCP listener bound to two addresses
69
+ /// let socket = Socket::new(Domain::ipv4(), Type::stream(), None).unwrap();
70
+ ///
71
+ /// socket.bind(&"127.0.0.1:12345".parse().unwrap()).unwrap();
72
+ /// socket.bind(&"127.0.0.1:12346".parse().unwrap()).unwrap();
73
+ /// socket.listen(128).unwrap();
74
+ ///
75
+ /// let listener = socket.into_tcp_listener();
76
+ /// // ...
77
+ /// ```
31
78
pub struct Socket {
32
79
inner : sys:: Socket ,
33
80
}
34
81
82
+ /// Specification of the communication domain for a socket.
83
+ ///
84
+ /// This is a newtype wrapper around an integer which provides a nicer API in
85
+ /// addition to an injection point for documentation. Convenience constructors
86
+ /// such as `Domain::ipv4`, `Domain::ipv6`, etc, are provided to avoid reaching
87
+ /// into libc for various constants.
88
+ ///
89
+ /// This type is freely interconvertible with the `i32` type, however, if a raw
90
+ /// value needs to be provided.
35
91
pub struct Domain ( i32 ) ;
36
92
93
+ /// Specification of communication semantics on a socket.
94
+ ///
95
+ /// This is a newtype wrapper around an integer which provides a nicer API in
96
+ /// addition to an injection point for documentation. Convenience constructors
97
+ /// such as `Type::stream`, `Type::dgram`, etc, are provided to avoid reaching
98
+ /// into libc for various constants.
99
+ ///
100
+ /// This type is freely interconvertible with the `i32` type, however, if a raw
101
+ /// value needs to be provided.
37
102
pub struct Type ( i32 ) ;
38
103
104
+ /// Protocol specification used for creating sockets via `Socket::new`.
105
+ ///
106
+ /// This is a newtype wrapper around an integer which provides a nicer API in
107
+ /// addition to an injection point for documentation.
108
+ ///
109
+ /// This type is freely interconvertible with the `i32` type, however, if a raw
110
+ /// value needs to be provided.
39
111
pub struct Protocol ( i32 ) ;
40
112
41
113
fn hton < I : NetInt > ( i : I ) -> I { i. to_be ( ) }
0 commit comments