Closed
Description
struct Foo<T>(T);
impl<T> Foo<T> {
fn new(x: T) -> Foo<T> { Foo(x) }
// ... methods without restrictions ...
fn restricted_good(&self) where T: Clone {}
}
impl<T: Clone> Foo<T> {
fn restricted_bad(&self) {}
}
struct NotClone;
fn main() {
let x = Foo::new(NotClone);
x.restricted_good();
x.restricted_bad();
}
<anon>:19:7: 19:24 error: the trait `core::clone::Clone` is not implemented for the type `NotClone`
<anon>:19 x.restricted_good();
^~~~~~~~~~~~~~~~~
<anon>:20:7: 20:23 error: type `Foo<NotClone>` does not implement any method in scope named `restricted_bad`
<anon>:20 x.restricted_bad();
^~~~~~~~~~~~~~~~
The _bad
one is completely unhelpful, the _good
is much better since it allows one to diagnose the problem. It would be nice for them both to be good.
This comes up relatively often with RefCell::<T, E>::unwrap
(which requires E: Show
in the impl
), and Mutex::<T>::lock
(which requires T: Send
in the impl).