kalosm_streams/
sender.rs

1use futures_channel::mpsc::UnboundedReceiver;
2use futures_util::{Stream, StreamExt};
3
4/// A stream of text from a tokio channel.
5pub struct ChannelTextStream<S: AsRef<str> = String> {
6    receiver: UnboundedReceiver<S>,
7}
8
9impl<S: AsRef<str>> std::fmt::Debug for ChannelTextStream<S> {
10    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
11        f.debug_struct("ChannelTextStream").finish()
12    }
13}
14
15impl<S: AsRef<str>> From<UnboundedReceiver<S>> for ChannelTextStream<S> {
16    fn from(receiver: UnboundedReceiver<S>) -> Self {
17        Self { receiver }
18    }
19}
20
21impl<S: AsRef<str>> Stream for ChannelTextStream<S> {
22    type Item = S;
23
24    fn poll_next(
25        mut self: std::pin::Pin<&mut Self>,
26        cx: &mut std::task::Context<'_>,
27    ) -> core::task::Poll<Option<Self::Item>> {
28        self.receiver.poll_next_unpin(cx)
29    }
30}