Refactor AsAny handling
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
use crate::collect_s::ToJsonBytes;
|
||||
use crate::collect_s::ToJsonResult;
|
||||
use crate::AsAnyMut;
|
||||
use crate::AsAnyRef;
|
||||
use crate::Events;
|
||||
use err::Error;
|
||||
use std::any::Any;
|
||||
use std::fmt;
|
||||
|
||||
pub trait Collector: fmt::Debug + Send {
|
||||
@@ -14,7 +14,7 @@ pub trait Collector: fmt::Debug + Send {
|
||||
fn result(&mut self) -> Result<Box<dyn Collected>, Error>;
|
||||
}
|
||||
|
||||
pub trait Collectable: fmt::Debug + crate::AsAnyMut {
|
||||
pub trait Collectable: fmt::Debug + AsAnyMut {
|
||||
fn new_collector(&self) -> Box<dyn Collector>;
|
||||
}
|
||||
|
||||
@@ -23,20 +23,10 @@ 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> {}
|
||||
@@ -57,15 +47,8 @@ pub trait CollectorDyn: fmt::Debug + Send {
|
||||
fn result(&mut self) -> Result<Box<dyn Collected>, Error>;
|
||||
}
|
||||
|
||||
pub trait CollectableWithDefault {
|
||||
pub trait CollectableWithDefault: AsAnyMut {
|
||||
fn new_collector(&self) -> Box<dyn CollectorDyn>;
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any;
|
||||
}
|
||||
|
||||
impl crate::AsAnyMut for Box<dyn Events> {
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Collectable for Box<dyn Events> {
|
||||
@@ -99,12 +82,6 @@ impl Collector for TimeBinnedCollector {
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::AsAnyMut for Box<dyn crate::TimeBinned> {
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Collectable for Box<dyn crate::TimeBinned> {
|
||||
fn new_collector(&self) -> Box<dyn Collector> {
|
||||
self.as_ref().new_collector()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::collect_c::Collected;
|
||||
use crate::WithLen;
|
||||
use crate::{AsAnyMut, AsAnyRef, WithLen};
|
||||
use err::Error;
|
||||
use serde::Serialize;
|
||||
use std::any::Any;
|
||||
@@ -26,14 +26,13 @@ pub trait Collector: Send + Unpin + WithLen {
|
||||
}
|
||||
|
||||
// TODO rename to `Typed`
|
||||
pub trait CollectableType {
|
||||
pub trait CollectableType: AsAnyRef + AsAnyMut {
|
||||
type Collector: CollectorType<Input = Self>;
|
||||
fn new_collector() -> Self::Collector;
|
||||
}
|
||||
|
||||
pub trait Collectable: Any {
|
||||
pub trait Collectable: AsAnyRef + AsAnyMut + Any {
|
||||
fn new_collector(&self) -> Box<dyn Collector>;
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any;
|
||||
}
|
||||
|
||||
impl<T: CollectorType + 'static> Collector for T {
|
||||
@@ -56,15 +55,13 @@ impl<T: CollectorType + 'static> Collector for T {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: CollectableType + 'static> Collectable for T {
|
||||
impl<T> Collectable for T
|
||||
where
|
||||
T: CollectableType + 'static,
|
||||
{
|
||||
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
|
||||
@@ -73,21 +70,28 @@ pub trait ToJsonBytes {
|
||||
}
|
||||
|
||||
// TODO check usage of this trait
|
||||
pub trait ToJsonResult: erased_serde::Serialize + fmt::Debug + Send {
|
||||
pub trait ToJsonResult: erased_serde::Serialize + fmt::Debug + AsAnyRef + AsAnyMut + Send {
|
||||
fn to_json_result(&self) -> Result<Box<dyn ToJsonBytes>, Error>;
|
||||
fn as_any(&self) -> &dyn Any;
|
||||
}
|
||||
|
||||
erased_serde::serialize_trait_object!(ToJsonResult);
|
||||
|
||||
impl AsAnyRef for serde_json::Value {
|
||||
fn as_any_ref(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl AsAnyMut for serde_json::Value {
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -101,8 +105,4 @@ 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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,10 @@ pub mod collect_s;
|
||||
pub mod scalar_ops;
|
||||
pub mod subfr;
|
||||
|
||||
pub mod bincode {
|
||||
pub use bincode::*;
|
||||
}
|
||||
|
||||
use collect_c::CollectableWithDefault;
|
||||
use collect_s::Collectable;
|
||||
use collect_s::ToJsonResult;
|
||||
@@ -61,6 +65,24 @@ pub trait AsAnyMut {
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any;
|
||||
}
|
||||
|
||||
impl<T> AsAnyRef for Box<T>
|
||||
where
|
||||
T: AsAnyRef + ?Sized,
|
||||
{
|
||||
fn as_any_ref(&self) -> &dyn Any {
|
||||
self.as_ref().as_any_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> AsAnyMut for Box<T>
|
||||
where
|
||||
T: AsAnyMut + ?Sized,
|
||||
{
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self.as_mut().as_any_mut()
|
||||
}
|
||||
}
|
||||
|
||||
/// Data in time-binned form.
|
||||
pub trait TimeBinned: Any + TimeBinnable + crate::collect_c::Collectable {
|
||||
fn as_time_binnable_dyn(&self) -> &dyn TimeBinnable;
|
||||
@@ -94,11 +116,9 @@ pub trait TimeBinner: Send {
|
||||
|
||||
/// 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 {
|
||||
pub trait TimeBinnable: fmt::Debug + WithLen + RangeOverlapInfo + Any + AsAnyRef + AsAnyMut + 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>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user