Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve derive-Display trait bounds inference (#93) #95

Conversation

ffuugoo
Copy link
Contributor

@ffuugoo ffuugoo commented Oct 21, 2019

First half of #93.

Modified Display macro, so that for each field of the following types or of reference to
such types
a where clause is generated bounding field's type by Display trait:

  • T
  • T::AssociatedType and <T as Trait>::AssociatedType
  • ::path::SomeOtherType<T> and ::path::SomeOtherType<&T>

E.g., for a structure Foo defined like this:

#[derive(Display)]
struct Foo<'a, T1, T2: Trait, T3> {
    field1: T1,
    field2: <T2 as Trait>::Type,
    field3: Bar<T3>,
    field4: &'a T1,
    field5: &'a <T2 as Trait>::Type,
    field6: &'a Bar<T3>,
}

Following where clauses would be generated (notice, that for reference types the
referred type is bound):

  • T1: Display
  • <T2 as Trait>::Type: Display
  • Bar<T3>: Display

As far as I understand, as we can't define Display for pointers, arrays, slices and tuples, adding bounds for such types (or any generic types parametrized by such types, e.g., SomeType<[T; 10]>) would be wrong.

@ffuugoo ffuugoo marked this pull request as ready for review October 21, 2019 11:26
Copy link
Collaborator

@tyranron tyranron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems nice! 👍

Tested on a real project, works as expected.

@ffuugoo
Copy link
Contributor Author

ffuugoo commented Oct 22, 2019

Implemented proper trait bounds inference for reference types as suggested by @tyranron in discussion in #93.

@JelteF
Copy link
Owner

JelteF commented Oct 22, 2019

Can you add a test for the reference types?

@ffuugoo
Copy link
Contributor Author

ffuugoo commented Oct 23, 2019

@JelteF

@JelteF
Copy link
Owner

JelteF commented Oct 23, 2019 via email

@JelteF JelteF merged commit f13d4cf into JelteF:master Oct 26, 2019
@tyranron tyranron deleted the improve-derive-display-trait-bounds-inference branch October 27, 2019 22:43
JelteF pushed a commit that referenced this pull request Nov 2, 2019
Second half of #93. This PR is based on top of #95.

Implemented [display(bound = "...")] attribute for Display macro as proposed by @tyranron in #93.
@JelteF
Copy link
Owner

JelteF commented Nov 11, 2019

This is now released as version 0.99.0. Announcement link: https://www.reddit.com/r/rust/comments/duptc0/derive_more_0990_released_biggest_release_ever/

@ffuugoo ffuugoo changed the title WIP: Improve derive-Display trait bounds inference (#93) Improve derive-Display trait bounds inference (#93) Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants