Why does not a function generic over a lifetime implement Debug?


#1

Try to compile:
https://play.rust-lang.org/?gist=b9bee34215f276f1ba5c41cbad3b9e18&version=stable&mode=debug

#[derive(Debug)]
struct Foo {
    ops: fn(&mut Foo),
}

fn main() {
    
}

Results in:

error[E0277]: `for<'r> fn(&'r mut Foo)` doesn't implement `std::fmt::Debug`
 --> src/main.rs:3:5
  |
3 |     ops: fn(&mut Foo),
  |     ^^^^^^^^^^^^^^^^^ `for<'r> fn(&'r mut Foo)` cannot be formatted using `:?` because it doesn't implement `std::fmt::Debug`
  |
  = help: the trait `std::fmt::Debug` is not implemented for `for<'r> fn(&'r mut Foo)`
  = note: required because of the requirements on the impl of `std::fmt::Debug` for `&for<'r> fn(&'r mut Foo)`
  = note: required for the cast to the object type `std::fmt::Debug`

error: aborting due to previous error

#2

This is essentially the same issue as Display function pointer

But, how would you like to see this displayed?


#3

In the same way as regular function pointers: https://play.rust-lang.org/?gist=3820dddd9c451c930973e921a0d56db1&version=stable&mode=debug


#4

Please note that I am deriving Debug. Function pointers implement Debug. The problem only comes when you add a reference with generic lifetime to the function.


#5

Yeah, it’s the same issue as with Pointer linked above. You can implement Debug manually to cast away the HRTB.


#6

It is kinda sad to implement Debug manually, but ok. It’s what’s left.


#7

Yeah, I don’t disagree :slight_smile:.