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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! Poll-based asynchronous byte I/O traits.
//!
//! This module contains the core [`AsyncRead`] and [`AsyncWrite`] traits used by
//! runite's files, sockets, process pipes, and adapters. Implementations expose
//! non-blocking poll methods; extension traits such as
//! [`AsyncReadExt`](super::AsyncReadExt) turn those poll methods into futures for
//! everyday async code.
//!
//! # Examples
//!
//! ```
//! use core::pin::Pin;
//! use core::task::{Context, Poll};
//! use std::cell::RefCell;
//! use std::io;
//! use std::rc::Rc;
//!
//! use runite::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
//!
//! struct Bytes(&'static [u8]);
//!
//! impl AsyncRead for Bytes {
//! fn poll_read(
//! mut self: Pin<&mut Self>,
//! _cx: &mut Context<'_>,
//! buf: &mut [u8],
//! ) -> Poll<io::Result<usize>> {
//! let read = buf.len().min(self.0.len());
//! buf[..read].copy_from_slice(&self.0[..read]);
//! self.0 = &self.0[read..];
//! Poll::Ready(Ok(read))
//! }
//! }
//!
//! #[derive(Clone)]
//! struct Sink(Rc<RefCell<Vec<u8>>>);
//!
//! impl AsyncWrite for Sink {
//! fn poll_write(
//! self: Pin<&mut Self>,
//! _cx: &mut Context<'_>,
//! buf: &[u8],
//! ) -> Poll<io::Result<usize>> {
//! self.0.borrow_mut().extend_from_slice(buf);
//! Poll::Ready(Ok(buf.len()))
//! }
//!
//! fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
//! Poll::Ready(Ok(()))
//! }
//!
//! fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
//! Poll::Ready(Ok(()))
//! }
//! }
//!
//! let written = Rc::new(RefCell::new(Vec::new()));
//! let observed = Rc::clone(&written);
//! runite::spawn(async move {
//! let mut reader = Bytes(b"ping");
//! let mut buf = [0; 4];
//! reader.read_exact(&mut buf).await.unwrap();
//!
//! let mut writer = Sink(written);
//! writer.write_all(&buf).await.unwrap();
//! writer.flush().await.unwrap();
//! });
//! runite::run();
//! assert_eq!(&*observed.borrow(), b"ping");
//! ```
use Pin;
use ;
use io;
/// Asynchronous byte-oriented input.
///
/// `AsyncRead` is the polling primitive behind [`AsyncReadExt`](super::AsyncReadExt)
/// and buffered readers. Implementors attempt to copy bytes into `buf` without
/// blocking the current thread. If no bytes are currently available, return
/// [`Poll::Pending`] and arrange for `cx.waker()` to be woken when progress may
/// be possible.
///
/// Returning `Poll::Ready(Ok(0))` means either EOF or that `buf` was empty. A
/// successful nonzero return value is the number of bytes initialized in `buf`.
/// Futures built on this trait are thread-local in runite and need not be
/// [`Send`](core::marker::Send).
///
/// # Examples
///
/// ```
/// use core::pin::Pin;
/// use core::task::{Context, Poll};
/// use std::io;
///
/// use runite::io::{AsyncRead, AsyncReadExt};
///
/// struct Bytes(&'static [u8]);
///
/// impl AsyncRead for Bytes {
/// fn poll_read(
/// mut self: Pin<&mut Self>,
/// _cx: &mut Context<'_>,
/// buf: &mut [u8],
/// ) -> Poll<io::Result<usize>> {
/// let read = buf.len().min(self.0.len());
/// buf[..read].copy_from_slice(&self.0[..read]);
/// self.0 = &self.0[read..];
/// Poll::Ready(Ok(read))
/// }
/// }
///
/// runite::spawn(async {
/// let mut reader = Bytes(b"runite");
/// let mut out = [0; 6];
/// reader.read_exact(&mut out).await.unwrap();
/// assert_eq!(&out, b"runite");
/// });
/// runite::run();
/// ```
/// Asynchronous byte-oriented output.
///
/// `AsyncWrite` is the polling primitive behind [`AsyncWriteExt`](super::AsyncWriteExt)
/// and buffered writers. Implementors attempt to accept bytes from `buf` without
/// blocking the current thread. If no progress is currently possible, return
/// [`Poll::Pending`] and wake `cx.waker()` when the writer may be ready again.
///
/// `poll_write` may accept fewer bytes than were provided. Callers that require
/// the whole buffer to be written should use
/// [`write_all`](super::AsyncWriteExt::write_all). Futures built on this trait
/// are thread-local in runite and need not be [`Send`](core::marker::Send).
///
/// # Examples
///
/// ```
/// use core::pin::Pin;
/// use core::task::{Context, Poll};
/// use std::cell::RefCell;
/// use std::io;
/// use std::rc::Rc;
///
/// use runite::io::{AsyncWrite, AsyncWriteExt};
///
/// #[derive(Clone)]
/// struct Sink(Rc<RefCell<Vec<u8>>>);
///
/// impl AsyncWrite for Sink {
/// fn poll_write(
/// self: Pin<&mut Self>,
/// _cx: &mut Context<'_>,
/// buf: &[u8],
/// ) -> Poll<io::Result<usize>> {
/// self.0.borrow_mut().extend_from_slice(buf);
/// Poll::Ready(Ok(buf.len()))
/// }
///
/// fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
/// Poll::Ready(Ok(()))
/// }
///
/// fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
/// Poll::Ready(Ok(()))
/// }
/// }
///
/// let written = Rc::new(RefCell::new(Vec::new()));
/// let observed = Rc::clone(&written);
/// runite::spawn(async move {
/// let mut writer = Sink(written);
/// writer.write_all(b"bytes").await.unwrap();
/// writer.flush().await.unwrap();
/// });
/// runite::run();
/// assert_eq!(&*observed.borrow(), b"bytes");
/// ```