Skip to content

Commit 400598c

Browse files
committed
Docs and a few more From
1 parent 1df97b7 commit 400598c

File tree

4 files changed

+348
-8
lines changed

4 files changed

+348
-8
lines changed

src/lib.rs

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,33 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

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)]
1438

1539
#[cfg(unix)] extern crate libc;
1640
#[cfg(unix)] #[macro_use] extern crate cfg_if;
@@ -19,7 +43,6 @@
1943
#[cfg(windows)] extern crate winapi;
2044
#[cfg(windows)] extern crate ws2_32;
2145

22-
2346
use utils::NetInt;
2447

2548
mod socket;
@@ -28,14 +51,63 @@ mod utils;
2851
#[cfg(unix)] #[path = "sys/unix/mod.rs"] mod sys;
2952
#[cfg(windows)] #[path = "sys/windows.rs"] mod sys;
3053

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+
/// ```
3178
pub struct Socket {
3279
inner: sys::Socket,
3380
}
3481

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.
3591
pub struct Domain(i32);
3692

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.
37102
pub struct Type(i32);
38103

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.
39111
pub struct Protocol(i32);
40112

41113
fn hton<I: NetInt>(i: I) -> I { i.to_be() }

0 commit comments

Comments
 (0)