use std::fmt::{Debug, Formatter};
use std::time::Duration;
use crate::promise::streaming_promise_map::MappedStreamingPromise;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum StreamingPromiseState {
Streaming,
Finished,
Broken,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct UpdateResult {
pub state: StreamingPromiseState,
pub has_changed: bool,
}
pub trait StreamingPromise<T> {
fn state(&self) -> StreamingPromiseState;
fn drain(&mut self, how_long: Option<Duration>) -> StreamingPromiseState;
fn update(&mut self) -> UpdateResult;
fn read(&self) -> &Vec<T>;
fn map<B, F: Fn(&T) -> B>(self, mapper: F) -> MappedStreamingPromise<T, Self, B, F>
where
Self: Sized,
B: Clone,
{
MappedStreamingPromise::new(self, mapper)
}
fn boxed(self) -> Box<dyn StreamingPromise<T>>
where
Self: Sized + 'static,
{
Box::new(self)
}
}
impl<A> Debug for dyn StreamingPromise<A> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
return write!(f, "[{:?} streaming promise of \"{}\"]", self.state(), std::any::type_name::<A>());
}
}