Enforcing Drop implementation for a drop guard trait

⚓ Rust    📅 2025-12-01    👤 surdeus    👁️ 3      

surdeus

/// A cancellation guard cancels an ongoing operation when dropped.
#[must_use = "Cancels the operation if dropped."]
trait Cancelable: Drop {}

trait MyApi {
  fn start_op() -> impl Cancelable
}

This obviously gives me the well-known lint warning: "bounds on Self: core::ops::Drop are most likely incorrect, consider instead using core::mem::needs_drop to detect whether a type can be trivially dropped."

It's not my intention to strictly enforce anything, though. I just want to give implementors a hint what is expected. This is about ergonomics, not about safety or correctness: Implementing Cancelable without also implementing Drop is almost certainly wrong.

I've seen, though, that it has even been suggested to forbid this pattern altogether. So, what's so bad about this pattern?

3 posts - 3 participants

Read full topic

🏷️ Rust_feed