Add a sample implementation of a double ended stream

This commit is contained in:
Felipe Sere 2019-11-16 12:43:05 +00:00
parent cc493df433
commit aabfefd015

View file

@ -22,3 +22,31 @@ pub trait DoubleEndedStream: Stream {
/// [trait-level]: trait.DoubleEndedStream.html
fn poll_next_back(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>;
}
pub struct Sample<T> {
inner: Vec<T>,
}
impl<T> From<Vec<T>> for Sample<T> {
fn from(data: Vec<T>) -> Self {
Sample { inner: data }
}
}
impl<T> Unpin for Sample<T> {}
impl<T> Stream for Sample<T> {
type Item = T;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
if self.inner.len() > 0 {
return Poll::Ready(Some(self.inner.remove(0)));
}
return Poll::Ready(None);
}
}
impl<T> DoubleEndedStream for Sample<T> {
fn poll_next_back(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Poll::Ready(self.inner.pop())
}
}