1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//! Async TCP, using the standard library's own types.
//!
//! There is no `nezuko::TcpStream` here. These are free functions that take
//! `std::net::TcpListener` / `TcpStream` and make them await-able, which keeps
//! the crate small and lets you see exactly where the async part happens.
//!
//! **You must set the socket to non-blocking yourself** with
//! `set_nonblocking(true)` before handing it to these functions. That is what
//! makes the OS say "not ready yet" instead of parking the thread - and "not
//! ready yet" is precisely the moment these functions hand the socket to the
//! reactor and return `Pending`.
//!
//! ```no_run
//! use nezuko::{Runtime, accept, spawn, write_all};
//! use std::net::TcpListener;
//!
//! let rt = Runtime::new().unwrap();
//!
//! rt.block_on(async {
//! let mut listener = TcpListener::bind("127.0.0.1:8080").unwrap();
//! listener.set_nonblocking(true).unwrap();
//!
//! loop {
//! let (mut socket, _addr) = accept(&mut listener).await.unwrap();
//! // One task per connection; the loop is free to take the next one.
//! spawn(async move {
//! write_all(b"hello\n", &mut socket).await.unwrap();
//! });
//! }
//! });
//! ```
//!
//! All three functions follow the same shape, so read one and you have read
//! them all: try the operation; if it succeeds you're done, and if it comes
//! back `WouldBlock`, register the fd with the reactor and return `Pending`.
use ;
use crateHandle;
/// Wait for someone to connect, without blocking the thread.
///
/// Gives you the new connection and the address it came from. The returned
/// stream is already set to non-blocking, so you can pass it straight to
/// [`write_all`] or [`print_all`].
///
/// `listener` must be non-blocking: call `set_nonblocking(true)` on it first.
///
/// ```no_run
/// # use nezuko::{Runtime, accept};
/// # use std::net::TcpListener;
/// # let rt = Runtime::new().unwrap();
/// # rt.block_on(async {
/// let mut listener = TcpListener::bind("127.0.0.1:9000").unwrap();
/// listener.set_nonblocking(true).unwrap();
///
/// let (stream, addr) = accept(&mut listener).await.unwrap();
/// println!("connected from {addr}");
/// # });
/// ```
pub async
/// Send every byte of `buf` down `stream`.
///
/// A single write often only takes part of the buffer, so this keeps going
/// until it is all gone, waiting whenever the socket's send buffer fills up.
///
/// ```no_run
/// # use nezuko::{Runtime, write_all};
/// # use std::net::TcpStream;
/// # let rt = Runtime::new().unwrap();
/// # rt.block_on(async {
/// let mut stream = TcpStream::connect("127.0.0.1:9000").unwrap();
/// stream.set_nonblocking(true).unwrap();
///
/// write_all(b"GET / HTTP/1.0\r\n\r\n", &mut stream).await.unwrap();
/// # });
/// ```
///
/// # Errors
///
/// Any socket error, plus [`WriteZero`](io::ErrorKind::WriteZero) if the peer
/// stops accepting data before the buffer is finished.
pub async
/// Read from `stream` and print it to stdout until the other side hangs up.
///
/// Handy for a quick client or for watching what a server sends back.
///
/// ```no_run
/// # use nezuko::{Runtime, print_all};
/// # use std::net::TcpStream;
/// # let rt = Runtime::new().unwrap();
/// # rt.block_on(async {
/// let mut stream = TcpStream::connect("127.0.0.1:9000").unwrap();
/// stream.set_nonblocking(true).unwrap();
///
/// // Returns once the server closes the connection.
/// print_all(&mut stream).await.unwrap();
/// # });
/// ```
pub async