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
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use std::io;

use futures::{Async, Poll, Stream};

use buf::ByteBuf;
use buf::codec::u32::little_endian as u32le;

const U32_SIZE: usize = 4;

enum State {
    More(usize),
    Pending,
    Done,
}

pub struct U32lePrefix<S> {
    stream: S,
    state: State,
    data: ByteBuf,
}

impl<S> U32lePrefix<S> {
    #[inline]
    fn new(stream: S) -> Self {
        U32lePrefix {
            stream,
            state: State::Pending,
            data: ByteBuf::new(),
        }
    }

    #[inline]
    pub fn get_ref(&self) -> &S {
        &self.stream
    }

    #[inline]
    pub fn get_mut(&mut self) -> &mut S {
        &mut self.stream
    }

    #[inline]
    pub fn into_inner(self) -> S {
        self.stream
    }
}

impl<S> U32lePrefix<S>
where
    S: Stream<Item = ByteBuf, Error = io::Error>,
{
    #[inline]
    fn poll_more(&mut self, n: usize) -> Poll<Option<ByteBuf>, io::Error> {
        match try_ready!(self.stream.poll()) {
            Some(pulled) => {
                self.data.extend(pulled);
                match self.data.drain_to(n) {
                    Ok(t) => {
                        self.state = State::Pending;
                        Ok(Async::Ready(Some(t)))
                    }
                    Err(..) => Ok(Async::NotReady),
                }
            }
            None => {
                self.state = State::Done;
                Ok(Async::Ready(None))
            }
        }
    }

    #[inline]
    fn poll_pending(&mut self) -> Poll<Option<ByteBuf>, io::Error> {
        match try_ready!(self.stream.poll()) {
            Some(pulled) => {
                self.data = pulled;
                let len = self.data.len();
                if len < U32_SIZE {
                    return Ok(Async::NotReady);
                }
                match self.data.read(u32le::read) {
                    Ok(n) => {
                        match len >= n as usize {
                            true => {
                                match self.data.drain_to(n as usize) {
                                    Ok(t) => Ok(Async::Ready(Some(t))),
                                    _ => ::unreachable(),
                                }
                            }
                            false => {
                                self.state = State::More(n as usize);
                                Ok(Async::NotReady)
                            }
                        }
                    }
                    _ => ::unreachable(),
                }
            }
            None => {
                self.state = State::Done;
                Ok(Async::Ready(None))
            }
        }
    }
}

impl<S> Stream for U32lePrefix<S>
where
    S: Stream<Item = ByteBuf, Error = io::Error>,
{
    type Item = ByteBuf;
    type Error = io::Error;

    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
        match self.state {
            State::Pending => {
                let len = self.data.len();
                if len < U32_SIZE {
                    return self.poll_pending();
                }
                match self.data.read(u32le::read) {
                    Ok(n) => {
                        match len >= n as usize {
                            true => {
                                match self.data.drain_to(n as usize) {
                                    Ok(t) => Ok(Async::Ready(Some(t))),
                                    _ => ::unreachable(),
                                }
                            }
                            false => {
                                self.state = State::More(n as usize);
                                self.poll_more(n as usize)
                            }
                        }
                    }
                    _ => ::unreachable(),
                }
            }
            State::More(n) => self.poll_more(n),
            State::Done => Ok(Async::Ready(None)),
        }
    }
}

impl<S> From<S> for U32lePrefix<S>
where
    S: Stream<Item = ByteBuf, Error = io::Error>,
{
    #[inline]
    fn from(stream: S) -> Self {
        Self::new(stream)
    }
}