|
| 1 | +use std::{ |
| 2 | + pin::Pin, |
| 3 | + task::{Context, Poll}, |
| 4 | +}; |
| 5 | + |
| 6 | +use futures::{ready, stream::FusedStream, Stream}; |
| 7 | +use pin_project_lite::pin_project; |
| 8 | + |
| 9 | +use crate::events::{Event, EventStream}; |
| 10 | + |
| 11 | +macro_rules! delegate_access_inner { |
| 12 | + ($field:ident, $inner:ty, ($($ind:tt)*)) => { |
| 13 | + /// Acquires a reference to the underlying sink or stream that this combinator is |
| 14 | + /// pulling from. |
| 15 | + pub fn get_ref(&self) -> &$inner { |
| 16 | + (&self.$field) $($ind get_ref())* |
| 17 | + } |
| 18 | + |
| 19 | + /// Acquires a mutable reference to the underlying sink or stream that this |
| 20 | + /// combinator is pulling from. |
| 21 | + /// |
| 22 | + /// Note that care must be taken to avoid tampering with the state of the |
| 23 | + /// sink or stream which may otherwise confuse this combinator. |
| 24 | + pub fn get_mut(&mut self) -> &mut $inner { |
| 25 | + (&mut self.$field) $($ind get_mut())* |
| 26 | + } |
| 27 | + |
| 28 | + /// Acquires a pinned mutable reference to the underlying sink or stream that this |
| 29 | + /// combinator is pulling from. |
| 30 | + /// |
| 31 | + /// Note that care must be taken to avoid tampering with the state of the |
| 32 | + /// sink or stream which may otherwise confuse this combinator. |
| 33 | + pub fn get_pin_mut(self: core::pin::Pin<&mut Self>) -> core::pin::Pin<&mut $inner> { |
| 34 | + self.project().$field $($ind get_pin_mut())* |
| 35 | + } |
| 36 | + |
| 37 | + /// Consumes this combinator, returning the underlying sink or stream. |
| 38 | + /// |
| 39 | + /// Note that this may discard intermediate state of this combinator, so |
| 40 | + /// care should be taken to avoid losing resources when this is called. |
| 41 | + pub fn into_inner(self) -> $inner { |
| 42 | + self.$field $($ind into_inner())* |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl<T: ?Sized> RxStreamExt for T where T: Stream {} |
| 48 | +pub trait RxStreamExt: Stream { |
| 49 | + fn into_ref_stream(self) -> IntoRefStream<Self> |
| 50 | + where |
| 51 | + Self: Sized, |
| 52 | + { |
| 53 | + assert_stream::<Event<Self::Item>, _>(IntoRefStream::new(self)) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +pin_project! { |
| 58 | + /// Stream for the [`into_ref_stream`](RxStreamExt::into_ref_stream) method. |
| 59 | + #[must_use = "streams do nothing unless polled"] |
| 60 | + pub struct IntoRefStream<S: Stream> { |
| 61 | + #[pin] |
| 62 | + stream: EventStream<S::Item, S>, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl<S: Stream> IntoRefStream<S> { |
| 67 | + pub(crate) fn new(stream: S) -> Self { |
| 68 | + Self { |
| 69 | + stream: EventStream::new(stream), |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + delegate_access_inner!(stream, EventStream<S::Item, S>, ()); |
| 74 | +} |
| 75 | + |
| 76 | +impl<S> FusedStream for IntoRefStream<S> |
| 77 | +where |
| 78 | + S: FusedStream, |
| 79 | +{ |
| 80 | + fn is_terminated(&self) -> bool { |
| 81 | + self.stream.is_done() |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl<S> Stream for IntoRefStream<S> |
| 86 | +where |
| 87 | + S: Stream, |
| 88 | +{ |
| 89 | + type Item = Event<S::Item>; |
| 90 | + |
| 91 | + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { |
| 92 | + let mut this = self.project(); |
| 93 | + let res = ready!(this.stream.as_mut().poll_next(cx)); |
| 94 | + Poll::Ready(res) |
| 95 | + } |
| 96 | + |
| 97 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 98 | + self.stream.size_hint() |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +pub(crate) fn assert_stream<T, S>(stream: S) -> S |
| 103 | +where |
| 104 | + S: Stream<Item = T>, |
| 105 | +{ |
| 106 | + stream |
| 107 | +} |
0 commit comments