This is my current problem:
/// The internal operations are simple and cheap, but users of the library
/// can create custom expensive operations.
pub trait Operation<Element> {
/// Since Element could be something big and complex, here it is better to
/// use always a reference instead of passing the value.
fn operator(&mut self, element: &Element);
}
/// An element could be a number or other types that could be used for the operations.
pub trait Element {}
/// Enum to allow storing any operation.
/// Since this enum is used everywhere, it needs to support Clone and other traits.
#[derive(Clone, PartialEq, Eq)]
pub enum Operations {
Sum(CheapSum),
Product(CheapProduct),
/// This variant is only for the users of the library, so they can create their
/// own operations.
Custom(Box<dyn for<'a> Operation<Box<&'a dyn Element>>>),
}
#[derive(Clone, PartialEq, Eq)]
pub struct CheapSum;
impl<T: Element> Operation<T> for CheapSum {
fn operator(&mut self, element: &T) {
todo!()
}
}
#[derive(Clone, PartialEq, Eq)]
pub struct CheapProduct;
impl<T: Element> Operation<T> for CheapProduct {
fn operator(&mut self, element: &T) {
todo!()
}
}
// Doesn't work
// impl<T: Element> Clone for Box<dyn for<'a> Operation<&'a T>> {
// fn clone(&self) -> Self {
// todo!()
// }
// }
//
// impl<T: Element> PartialEq for Box<dyn for<'a> Operation<&'a T>> {
// fn eq(&self, other: &Self) -> bool {
// todo!()
// }
// }
//
// impl<T: Element> Eq for Box<dyn for<'a> Operation<&'a T>> {}
// Doesn't work
// trait CloneBox {
// fn clone_box(&self) -> Box<dyn for<'a> Operation<&'a dyn Element>>;
// }
//
// impl<T> CloneBox for T
// where
// T: for<'a> Operation<&'a (dyn Element + 'a)> + Clone + 'static,
// {
// fn clone_box(&self) -> Box<dyn for<'a> Operation<&'a (dyn Element + 'a)> + 'static> {
// Box::new(self.clone())
// }
// }