/rust/registry/src/index.crates.io-1949cf8c6b5b557f/hyper-util-0.1.20/src/rt/io.rs
Line | Count | Source |
1 | | use std::marker::Unpin; |
2 | | use std::pin::Pin; |
3 | | use std::task::{ready, Poll}; |
4 | | |
5 | | use hyper::rt::{Read, ReadBuf, Write}; |
6 | | |
7 | | use std::future::poll_fn; |
8 | | |
9 | 0 | pub(crate) async fn read<T>(io: &mut T, buf: &mut [u8]) -> Result<usize, std::io::Error> |
10 | 0 | where |
11 | 0 | T: Read + Unpin, |
12 | 0 | { |
13 | 0 | poll_fn(move |cx| { |
14 | 0 | let mut buf = ReadBuf::new(buf); |
15 | 0 | ready!(Pin::new(&mut *io).poll_read(cx, buf.unfilled()))?; |
16 | 0 | Poll::Ready(Ok(buf.filled().len())) |
17 | 0 | }) |
18 | 0 | .await |
19 | 0 | } |
20 | | |
21 | 0 | pub(crate) async fn write_all<T>(io: &mut T, buf: &[u8]) -> Result<(), std::io::Error> |
22 | 0 | where |
23 | 0 | T: Write + Unpin, |
24 | 0 | { |
25 | 0 | let mut n = 0; |
26 | 0 | poll_fn(move |cx| { |
27 | 0 | while n < buf.len() { |
28 | 0 | n += ready!(Pin::new(&mut *io).poll_write(cx, &buf[n..])?); |
29 | | } |
30 | 0 | Poll::Ready(Ok(())) |
31 | 0 | }) |
32 | 0 | .await |
33 | 0 | } |