use crate::output_stream::num_bytes::NumBytes;
use crate::output_stream::{Consumable, OutputStream};
use crate::{StreamEvent, Subscribable, Subscription};
use std::convert::Infallible;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DiscardedOutputStream {
name: &'static str,
}
impl DiscardedOutputStream {
pub(crate) fn new(name: &'static str) -> Self {
Self { name }
}
}
#[doc(hidden)]
pub struct ImmediateEof {
eof_emitted: bool,
}
impl ImmediateEof {
fn new() -> Self {
Self { eof_emitted: false }
}
}
impl Subscription for ImmediateEof {
fn next_event(&mut self) -> impl Future<Output = Option<StreamEvent>> + Send + '_ {
let eof_emitted = self.eof_emitted;
self.eof_emitted = true;
async move {
if eof_emitted {
None
} else {
Some(StreamEvent::Eof)
}
}
}
}
impl Subscribable for DiscardedOutputStream {
type Subscription = ImmediateEof;
type SubscribeError = Infallible;
fn try_subscribe(&self) -> Result<Self::Subscription, Self::SubscribeError> {
Ok(Self::Subscription::new())
}
}
impl Consumable for DiscardedOutputStream {
type Error = Infallible;
}
impl OutputStream for DiscardedOutputStream {
fn read_chunk_size(&self) -> NumBytes {
NumBytes::zero()
}
fn max_buffered_chunks(&self) -> usize {
0
}
fn name(&self) -> &'static str {
self.name
}
}