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

Enum pretty-printing #559

Merged
merged 5 commits into from
Nov 21, 2020
Merged

Enum pretty-printing #559

merged 5 commits into from
Nov 21, 2020

Conversation

psychon
Copy link
Owner

@psychon psychon commented Nov 21, 2020

This supersedes #555 and depends on #556 (the commit from that PR is included in this one).

CC #554 and @andrewshadura


Improve Debug-printing of enum values

When we switched from representing enums as Rust enums to representing
them as newtypes around numbers, the derived Debug implementation
followed and instead of printing pretty names, we got just numbers. This
commit stops deriving a Debug implementation and instead one is
implemented manually.

The new Debug impl uses the alternate() flag to decide between
UPPER_CASE and CamelCase. By default, the UPPER_CASE representation that
is now also used in the source is used. With the alternate() flag,
CamelCase is used instead.

This also prints bitmasks as combinations of the individual values. For
example, ModMask::SHIFT | ModMask::LOCK actually is represented as
"SHIFT | LOCK". This was not possible before when we used a Rust enum to
represent these.

Signed-off-by: Uli Schlachter <[email protected]>

When we switched from representing enums as Rust enums to representing
them as newtypes around numbers, the derived Debug implementation
followed and instead of printing pretty names, we got just numbers. This
commit stops deriving a Debug implementation and instead one is
implemented manually.

The new Debug impl uses the alternate() flag to decide between
UPPER_CASE and CamelCase. By default, the UPPER_CASE representation that
is now also used in the source is used. With the alternate() flag,
CamelCase is used instead.

This also prints bitmasks as combinations of the individual values. For
example, ModMask::SHIFT | ModMask::LOCK actually is represented as
"SHIFT | LOCK". This was not possible before when we used a Rust enum to
represent these.

Signed-off-by: Uli Schlachter <[email protected]>
@psychon
Copy link
Owner Author

psychon commented Nov 21, 2020

Rebased on master

Comment on lines +93 to +99
let variants = [
(Self::RAW_RECTANGLES.0.into(), "RAW_RECTANGLES", "RawRectangles"),
(Self::DELTA_RECTANGLES.0.into(), "DELTA_RECTANGLES", "DeltaRectangles"),
(Self::BOUNDING_BOX.0.into(), "BOUNDING_BOX", "BoundingBox"),
(Self::NON_EMPTY.0.into(), "NON_EMPTY", "NonEmpty"),
];
pretty_print_enum(fmt, self.0.into(), &variants)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not a match (or two depending on fmt)?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Bad reason: I started writing the bit variant and then made the other version similar.

Slightly better reason, but I am not sure if this is true: #488. I hope that we only end up with one implementation of the code in the binary that just searches through some lookup table. With a match, I fear that the binary size would be larger. However, I didn't test this and it is just a theory.

Also: Two matches would mean more lines of code. :-)

Signed-off-by: Uli Schlachter <[email protected]>
Clippy complained about: useless conversion to the same type: `u32`

Signed-off-by: Uli Schlachter <[email protected]>
error: very complex type used. Consider factoring parts into `type` definitions
   --> src/utils.rs:286:14
    |
286 |         ) -> CallbackFormating<'a, 'b, fn(&mut Formatter<'_>, u32, &[(u32, &str, &str)]) -> Result>
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: `-D clippy::type-complexity` implied by `-D warnings`
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#type_complexity

Signed-off-by: Uli Schlachter <[email protected]>
In commit 7e4ea9c, I only fixed one of the two cases of "useless
identity conversion". This commit also fixes the other half of the
problem.

Signed-off-by: Uli Schlachter <[email protected]>
@mergify mergify bot merged commit dc4ad1d into master Nov 21, 2020
@mergify mergify bot deleted the pretty-printing2 branch November 21, 2020 16:01
@andrewshadura
Copy link

andrewshadura commented Jul 21, 2021

Yay, I have just tested this and it works just as I wanted it 🙂

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