I am trying to compare two function objects to see if they point to the same function. However I can't seem to figure out how to do that. I get this error with the below sample code
error[E0369]: binary operation `==` cannot be applied to type `for<'r> fn(&'r u64) -> u64`
if I change the function signature from (&u64) -> u64 to (u64) -> u64 it all works. I am not sure why there is an issue if the function signature contains references.
If they're the same function, their addresses will be identical, and since functions with different lifetimes are still compiled to the same machine code, you don't need to worry about the HRTB errors you're getting:
It's not guaranteed that function pointers to the same function have same address. Same generic function with same type parameters can be monomorphised in different compilation unit independently.
since functions with different lifetimes are still compiled to the same machine code
Is that the reason it wouldn't let me compare them? Because the functions could have different lifetimes (which makes they type "different" according to rust)?
In a sense you're right, however the view I came up with as to why it doesn't work is this: HRTBs-ed types (like your function) are groups of types, which have an infinite number of members (because there could exist an infinite number of lifetimes in a rust program). Hence we are restricted to comparing discrete functions (by forcibly resolving them as @steffahn has done) or completely ignoring them as I exemplified.
Note however that this does suffer from the issues as presented by @cuviper and @Hyeonu, and such something like so might be result in true:
It looks like a new language feature is needed to resolve this correctly. A language feature that makes it possible to blanket implement traits across all function pointer types (and similar, fn items?), not just a "hand picked" subset of them.