LibAFL icon indicating copy to clipboard operation
LibAFL copied to clipboard

Use PhantomData<fn() -> (T0, T1, ...)> everywhere

Open addisoncrump opened this issue 1 year ago • 2 comments

This was a trick I saw here: https://github.com/astral-sh/ruff/pull/10849#discussion_r1562158940

This has a lot of nice qualities for derive that can help us with #2068:

  • Eq
  • Copy
  • Debug
  • Serialize/Deserialize (with #[serde(skip)])
  • Send/Sync (in case we ever do any async)

If a lifetime is stored in the PhantomData, then use PhantomData<fn() -> &'a &'b ... (T0, T1, ...)>.

addisoncrump avatar Apr 26 '24 01:04 addisoncrump

Where exactly will this help us? Think I'm too stupid to understand

domenukk avatar Apr 26 '24 07:04 domenukk

No, this one's pretty arcane. I had no idea this trick existed until yesterday.

Basically, we currently require e.g. T: Debug in a lot of places because we hold PhantomData<T>, but this is actually an excessive restriction because T is never debug'd. Similarly with T: Copy, T: Eq, T: Serialize + Deserialize: we probably have lots of places where we can simplify the generic constraints because we never actually handle the type except in passing (e.g. in methods).

addisoncrump avatar Apr 26 '24 09:04 addisoncrump

When deriving the Debug trait, the type parameter T is required to implement Debug as well. However, this requirement is imposed by the derive macro itself, not by the presence of PhantomData<T>. Therefore, changing PhantomData<T> to PhantomData<fn() -> T> will not alter this requirement. The derive macro will still enforce T: Debug regardless of which version of PhantomData is used.

fiveseven-lambda avatar Sep 05 '24 04:09 fiveseven-lambda

So it would seem. This is very annoying, I thought I checked that... :confused:

addisoncrump avatar Sep 05 '24 13:09 addisoncrump