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
use never::Never;
use BufStream;
use SizeHint;

use futures::Poll;

use std::io;
use std::mem;

impl BufStream for String {
    type Item = io::Cursor<Vec<u8>>;
    type Error = Never;

    fn poll_buf(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
        if self.is_empty() {
            return Ok(None.into());
        }

        let bytes = mem::replace(self, Default::default()).into_bytes();
        let buf = io::Cursor::new(bytes);

        Ok(Some(buf).into())
    }

    fn size_hint(&self) -> SizeHint {
        size_hint(&self[..])
    }
}

impl BufStream for &'static str {
    type Item = io::Cursor<&'static [u8]>;
    type Error = Never;

    fn poll_buf(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
        if self.is_empty() {
            return Ok(None.into());
        }

        let bytes = mem::replace(self, Default::default()).as_bytes();
        let buf = io::Cursor::new(bytes);

        Ok(Some(buf).into())
    }

    fn size_hint(&self) -> SizeHint {
        size_hint(&self[..])
    }
}

fn size_hint(s: &str) -> SizeHint {
    let mut hint = SizeHint::new();
    hint.set_lower(s.len() as u64);
    hint.set_upper(s.len() as u64);
    hint
}