Skip to content

Commit 40697e1

Browse files
committed
Implement Read, Write, and Seek for &File, &TcpSocket, etc.
`std` provides these impls, so `cap_std` should too.
1 parent 103f7f0 commit 40697e1

File tree

8 files changed

+568
-0
lines changed

8 files changed

+568
-0
lines changed

cap-async-std/src/fs/file.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,30 @@ impl Read for File {
198198
// async_std doesn't have `initializer`.
199199
}
200200

201+
impl Read for &File {
202+
#[inline]
203+
fn poll_read(
204+
self: Pin<&mut Self>,
205+
cx: &mut Context,
206+
buf: &mut [u8],
207+
) -> Poll<io::Result<usize>> {
208+
Read::poll_read(Pin::new(&mut &self.std), cx, buf)
209+
}
210+
211+
#[inline]
212+
fn poll_read_vectored(
213+
self: Pin<&mut Self>,
214+
cx: &mut Context,
215+
bufs: &mut [IoSliceMut],
216+
) -> Poll<io::Result<usize>> {
217+
Read::poll_read_vectored(Pin::new(&mut &self.std), cx, bufs)
218+
}
219+
220+
// async_std doesn't have `is_read_vectored`.
221+
222+
// async_std doesn't have `initializer`.
223+
}
224+
201225
impl Write for File {
202226
#[inline]
203227
fn poll_write(
@@ -232,6 +256,36 @@ impl Write for File {
232256
// async_std doesn't have `write_all_vectored`.
233257
}
234258

259+
impl Write for &File {
260+
#[inline]
261+
fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<io::Result<usize>> {
262+
Write::poll_write(Pin::new(&mut &self.std), cx, buf)
263+
}
264+
265+
#[inline]
266+
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
267+
Write::poll_flush(Pin::new(&mut &self.std), cx)
268+
}
269+
270+
#[inline]
271+
fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
272+
Write::poll_close(Pin::new(&mut &self.std), cx)
273+
}
274+
275+
#[inline]
276+
fn poll_write_vectored(
277+
self: Pin<&mut Self>,
278+
cx: &mut Context,
279+
bufs: &[IoSlice],
280+
) -> Poll<io::Result<usize>> {
281+
Write::poll_write_vectored(Pin::new(&mut &self.std), cx, bufs)
282+
}
283+
284+
// async_std doesn't have `is_write_vectored`.
285+
286+
// async_std doesn't have `write_all_vectored`.
287+
}
288+
235289
impl Seek for File {
236290
#[inline]
237291
fn poll_seek(
@@ -247,6 +301,17 @@ impl Seek for File {
247301
// async_std doesn't have `seek_convenience`.
248302
}
249303

304+
impl Seek for &File {
305+
#[inline]
306+
fn poll_seek(self: Pin<&mut Self>, cx: &mut Context, pos: SeekFrom) -> Poll<io::Result<u64>> {
307+
Seek::poll_seek(Pin::new(&mut &self.std), cx, pos)
308+
}
309+
310+
// async_std doesn't have `stream_len`.
311+
312+
// async_std doesn't have `seek_convenience`.
313+
}
314+
250315
// TODO: Can async_std implement `From<File>` for `process::Stdio`?
251316

252317
// async_std doesn't have `FileExt`.

cap-async-std/src/fs_utf8/file.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,30 @@ impl Read for File {
170170
// async_std doesn't have `initializer`.
171171
}
172172

173+
impl Read for &File {
174+
#[inline]
175+
fn poll_read(
176+
self: Pin<&mut Self>,
177+
cx: &mut Context,
178+
buf: &mut [u8],
179+
) -> Poll<io::Result<usize>> {
180+
Read::poll_read(Pin::new(&mut &self.cap_std), cx, buf)
181+
}
182+
183+
#[inline]
184+
fn poll_read_vectored(
185+
self: Pin<&mut Self>,
186+
cx: &mut Context,
187+
bufs: &mut [IoSliceMut],
188+
) -> Poll<io::Result<usize>> {
189+
Read::poll_read_vectored(Pin::new(&mut &self.cap_std), cx, bufs)
190+
}
191+
192+
// async_std doesn't have `is_read_vectored`.
193+
194+
// async_std doesn't have `initializer`.
195+
}
196+
173197
impl Write for File {
174198
#[inline]
175199
fn poll_write(
@@ -204,6 +228,36 @@ impl Write for File {
204228
// async_std doesn't have `write_all_vectored`.
205229
}
206230

231+
impl Write for &File {
232+
#[inline]
233+
fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<io::Result<usize>> {
234+
Write::poll_write(Pin::new(&mut &self.cap_std), cx, buf)
235+
}
236+
237+
#[inline]
238+
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
239+
Write::poll_flush(Pin::new(&mut &self.cap_std), cx)
240+
}
241+
242+
#[inline]
243+
fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
244+
Write::poll_close(Pin::new(&mut &self.cap_std), cx)
245+
}
246+
247+
#[inline]
248+
fn poll_write_vectored(
249+
self: Pin<&mut Self>,
250+
cx: &mut Context,
251+
bufs: &[IoSlice],
252+
) -> Poll<io::Result<usize>> {
253+
Write::poll_write_vectored(Pin::new(&mut &self.cap_std), cx, bufs)
254+
}
255+
256+
// async_std doesn't have `can_vector`.
257+
258+
// async_std doesn't have `write_all_vectored`.
259+
}
260+
207261
impl Seek for File {
208262
#[inline]
209263
fn poll_seek(
@@ -219,6 +273,17 @@ impl Seek for File {
219273
// async_std doesn't have `stream_position`.
220274
}
221275

276+
impl Seek for &File {
277+
#[inline]
278+
fn poll_seek(self: Pin<&mut Self>, cx: &mut Context, pos: SeekFrom) -> Poll<io::Result<u64>> {
279+
Seek::poll_seek(Pin::new(&mut &self.cap_std), cx, pos)
280+
}
281+
282+
// async_std doesn't have `stream_len`.
283+
284+
// async_std doesn't have `stream_position`.
285+
}
286+
222287
// TODO: Can async_std implement `From<File>` for `process::Stdio`?
223288

224289
// async_std doesn't have `FileExt`.

cap-async-std/src/net/tcp_stream.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,30 @@ impl Read for TcpStream {
190190
// async_std doesn't have `initializer`.
191191
}
192192

193+
impl Read for &TcpStream {
194+
#[inline]
195+
fn poll_read(
196+
self: Pin<&mut Self>,
197+
cx: &mut Context,
198+
buf: &mut [u8],
199+
) -> Poll<io::Result<usize>> {
200+
Read::poll_read(Pin::new(&mut &self.std), cx, buf)
201+
}
202+
203+
#[inline]
204+
fn poll_read_vectored(
205+
self: Pin<&mut Self>,
206+
cx: &mut Context,
207+
bufs: &mut [IoSliceMut],
208+
) -> Poll<io::Result<usize>> {
209+
Read::poll_read_vectored(Pin::new(&mut &self.std), cx, bufs)
210+
}
211+
212+
// async_std doesn't have `is_read_vectored`.
213+
214+
// async_std doesn't have `initializer`.
215+
}
216+
193217
impl Write for TcpStream {
194218
#[inline]
195219
fn poll_write(
@@ -224,4 +248,34 @@ impl Write for TcpStream {
224248
// async_std doesn't have `write_all_vectored`.
225249
}
226250

251+
impl Write for &TcpStream {
252+
#[inline]
253+
fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<io::Result<usize>> {
254+
Write::poll_write(Pin::new(&mut &self.std), cx, buf)
255+
}
256+
257+
#[inline]
258+
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
259+
Write::poll_flush(Pin::new(&mut &self.std), cx)
260+
}
261+
262+
#[inline]
263+
fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
264+
Write::poll_close(Pin::new(&mut &self.std), cx)
265+
}
266+
267+
#[inline]
268+
fn poll_write_vectored(
269+
self: Pin<&mut Self>,
270+
cx: &mut Context,
271+
bufs: &[IoSlice],
272+
) -> Poll<io::Result<usize>> {
273+
Write::poll_write_vectored(Pin::new(&mut &self.std), cx, bufs)
274+
}
275+
276+
// async_std doesn't have `is_write_vectored`.
277+
278+
// async_std doesn't have `write_all_vectored`.
279+
}
280+
227281
// TODO: impl Debug for TcpStream

cap-async-std/src/os/unix/net/unix_stream.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,30 @@ impl Read for UnixStream {
133133
// async_std doesn't have `initializer`.
134134
}
135135

136+
impl Read for &UnixStream {
137+
#[inline]
138+
fn poll_read(
139+
self: Pin<&mut Self>,
140+
cx: &mut Context,
141+
buf: &mut [u8],
142+
) -> Poll<io::Result<usize>> {
143+
Read::poll_read(Pin::new(&mut &self.std), cx, buf)
144+
}
145+
146+
#[inline]
147+
fn poll_read_vectored(
148+
self: Pin<&mut Self>,
149+
cx: &mut Context,
150+
bufs: &mut [IoSliceMut],
151+
) -> Poll<io::Result<usize>> {
152+
Read::poll_read_vectored(Pin::new(&mut &self.std), cx, bufs)
153+
}
154+
155+
// async_std doesn't have `is_read_vectored`.
156+
157+
// async_std doesn't have `initializer`.
158+
}
159+
136160
impl Write for UnixStream {
137161
#[inline]
138162
fn poll_write(
@@ -167,4 +191,34 @@ impl Write for UnixStream {
167191
// async_std doesn't have `write_all_vectored`.
168192
}
169193

194+
impl Write for &UnixStream {
195+
#[inline]
196+
fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<io::Result<usize>> {
197+
Write::poll_write(Pin::new(&mut &self.std), cx, buf)
198+
}
199+
200+
#[inline]
201+
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
202+
Write::poll_flush(Pin::new(&mut &self.std), cx)
203+
}
204+
205+
#[inline]
206+
fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
207+
Write::poll_close(Pin::new(&mut &self.std), cx)
208+
}
209+
210+
#[inline]
211+
fn poll_write_vectored(
212+
self: Pin<&mut Self>,
213+
cx: &mut Context,
214+
bufs: &[IoSlice],
215+
) -> Poll<io::Result<usize>> {
216+
Write::poll_write_vectored(Pin::new(&mut &self.std), cx, bufs)
217+
}
218+
219+
// async_std doesn't have `is_write_vectored`.
220+
221+
// async_std doesn't have `write_all_vectored`.
222+
}
223+
170224
// TODO: impl Debug for UnixStream

0 commit comments

Comments
 (0)