I have a trait Component with an associated type Msg:
trait Component {
type Msg;
fn update(&self, msg: Self::Msg);
}
I modified it to implement a clone:
trait Component: ComponentClone<Self::Msg> {
type Msg;
fn update(&self, msg: Self::Msg);
}
pub trait ComponentClone<T> {
fn clone_box(&self) -> Box<dyn Component<Msg = T>>;
}
impl<T, M> ComponentClone<M> for T
where
T: 'static + Component<M> + Clone,
{
fn clone_box(&self) -> Box<dyn Component<M>> {
Box::new(self.clone())
}
}
impl<M: 'static> Clone for Box<dyn Component<M>> {
fn clone(&self) -> Box<dyn Component<M>> {
self.clone_box()
}
}
I got an error:
error[E0391]: cycle detected when computing the supertraits of `Component`
--> src/lib.rs:1:1
|
1 | trait Component: ComponentClone<Self::Msg> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: ...which again requires computing the supertraits of `Component`, completing the cycle
note: cycle used when collecting item types in top-level module
--> src/lib.rs:1:1
|
1 | trait Component: ComponentClone<Self::Msg> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
If I try to use generics, everything compiles (but I do get warnings):
trait Component<M>: ComponentClone<M> {
fn update(&self, msg: M);
}
pub trait ComponentClone<T> {
fn clone_box(&self) -> Box<dyn Component<T>>;
}
impl<T, M> ComponentClone<M> for T
where
T: 'static + Component<M> + Clone,
{
fn clone_box(&self) -> Box<dyn Component<M>> {
Box::new(self.clone())
}
}
impl<M: 'static> Clone for Box<dyn Component<M>> {
fn clone(&self) -> Box<dyn Component<M>> {
self.clone_box()
}
}
What's wrong? How to solve this problem?