I have a trait Foo
. I want to force implementors to define a method, if those implementors implement another trait (Clone
in this example). My idea (Playground):
trait Foo {
// Note: in my real application, the trait has other methods as well,
// so I can't simply add `Clone` as super trait
fn foo(&self)
where
Self: Clone;
}
struct NoClone;
impl Foo for NoClone {}
Sadly, this leads to:
error[E0046]: not all trait items implemented, missing: `foo`
--> src/lib.rs:8:1
|
2 | / fn foo(&self)
3 | | where
4 | | Self: Clone;
| |____________________- `foo` from trait
...
8 | impl Foo for NoClone {}
| ^^^^^^^^^^^^^^^^^^^^ missing `foo` in implementation
I don't understand this error: the compiler clearly knows that NoClone
does not implement Clone
, so why am I required to provide a definitoin for foo
? In particular, if I attempt to provide a definition (Playground):
impl Foo for NoClone {
fn foo(&self)
where
Self: Clone
{
unreachable!()
}
}
I get the error:
error[E0277]: the trait bound `NoClone: std::clone::Clone` is not satisfied
--> src/lib.rs:9:5
|
9 | / fn foo(&self)
10 | | where
11 | | Self: Clone
12 | | {
13 | | unreachable!()
14 | | }
| |_____^ the trait `std::clone::Clone` is not implemented for `NoClone`
|
= help: see issue #48214
= help: add #![feature(trivial_bounds)] to the crate attributes to enable
So the compiler knows for sure. (FYI: with #![feature(trivial_bounds)]
it compiles, but I don't want to define a bunch of methods with unreachable!()
as body.)
Why does the compiler force me to provide the method definition? Can I work around this problem somehow?