mirror of
https://github.com/async-rs/async-std.git
synced 2025-03-05 17:49:40 +00:00
Add a sample implementation of a double ended stream
This commit is contained in:
parent
cc493df433
commit
aabfefd015
1 changed files with 28 additions and 0 deletions
|
@ -22,3 +22,31 @@ pub trait DoubleEndedStream: Stream {
|
||||||
/// [trait-level]: trait.DoubleEndedStream.html
|
/// [trait-level]: trait.DoubleEndedStream.html
|
||||||
fn poll_next_back(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>;
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue