logoalt Hacker News

dwattttt • yesterday at 9:50 PM • 1 reply • view on HN

This code, from the bug submission?

  impl<T: 'static + Send> Drop for Q<T> {
      fn drop(&mut self) { if let Some(t) = self.0.take() { KEPT.lock().unwrap().push(Box::new(t)); } }
  }
The bound on T in the Drop impl means Drop is not implemented for all T, only where T: 'static + Send. Q<T> also having the same bound doesn't make a difference to the Drop impl.

Replies

NobodyNada • yesterday at 10:36 PM

You cannot write a type which conditionally implements Drop: https://play.rust-lang.org/?version=stable&mode=debug&editio...

    struct Q<T>(Option<T>);

    impl<T: 'static> Drop for Q<T> {
        fn drop(&mut self) {}
    }


    error[E0367]: `Drop` impl requires `T: 'static` but the struct it is implemented for does not
     --> src/main.rs:3:9
      |
    3 | impl<T: 'static> Drop for Q<T> {
      |         ^^^^^^^
      |
    note: the implementor must specify the same requirement
     --> src/main.rs:1:1
      |
    1 | struct Q<T>(Option<T>);
      | ^^^^^^^^^^^
➕ show 1 reply