Rust macro for dyn Trait Clones
With this Rust macro...
#[macro_export]
macro_rules! box_dyn_clone {
($vis:vis, $name:ident, $t:ident) => {
#[doc = "Cloneable `Iterator` trait"]
$vis trait $name {
#[doc = "Clones `Box<dyn ...>`"]
fn box_dyn_clone(&self) -> Box<dyn $t>;
}
impl<T> $name for T
where
T: 'static + $t + Clone,
{
fn box_dyn_clone(&self) -> Box<dyn $t> {
Box::new(self.clone())
}
}
impl Clone for Box<dyn $t> {
fn clone(&self) -> Self {
self.box_dyn_clone()
}
}
};
}
it is possible to create dyn Traits
which are cloneable...
pub trait MyItemIter:
Iterator<Item = MyItem> + std::fmt::Debug + MyItemIterClone
{
}
box_dyn_clone!(pub, MyItemIterClone, MyItemIter);
Afterwards you can use it like...
#[derive(Clone, Debug)]
pub struct MyStruct {
pub my_iter: Box<dyn MyItemIter>,
}