use std::{
fmt::Debug,
ops::{Deref, DerefMut},
};
use peace_cfg::Item;
use peace_data::fn_graph::{DataAccessDyn, TypeIds};
use peace_params::{Params, ParamsMergeExt};
use crate::{ItemRt, ItemWrapper};
#[derive(Debug)]
pub struct ItemBoxed<E>(Box<dyn ItemRt<E>>);
impl<E> Clone for ItemBoxed<E> {
fn clone(&self) -> Self {
Self(dyn_clone::clone_box(self.0.as_ref()))
}
}
impl<E> Deref for ItemBoxed<E> {
type Target = dyn ItemRt<E>;
fn deref(&self) -> &Self::Target {
&*self.0
}
}
impl<E> DerefMut for ItemBoxed<E> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut *self.0
}
}
impl<E> PartialEq for ItemBoxed<E>
where
E: 'static,
{
fn eq(&self, other: &Self) -> bool {
self.0.eq(&*other.0)
}
}
impl<E> Eq for ItemBoxed<E> where E: 'static {}
impl<I, E> From<I> for ItemBoxed<E>
where
I: Clone + Debug + Item + Send + Sync + 'static,
<I as Item>::Error: Send + Sync,
E: Debug
+ Send
+ Sync
+ std::error::Error
+ From<<I as Item>::Error>
+ From<crate::Error>
+ 'static,
for<'params> <I as Item>::Params<'params>:
ParamsMergeExt + TryFrom<<<I as Item>::Params<'params> as Params>::Partial>,
for<'params> <<I as Item>::Params<'params> as Params>::Partial: From<
<<I as Item>::Params<'params> as TryFrom<
<<I as Item>::Params<'params> as Params>::Partial,
>>::Error,
>,
for<'params> <I::Params<'params> as Params>::Partial: From<I::Params<'params>>,
{
fn from(item: I) -> Self {
Self(Box::new(ItemWrapper::from(item)))
}
}
impl<E> DataAccessDyn for ItemBoxed<E> {
fn borrows(&self) -> TypeIds {
DataAccessDyn::borrows(self.0.as_ref())
}
fn borrow_muts(&self) -> TypeIds {
DataAccessDyn::borrow_muts(self.0.as_ref())
}
}