Reputation: 15276
I have a generic container struct like this:
struct Container<T> {
data: T,
key: String
}
I want to implement Drop
for Container<T>
when T
implements an additional trait.
For instance:
trait Cleanup {
fn clean(&self);
}
I want to implement Drop
for Container
in the case that T: Cleanup
. In my case I have implemented for my concrete type MyStruct
:
impl Cleanup for MyStruct { ... }
I tried to confine the Drop
impl like so:
impl<T> Drop for Container<T> where T: Cleanup {
fn drop(&mut self) { self.data.cleanup() ; }
}
But I get this error:
`Drop` impl requires `T: Cleanup` but the struct it is implemented for does not
Upvotes: 0
Views: 889
Reputation: 58785
It's not allowed for Drop
to only be implemented for some instantiations of a type. Since your Drop
implementation requires that T: Cleanup
, you need to make sure that T: Cleanup
for every possible instance of Container<T>
, i.e. define the struct like this:
struct Container<T: Cleanup> {
data: T,
key: String
}
You can see an explanation like this if you follow the link for the full description for E0367, the error code that you got.
Upvotes: 2