[go: up one dir, main page]

romio/
lib.rs

1//! *O Romio, Romio, wherefore art thou Romio?*<br/>
2//! *Deny thy father and refuse thy name;*<br/>
3//! *Or if thou wilt not, be but sworn my love*<br/>
4//! *And I'll no longer be so synchronous.*
5//!
6//! # Asynchronous networking bindings for TCP, UDP, and UDS
7//!
8//! The types defined in this module are designed to closely follow the APIs of the
9//! analogous types in `std::net`. But rather than implementing synchronous traits
10//! like `std::io::{Read, Write}`, these types implement the asychronous versions
11//! provided by the `futures-preview` crate, i.e. `futures::io::{AsyncRead, AsyncWrite}`.
12//! When using `async`/`await` syntax, the experience should be quite similar to
13//! traditional blocking code that uses `std::net`.
14//!
15//! Because futures-preview is currently unstable, this crate requires
16//! nightly Rust.
17//!
18//! # Examples
19//! __TCP Server__
20//! ```rust
21//! use romio::tcp::{TcpListener, TcpStream};
22//! use futures::prelude::*;
23//!
24//! async fn say_hello(mut stream: TcpStream) {
25//!     stream.write_all(b"Shall I hear more, or shall I speak at this?").await;
26//! }
27//!
28//! async fn listen() -> Result<(), Box<dyn std::error::Error + 'static>> {
29//!     let socket_addr = "127.0.0.1:8080".parse()?;
30//!     let mut listener = TcpListener::bind(&socket_addr)?;
31//!     let mut incoming = listener.incoming();
32//!
33//!     // accept connections and process them serially
34//!     while let Some(stream) = incoming.next().await {
35//!         say_hello(stream?).await;
36//!     }
37//!     Ok(())
38//! }
39//! ```
40//! __TCP Client__
41//! ```rust,no_run
42//! use std::error::Error;
43//! use futures::prelude::*;
44//! use romio::tcp::{TcpListener, TcpStream};
45//!
46//! async fn receive_sonnet() -> Result<(), Box<dyn Error + 'static>> {
47//!     let socket_addr = "127.0.0.1:8080".parse()?;
48//!     let mut buffer = vec![];
49//!     let mut stream = TcpStream::connect(&socket_addr).await?;
50//!
51//!     stream.read(&mut buffer).await?;
52//!     println!("{:?}", buffer);
53//!     Ok(())
54//! }
55//! ```
56
57#![deny(missing_docs, missing_debug_implementations)]
58#![cfg_attr(test, deny(warnings))]
59
60pub mod tcp;
61pub mod udp;
62
63#[cfg(unix)]
64pub mod uds;
65
66pub mod raw;
67
68mod reactor;
69
70#[doc(inline)]
71pub use crate::tcp::{TcpListener, TcpStream};
72#[doc(inline)]
73pub use crate::udp::UdpSocket;