1

I'm trying to implement additional behaviour for a generic type if its type parameters meet particular trait bounds:

struct Foo<T> {
    data: T
}

impl<T> Drop for Foo<T> where T: Debug {
    fn drop(&mut self) {
        println!("{:?} has been deallocated", self.data);
    }
}

The idea being that Foo would implement Drop if T implements Debug, while still allowing Foo to be used for types T without Debug (which simply won't implement Drop). However, this results in a compiler error:

`Drop` impl requires `T: Debug` but the struct it is implemented for does not

Is there a way to achieve this behaviour (without splitting Foo into two separate types)?

This discussion suggests that this is not possible (or at least wasn't at the time):

Apparently this is only an issue for the Drop trait, for other traits it works just fine:

struct Foo<T> {
    data: T
}

trait MyDrop {
    fn drop(&mut self);
}

impl<T> MyDrop for Foo<T> where T: Debug {
    fn drop(&mut self) {
        println!("{:?} has been deallocated", self.data);
    }
}

So I guess my real question is: Why is conditional destructor implementation different from conditional implementation of other traits?

3
  • 1
    This discussion: users.rust-lang.org/t/… suggests that this is not possible (or at least wasn't at the time). Commented Aug 29, 2022 at 1:32
  • 1
    This is still true. Commented Aug 29, 2022 at 2:01
  • Any reason why this is prohibited, and is this something that will be addressed in the future? Conditional implementation of Foo methods seems to work fine (doc.rust-lang.org/book/…). Commented Aug 29, 2022 at 2:07

1 Answer 1

1

After reading and thinking about it some more, I believe the issue is that allowing conditional implementation for Drop would cause a conflict with the compiler-generated default implementation of Drop. That's similar to the way you cannot currently specialize an implementation, as discussed here.

However, once specialization becomes stable it might be possible to allow conditional implementation of Drop as well. Does that sound correct?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.