use crate :: { Filter, Observe, ObserveLocal };
pub trait Observable<Event>
where Event: Clone + 'static + Send ,
{
type Error: std::error::Error;
fn observe( &mut self, options: ObserveConfig<Event> ) -> Observe<'_, Event, Self::Error >;
}
pub trait ObservableLocal<Event>
where Event: Clone + 'static + Send ,
{
type Error: std::error::Error;
fn observe_local( &mut self, options: ObserveConfig<Event> ) -> ObserveLocal<'_, Event, Self::Error >;
}
#[ derive( Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord )]
pub enum Channel
{
Bounded(usize),
Unbounded,
__NonExhaustive__
}
impl Default for Channel
{
fn default() -> Self
{
Channel::Unbounded
}
}
#[ derive( Debug ) ]
pub struct ObserveConfig<Event> where Event: Clone + 'static + Send
{
pub(crate) channel: Channel,
pub(crate) filter : Option<Filter<Event>>,
}
impl<Event> Default for ObserveConfig<Event> where Event: Clone + 'static + Send
{
fn default() -> Self
{
Self
{
channel: Channel::default(),
filter : None ,
}
}
}
impl<Event> ObserveConfig<Event> where Event: Clone + 'static + Send
{
pub fn channel( mut self, channel: Channel ) -> Self
{
self.channel = channel;
self
}
pub fn filter( mut self, filter: fn(&Event) -> bool ) -> Self
{
debug_assert!( self.filter.is_none(), "You can only set one filter on ObserveConfig" );
self.filter = Some( Filter::Pointer(filter) );
self
}
pub fn filter_boxed( mut self, filter: impl FnMut(&Event) -> bool + Send + 'static ) -> Self
{
debug_assert!( self.filter.is_none(), "You can only set one filter on ObserveConfig" );
self.filter = Some( Filter::Closure( Box::new(filter) ) );
self
}
}
impl<Event> From<Channel> for ObserveConfig<Event> where Event: Clone + 'static + Send
{
fn from( channel: Channel ) -> Self
{
Self::default().channel( channel )
}
}
impl<Event> From<Filter<Event>> for ObserveConfig<Event> where Event: Clone + 'static + Send
{
fn from( filter: Filter<Event> ) -> Self
{
Self
{
filter: Some(filter),
..Self::default()
}
}
}