sheave_core/handlers/
vec_stream.rs

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
use std::{
    io::Result as IOResult,
    pin::{
        Pin,
        pin
    },
    task::{
        Context,
        Poll
    }
};
use futures::ready;
use tokio::io::{
    AsyncRead,
    AsyncWrite,
    ReadBuf
};

/// The simple stream for std's buffer-like types.
///
/// `[u8]` can't write, against `Vec` can't read.
/// std's buffer-like types can't act as streams which coincide reading and writing.
/// Therefore this type was prepared for wrapping them.
/// Mainly, this is expected that is used for testing handlers.
#[derive(Debug, Default)]
pub struct VecStream(Vec<u8>);

impl AsyncRead for VecStream {
    fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>) -> Poll<IOResult<()>> {
        ready!(pin!(self.0.as_slice()).poll_read(cx, buf))?;
        self.0 = self.0.split_off(buf.filled().len());
        Poll::Ready(Ok(()))
    }
}

impl AsyncWrite for VecStream {
    fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<IOResult<usize>> {
        Pin::new(&mut self.0).poll_write(cx, buf)
    }

    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<IOResult<()>> {
        Pin::new(&mut self.0).poll_flush(cx)
    }

    fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<IOResult<()>> {
        Pin::new(&mut self.0).poll_shutdown(cx)
    }
}