tokio_buf/
u8.rs

1use bytes::{Bytes, BytesMut};
2use futures::Poll;
3use never::Never;
4use std::io;
5use BufStream;
6
7impl BufStream for Vec<u8> {
8    type Item = io::Cursor<Vec<u8>>;
9    type Error = Never;
10
11    fn poll_buf(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
12        if self.is_empty() {
13            return Ok(None.into());
14        }
15
16        poll_bytes(self)
17    }
18}
19
20impl BufStream for &'static [u8] {
21    type Item = io::Cursor<&'static [u8]>;
22    type Error = Never;
23
24    fn poll_buf(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
25        if self.is_empty() {
26            return Ok(None.into());
27        }
28
29        poll_bytes(self)
30    }
31}
32
33impl BufStream for Bytes {
34    type Item = io::Cursor<Bytes>;
35    type Error = Never;
36
37    fn poll_buf(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
38        if self.is_empty() {
39            return Ok(None.into());
40        }
41
42        poll_bytes(self)
43    }
44}
45
46impl BufStream for BytesMut {
47    type Item = io::Cursor<BytesMut>;
48    type Error = Never;
49
50    fn poll_buf(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
51        if self.is_empty() {
52            return Ok(None.into());
53        }
54
55        poll_bytes(self)
56    }
57}
58
59fn poll_bytes<T: Default>(buf: &mut T) -> Poll<Option<io::Cursor<T>>, Never> {
60    use std::mem;
61
62    let bytes = mem::replace(buf, Default::default());
63    let buf = io::Cursor::new(bytes);
64
65    Ok(Some(buf).into())
66}