use std::pin::Pin;
use std::task::{Context, Poll};
use futures_core::Stream;
use tokio_util::sync::ReusableBoxFuture;
use super::Consumer;
use super::record::ConsumerRecord;
use crate::error::Result;
pub struct ConsumerStream<'a> {
consumer: &'a Consumer,
fut: ReusableBoxFuture<'a, Result<Option<ConsumerRecord>>>,
done: bool,
}
impl<'a> ConsumerStream<'a> {
pub(super) fn new(consumer: &'a Consumer) -> Self {
Self {
fut: ReusableBoxFuture::new(consumer.recv()),
consumer,
done: false,
}
}
}
impl Stream for ConsumerStream<'_> {
type Item = Result<ConsumerRecord>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.get_mut();
if this.done {
return Poll::Ready(None);
}
match this.fut.poll(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(result) => match result {
Ok(Some(record)) => {
this.fut.set(this.consumer.recv());
Poll::Ready(Some(Ok(record)))
}
Ok(None) => {
this.done = true;
Poll::Ready(None)
}
Err(e) => {
this.fut.set(this.consumer.recv());
Poll::Ready(Some(Err(e)))
}
},
}
}
}