@@ -18,16 +18,85 @@ extension_trait! {
1818
1919
2020 #[ doc = r#"
21- Something fancy
21+ A stream able to yield elements from both ends.
22+
23+ Something that implements `DoubleEndedStream` has one extra capability
24+ over something that implements [`Stream`]: the ability to also take
25+ `Item`s from the back, as well as the front.
26+
27+ It is important to note that both back and forth work on the same range,
28+ and do not cross: iteration is over when they meet in the middle.
29+
30+ In a similar fashion to the [`Stream`] protocol, once a
31+ `DoubleEndedStream` returns `None` from a `next_back()`, calling it again
32+ may or may not ever return `Some` again. `next()` and `next_back()` are
33+ interchangeable for this purpose.
34+ ```
2235 "# ]
2336 pub trait DoubleEndedStream {
37+ #[ doc = r#"
38+ The type of items yielded by this stream.
39+ "# ]
2440 type Item ;
2541
42+ #[ doc = r#"
43+ Attempts to receive the next item from the back of the stream.
44+
45+ There are several possible return values:
46+
47+ * `Poll::Pending` means this stream's next_back value is not ready yet.
48+ * `Poll::Ready(None)` means this stream has been exhausted.
49+ * `Poll::Ready(Some(item))` means `item` was received out of the stream.
50+
51+ # Examples
52+
53+ ```
54+ # fn main() { async_std::task::block_on(async {
55+ #
56+ use std::pin::Pin;
57+
58+ use async_std::prelude::*;
59+ use async_std::stream;
60+ use async_std::task::{Context, Poll};
61+
62+ fn increment(
63+ s: impl DoubleEndedStream<Item = i32> + Unpin,
64+ ) -> impl DoubleEndedStream<Item = i32> + Unpin {
65+ struct Increment<S>(S);
66+
67+ impl<S: DoubleEndedStream<Item = i32> + Unpin> Stream for Increment<S> {
68+ type Item = S::Item;
69+
70+ fn poll_next_back(
71+ mut self: Pin<&mut Self>,
72+ cx: &mut Context<'_>,
73+ ) -> Poll<Option<Self::Item>> {
74+ match Pin::new(&mut self.0).poll_next_back(cx) {
75+ Poll::Pending => Poll::Pending,
76+ Poll::Ready(None) => Poll::Ready(None),
77+ Poll::Ready(Some(item)) => Poll::Ready(Some(item + 1)),
78+ }
79+ }
80+ }
81+
82+ Increment(s)
83+ }
84+
85+ let mut s = increment(stream::once(7)); // will need to implement DoubleEndedStream
86+
87+ assert_eq!(s.next_back().await, Some(8));
88+ assert_eq!(s.next_back().await, None);
89+ #
90+ # }) }
91+ ```
92+ "# ]
2693 fn poll_next_back( self : Pin <& mut Self >, cx: & mut Context <' _>) -> Poll <Option <Self :: Item >>;
2794 }
2895
2996 #[ doc = r#"
30- Something else
97+ Extension methods for [`DoubleEndedStreamExt`].
98+
99+ [`Stream`]: ../stream/trait.Stream.html
31100 "# ]
32101 pub trait DoubleEndedStreamExt : crate :: stream:: DoubleEndedStream {
33102 #[ doc = r#"
0 commit comments