JelteF / derive_more

Some more derive(Trait) options
MIT License
1.74k stars 123 forks source link

Only put Display-like bounds on type variables #387

Closed JelteF closed 3 months ago

JelteF commented 4 months ago

Only put Display-like bounds on type variables

Resolves #363 Requires #377 Related to #371

Synopsis

The problem is that the Display derive adds a bounds for all types that are used in the format string. But this is not necessary for types that don't contain a type variable. And adding those bounds can result in errors like for the following code:

#[derive(Display, Debug)]
#[display("{inner:?}")]
#[display(bounds(T: Display))]
struct OptionalBox<T> {
    inner: Option<Box<T>>,
}

#[derive(Display, Debug)]
#[display("{next}")]
struct ItemStruct {
    next: OptionalBox<ItemStruct>,
}

That code would generate the following error:

error[E0275]: overflow evaluating the requirement `ItemStruct: derive_more::Display`

Solution

This makes sure we don't add unnecessary bounds for Display-like derives. It does so in the same way as #371 did for the Debug derive: By only adding bounds when the type contains a type variable.

Checklist

Changelog is not updated because the 0.99 did not have this issue.