displaydoc::expand

Function generate_where_clause

source
fn generate_where_clause(
    generics: &Generics,
    where_clause: Option<&WhereClause>,
) -> WhereClause
Expand description

Generate a where clause that ensures all generic type parameters impl core::fmt::Display unless already constrained.

This approach allows struct/enum definitions deriving crate::Display to avoid hardcoding a core::fmt::Display constraint into every type parameter.

If the type parameter isn’t already constrained, we add a where _: Display clause to our display implementation to expect to be able to format every enum case or struct member.

In fact, we would preferably only require where _: Display or where _: Debug where the format string actually requires it. However, while std::fmt defines a formal syntax for format!(), it doesn’t expose the actual logic to parse the format string, which appears to live in rustc_parse_format. While we use the syn crate to parse rust syntax, it also doesn’t currently provide any method to introspect a format!() string. It would be nice to contribute this upstream in syn.