1use never::Never;
2use BufStream;
3use SizeHint;
4
5use futures::Poll;
6
7use std::io;
8use std::mem;
9
10impl BufStream for String {
11 type Item = io::Cursor<Vec<u8>>;
12 type Error = Never;
13
14 fn poll_buf(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
15 if self.is_empty() {
16 return Ok(None.into());
17 }
18
19 let bytes = mem::replace(self, Default::default()).into_bytes();
20 let buf = io::Cursor::new(bytes);
21
22 Ok(Some(buf).into())
23 }
24
25 fn size_hint(&self) -> SizeHint {
26 size_hint(&self[..])
27 }
28}
29
30impl BufStream for &'static str {
31 type Item = io::Cursor<&'static [u8]>;
32 type Error = Never;
33
34 fn poll_buf(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
35 if self.is_empty() {
36 return Ok(None.into());
37 }
38
39 let bytes = mem::replace(self, Default::default()).as_bytes();
40 let buf = io::Cursor::new(bytes);
41
42 Ok(Some(buf).into())
43 }
44
45 fn size_hint(&self) -> SizeHint {
46 size_hint(&self[..])
47 }
48}
49
50fn size_hint(s: &str) -> SizeHint {
51 let mut hint = SizeHint::new();
52 hint.set_lower(s.len() as u64);
53 hint.set_upper(s.len() as u64);
54 hint
55}