factor out into common crate
This commit is contained in:
118
items_0/src/collect_c.rs
Normal file
118
items_0/src/collect_c.rs
Normal file
@@ -0,0 +1,118 @@
|
||||
use crate::collect_s::ToJsonBytes;
|
||||
use crate::collect_s::ToJsonResult;
|
||||
use crate::AsAnyRef;
|
||||
use crate::Events;
|
||||
use err::Error;
|
||||
use std::any::Any;
|
||||
use std::fmt;
|
||||
|
||||
pub trait Collector: fmt::Debug + Send {
|
||||
// TODO should require here Collectable?
|
||||
type Input;
|
||||
type Output: Collected;
|
||||
|
||||
fn len(&self) -> usize;
|
||||
|
||||
fn ingest(&mut self, item: &mut Self::Input);
|
||||
|
||||
fn set_range_complete(&mut self);
|
||||
|
||||
fn set_timed_out(&mut self);
|
||||
|
||||
fn result(&mut self) -> Result<Self::Output, Error>;
|
||||
}
|
||||
|
||||
pub trait Collectable: fmt::Debug {
|
||||
type Collector: Collector<Input = Self>;
|
||||
|
||||
fn new_collector(&self) -> Self::Collector;
|
||||
}
|
||||
|
||||
pub trait Collected: fmt::Debug + ToJsonResult + AsAnyRef + Send {}
|
||||
|
||||
erased_serde::serialize_trait_object!(Collected);
|
||||
|
||||
impl AsAnyRef for Box<dyn Collected> {
|
||||
fn as_any_ref(&self) -> &dyn Any {
|
||||
self.as_ref().as_any_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToJsonResult for Box<dyn Collected> {
|
||||
fn to_json_result(&self) -> Result<Box<dyn ToJsonBytes>, Error> {
|
||||
self.as_ref().to_json_result()
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Collected for Box<dyn Collected> {}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CollectorDynDefault {}
|
||||
|
||||
pub trait CollectorDyn: fmt::Debug + Send {
|
||||
fn len(&self) -> usize;
|
||||
|
||||
fn ingest(&mut self, item: &mut dyn CollectableWithDefault);
|
||||
|
||||
fn set_range_complete(&mut self);
|
||||
|
||||
fn set_timed_out(&mut self);
|
||||
|
||||
fn result(&mut self) -> Result<Box<dyn Collected>, Error>;
|
||||
}
|
||||
|
||||
pub trait CollectableWithDefault {
|
||||
fn new_collector(&self) -> Box<dyn CollectorDyn>;
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct EventsCollector {
|
||||
coll: Box<dyn CollectorDyn>,
|
||||
}
|
||||
|
||||
impl EventsCollector {
|
||||
pub fn new(coll: Box<dyn CollectorDyn>) -> Self {
|
||||
Self { coll }
|
||||
}
|
||||
}
|
||||
|
||||
impl Collector for EventsCollector {
|
||||
type Input = Box<dyn Events>;
|
||||
|
||||
// TODO this Output trait does not differentiate between e.g. collected events, collected bins, different aggs, etc...
|
||||
type Output = Box<dyn Collected>;
|
||||
|
||||
fn len(&self) -> usize {
|
||||
self.coll.len()
|
||||
}
|
||||
|
||||
fn ingest(&mut self, item: &mut Self::Input) {
|
||||
self.coll.ingest(item.as_collectable_with_default_mut());
|
||||
}
|
||||
|
||||
fn set_range_complete(&mut self) {
|
||||
self.coll.set_range_complete()
|
||||
}
|
||||
|
||||
fn set_timed_out(&mut self) {
|
||||
self.coll.set_timed_out()
|
||||
}
|
||||
|
||||
fn result(&mut self) -> Result<Self::Output, Error> {
|
||||
self.coll.result()
|
||||
}
|
||||
}
|
||||
|
||||
impl Collectable for Box<dyn Events> {
|
||||
type Collector = EventsCollector;
|
||||
|
||||
fn new_collector(&self) -> Self::Collector {
|
||||
let coll = CollectableWithDefault::new_collector(self.as_ref());
|
||||
EventsCollector::new(coll)
|
||||
}
|
||||
}
|
||||
106
items_0/src/collect_s.rs
Normal file
106
items_0/src/collect_s.rs
Normal file
@@ -0,0 +1,106 @@
|
||||
use super::collect_c::Collected;
|
||||
use crate::WithLen;
|
||||
use err::Error;
|
||||
use serde::Serialize;
|
||||
use std::any::Any;
|
||||
use std::fmt;
|
||||
|
||||
pub trait CollectorType: Send + Unpin + WithLen {
|
||||
type Input: Collectable;
|
||||
type Output: Collected + ToJsonResult + Serialize;
|
||||
|
||||
fn ingest(&mut self, src: &mut Self::Input);
|
||||
fn set_range_complete(&mut self);
|
||||
fn set_timed_out(&mut self);
|
||||
|
||||
// TODO use this crate's Error instead:
|
||||
fn result(&mut self) -> Result<Self::Output, Error>;
|
||||
}
|
||||
|
||||
pub trait Collector: Send + Unpin + WithLen {
|
||||
fn ingest(&mut self, src: &mut dyn Collectable);
|
||||
fn set_range_complete(&mut self);
|
||||
fn set_timed_out(&mut self);
|
||||
fn result(&mut self) -> Result<Box<dyn ToJsonResult>, Error>;
|
||||
}
|
||||
|
||||
pub trait CollectableType {
|
||||
type Collector: CollectorType<Input = Self>;
|
||||
fn new_collector() -> Self::Collector;
|
||||
}
|
||||
|
||||
pub trait Collectable: Any {
|
||||
fn new_collector(&self) -> Box<dyn Collector>;
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any;
|
||||
}
|
||||
|
||||
impl<T: CollectorType + 'static> Collector for T {
|
||||
fn ingest(&mut self, src: &mut dyn Collectable) {
|
||||
let src: &mut <T as CollectorType>::Input = src.as_any_mut().downcast_mut().expect("can not downcast");
|
||||
T::ingest(self, src)
|
||||
}
|
||||
|
||||
fn set_range_complete(&mut self) {
|
||||
T::set_range_complete(self)
|
||||
}
|
||||
|
||||
fn set_timed_out(&mut self) {
|
||||
T::set_timed_out(self)
|
||||
}
|
||||
|
||||
fn result(&mut self) -> Result<Box<dyn ToJsonResult>, Error> {
|
||||
let ret = T::result(self)?;
|
||||
Ok(Box::new(ret) as _)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: CollectableType + 'static> Collectable for T {
|
||||
fn new_collector(&self) -> Box<dyn Collector> {
|
||||
Box::new(T::new_collector()) as _
|
||||
}
|
||||
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
// TODO interesting: why exactly does returning `&mut self` not work here?
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
// TODO check usage of this trait
|
||||
pub trait ToJsonBytes {
|
||||
fn to_json_bytes(&self) -> Result<Vec<u8>, Error>;
|
||||
}
|
||||
|
||||
// TODO check usage of this trait
|
||||
pub trait ToJsonResult: erased_serde::Serialize + fmt::Debug + Send {
|
||||
fn to_json_result(&self) -> Result<Box<dyn ToJsonBytes>, Error>;
|
||||
fn as_any(&self) -> &dyn Any;
|
||||
}
|
||||
|
||||
erased_serde::serialize_trait_object!(ToJsonResult);
|
||||
|
||||
impl ToJsonResult for serde_json::Value {
|
||||
fn to_json_result(&self) -> Result<Box<dyn ToJsonBytes>, Error> {
|
||||
Ok(Box::new(self.clone()))
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl ToJsonBytes for serde_json::Value {
|
||||
fn to_json_bytes(&self) -> Result<Vec<u8>, Error> {
|
||||
Ok(serde_json::to_vec(self)?)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO do this with some blanket impl:
|
||||
impl Collectable for Box<dyn Collectable> {
|
||||
fn new_collector(&self) -> Box<dyn Collector> {
|
||||
Collectable::new_collector(self.as_ref())
|
||||
}
|
||||
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
Collectable::as_any_mut(self.as_mut())
|
||||
}
|
||||
}
|
||||
139
items_0/src/items_0.rs
Normal file
139
items_0/src/items_0.rs
Normal file
@@ -0,0 +1,139 @@
|
||||
pub mod collect_c;
|
||||
pub mod collect_s;
|
||||
|
||||
use collect_c::CollectableWithDefault;
|
||||
use collect_s::Collectable;
|
||||
use collect_s::ToJsonResult;
|
||||
use netpod::{NanoRange, ScalarType, Shape};
|
||||
use std::any::Any;
|
||||
use std::fmt;
|
||||
|
||||
pub trait WithLen {
|
||||
fn len(&self) -> usize;
|
||||
}
|
||||
|
||||
// TODO can probably be removed.
|
||||
pub trait TimeBins {
|
||||
fn ts_min(&self) -> Option<u64>;
|
||||
fn ts_max(&self) -> Option<u64>;
|
||||
fn ts_min_max(&self) -> Option<(u64, u64)>;
|
||||
}
|
||||
|
||||
pub enum Fits {
|
||||
Empty,
|
||||
Lower,
|
||||
Greater,
|
||||
Inside,
|
||||
PartlyLower,
|
||||
PartlyGreater,
|
||||
PartlyLowerAndGreater,
|
||||
}
|
||||
|
||||
pub trait RangeOverlapInfo {
|
||||
fn ends_before(&self, range: NanoRange) -> bool;
|
||||
fn ends_after(&self, range: NanoRange) -> bool;
|
||||
fn starts_after(&self, range: NanoRange) -> bool;
|
||||
}
|
||||
|
||||
pub trait EmptyForScalarTypeShape {
|
||||
fn empty(scalar_type: ScalarType, shape: Shape) -> Self;
|
||||
}
|
||||
|
||||
pub trait EmptyForShape {
|
||||
fn empty(shape: Shape) -> Self;
|
||||
}
|
||||
|
||||
pub trait Empty {
|
||||
fn empty() -> Self;
|
||||
}
|
||||
|
||||
pub trait AppendEmptyBin {
|
||||
fn append_empty_bin(&mut self, ts1: u64, ts2: u64);
|
||||
}
|
||||
|
||||
pub trait AsAnyRef {
|
||||
fn as_any_ref(&self) -> &dyn Any;
|
||||
}
|
||||
|
||||
pub trait AsAnyMut {
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any;
|
||||
}
|
||||
|
||||
/*impl AsAnyRef for Box<dyn AsAnyRef> {
|
||||
fn as_any_ref(&self) -> &dyn Any {
|
||||
self.as_ref().as_any_ref()
|
||||
}
|
||||
}*/
|
||||
|
||||
/// Data in time-binned form.
|
||||
pub trait TimeBinned: Any + TimeBinnable {
|
||||
fn as_time_binnable_dyn(&self) -> &dyn TimeBinnable;
|
||||
fn as_collectable_mut(&mut self) -> &mut dyn Collectable;
|
||||
fn edges_slice(&self) -> (&[u64], &[u64]);
|
||||
fn counts(&self) -> &[u64];
|
||||
fn mins(&self) -> Vec<f32>;
|
||||
fn maxs(&self) -> Vec<f32>;
|
||||
fn avgs(&self) -> Vec<f32>;
|
||||
fn validate(&self) -> Result<(), String>;
|
||||
}
|
||||
|
||||
pub trait TimeBinner: Send {
|
||||
fn ingest(&mut self, item: &dyn TimeBinnable);
|
||||
fn bins_ready_count(&self) -> usize;
|
||||
fn bins_ready(&mut self) -> Option<Box<dyn TimeBinned>>;
|
||||
|
||||
/// If there is a bin in progress with non-zero count, push it to the result set.
|
||||
/// With push_empty == true, a bin in progress is pushed even if it contains no counts.
|
||||
fn push_in_progress(&mut self, push_empty: bool);
|
||||
|
||||
/// Implies `Self::push_in_progress` but in addition, pushes a zero-count bin if the call
|
||||
/// to `push_in_progress` did not change the result count, as long as edges are left.
|
||||
/// The next call to `Self::bins_ready_count` must return one higher count than before.
|
||||
fn cycle(&mut self);
|
||||
|
||||
fn set_range_complete(&mut self);
|
||||
}
|
||||
|
||||
// TODO remove the Any bound. Factor out into custom AsAny trait.
|
||||
|
||||
/// Provides a time-binned representation of the implementing type.
|
||||
/// In contrast to `TimeBinnableType` this is meant for trait objects.
|
||||
pub trait TimeBinnable: fmt::Debug + WithLen + RangeOverlapInfo + Any + Send {
|
||||
// TODO implementors may fail if edges contain not at least 2 entries.
|
||||
fn time_binner_new(&self, edges: Vec<u64>, do_time_weight: bool) -> Box<dyn TimeBinner>;
|
||||
fn as_any(&self) -> &dyn Any;
|
||||
|
||||
// TODO just a helper for the empty result.
|
||||
fn to_box_to_json_result(&self) -> Box<dyn ToJsonResult>;
|
||||
}
|
||||
|
||||
// TODO can I remove the Any bound?
|
||||
|
||||
/// Container of some form of events, for use as trait object.
|
||||
pub trait Events:
|
||||
fmt::Debug + Any + Collectable + CollectableWithDefault + TimeBinnable + Send + erased_serde::Serialize
|
||||
{
|
||||
fn as_time_binnable(&self) -> &dyn TimeBinnable;
|
||||
fn verify(&self) -> bool;
|
||||
fn output_info(&self);
|
||||
fn as_collectable_mut(&mut self) -> &mut dyn Collectable;
|
||||
fn as_collectable_with_default_ref(&self) -> &dyn CollectableWithDefault;
|
||||
fn as_collectable_with_default_mut(&mut self) -> &mut dyn CollectableWithDefault;
|
||||
fn ts_min(&self) -> Option<u64>;
|
||||
fn ts_max(&self) -> Option<u64>;
|
||||
fn take_new_events_until_ts(&mut self, ts_end: u64) -> Box<dyn Events>;
|
||||
fn move_into_fresh(&mut self, ts_end: u64) -> Box<dyn Events>;
|
||||
fn move_into_existing(&mut self, tgt: &mut Box<dyn Events>, ts_end: u64) -> Result<(), ()>;
|
||||
fn clone_dyn(&self) -> Box<dyn Events>;
|
||||
fn partial_eq_dyn(&self, other: &dyn Events) -> bool;
|
||||
fn serde_id(&self) -> &'static str;
|
||||
fn nty_id(&self) -> u32;
|
||||
}
|
||||
|
||||
erased_serde::serialize_trait_object!(Events);
|
||||
|
||||
impl PartialEq for Box<dyn Events> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
Events::partial_eq_dyn(self.as_ref(), other.as_ref())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user