|
| 1 | +// check-pass |
| 2 | + |
| 3 | +use std::error::Error as StdError; |
| 4 | +use std::pin::Pin; |
| 5 | +use std::task::{Context, Poll}; |
| 6 | + |
| 7 | +pub trait Stream { |
| 8 | + type Item; |
| 9 | + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>; |
| 10 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 11 | + (0, None) |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +pub trait TryStream: Stream { |
| 16 | + type Ok; |
| 17 | + type Error; |
| 18 | + |
| 19 | + fn try_poll_next( |
| 20 | + self: Pin<&mut Self>, |
| 21 | + cx: &mut Context<'_>, |
| 22 | + ) -> Poll<Option<Result<Self::Ok, Self::Error>>>; |
| 23 | +} |
| 24 | + |
| 25 | +impl<S, T, E> TryStream for S |
| 26 | +where |
| 27 | + S: ?Sized + Stream<Item = Result<T, E>>, |
| 28 | +{ |
| 29 | + type Ok = T; |
| 30 | + type Error = E; |
| 31 | + |
| 32 | + fn try_poll_next( |
| 33 | + self: Pin<&mut Self>, |
| 34 | + cx: &mut Context<'_>, |
| 35 | + ) -> Poll<Option<Result<Self::Ok, Self::Error>>> { |
| 36 | + self.poll_next(cx) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +pub trait ServerSentEvent: Sized + Send + Sync + 'static {} |
| 41 | + |
| 42 | +impl<T: Send + Sync + 'static> ServerSentEvent for T {} |
| 43 | + |
| 44 | +struct SseKeepAlive<S> { |
| 45 | + event_stream: S, |
| 46 | +} |
| 47 | + |
| 48 | +struct SseComment<T>(T); |
| 49 | + |
| 50 | +impl<S> Stream for SseKeepAlive<S> |
| 51 | +where |
| 52 | + S: TryStream + Send + 'static, |
| 53 | + S::Ok: ServerSentEvent, |
| 54 | + S::Error: StdError + Send + Sync + 'static, |
| 55 | +{ |
| 56 | + type Item = Result<SseComment<&'static str>, ()>; |
| 57 | + fn poll_next(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Option<Self::Item>> { |
| 58 | + unimplemented!() |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +pub fn keep<S>( |
| 63 | + event_stream: S, |
| 64 | +) -> impl TryStream<Ok = impl ServerSentEvent + Send + 'static, Error = ()> + Send + 'static |
| 65 | +where |
| 66 | + S: TryStream + Send + 'static, |
| 67 | + S::Ok: ServerSentEvent + Send, |
| 68 | + S::Error: StdError + Send + Sync + 'static, |
| 69 | +{ |
| 70 | + SseKeepAlive { event_stream } |
| 71 | +} |
| 72 | + |
| 73 | +fn main() {} |
0 commit comments