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

Use a macro to implement context methods #972

Merged
merged 2 commits into from
May 31, 2020
Merged

Conversation

cmyr
Copy link
Member

@cmyr cmyr commented May 22, 2020

This attempts to solve the issue of duplicate code and out of
date documentation for methods shared between multiple contexts.

The approach taken here is to use a simple macro to implement
those methods.

The macro was actually a real pain to write, and kind of gross API
wise; in particular you need to pass in all of the lifetimes twice.
I've given up trying to improve it, and given that this is private API
it doesn't matter too much, but it does offend my sensibilites
somewhat.

There are a few bits of future work; window & window_id should
be shared between all contexts, but that requires a change to
PaintCtx; another change would let us reuse is_focused.

In any case, I think this is a step in a good direction.

This is based off of #970, which should go in first.

@luleyleo luleyleo mentioned this pull request May 22, 2020
@luleyleo luleyleo added S-blocked waits for another PR or dependency S-needs-review waits for review labels May 22, 2020
@xStrom
Copy link
Member

xStrom commented May 22, 2020

I really dislike macros for large amount of code because cargo fmt doesn't work on it. There's misformatted stuff in this PR already, but more crucially this will lead over time to either the formatting being a mess or PR review time spent on doing human powered cargo fmt.

I propose we try to solve this using traits instead. One possible way can be seen in #974.

@cmyr
Copy link
Member Author

cmyr commented May 22, 2020

/bloat

@cmyr cmyr force-pushed the common-context-state branch 2 times, most recently from 6713aa7 to 74fa6a9 Compare May 22, 2020 15:35
@xStrom
Copy link
Member

xStrom commented May 22, 2020

If we do continue with a macro for docs purposes, then I wonder if we can somehow have the function definitions/comments outside of the macro, and then a macro that takes a reference to the code. That way cargo fmt could still work.

@cmyr cmyr force-pushed the common-context-state branch 2 times, most recently from 8fc5863 to ce8a1b2 Compare May 22, 2020 17:06
Base automatically changed from common-context-state to master May 22, 2020 17:37
@cmyr
Copy link
Member Author

cmyr commented May 22, 2020

The alternative to a macro that would let us do something like that would be to use build.rs and do codegen. This feels a bit worse in some ways (less discoverable, maybe harder to read the code?) but would at least ensure formatting.

@xStrom
Copy link
Member

xStrom commented May 22, 2020

I read through a bunch of rustfmt issues and apparently it does do formatting in some specific macro cases. It was unclear which though. However there might be a chance to someshow either restructure the macro or wrap individual functions in another macro to enable formatting.

@cmyr
Copy link
Member Author

cmyr commented May 23, 2020

I played around with this without luck, if you've read any issues about this that have reasonable explanations I'd be all ears?

@xStrom
Copy link
Member

xStrom commented May 25, 2020

Okay so I did some more extensive googling and it seems that the macro usage cases that get formatted are expressions. [1] [2]

I tested a simple macro that just takes an expression and fed it a function and cargo fmt indeed formats the function and its body.

@xStrom
Copy link
Member

xStrom commented May 25, 2020

I couldn't get it to work in relation to this PR, although I'm not experienced with macros and may be missing something.

More generally I guess we can minimize the amount of formatting trouble by keeping the macro'd methods mostly lightweight shims and if we need actual work then do that in another method somewhere, e.g. on WidgetState etc.

@cmyr cmyr force-pushed the macro-for-context-methods branch from eda2144 to dc93445 Compare May 29, 2020 18:33
@cmyr cmyr changed the base branch from master to context-text May 29, 2020 18:34
@cmyr
Copy link
Member Author

cmyr commented May 29, 2020

@xStrom assuming that we can land #996, this now works with rustfmt (since we don't need to have any explicit lifetimes in our impls).

I'm pretty satisfied with this, since it also let me really clean up the macro itself, which is now much less gross.

@cmyr cmyr force-pushed the macro-for-context-methods branch from dc93445 to 2cb249b Compare May 29, 2020 18:37
Base automatically changed from context-text to master May 31, 2020 02:49
This attempts to solve the issue of duplicate code and out of
date documentation for methods shared between multiple contexts.

The approach taken here is to use a simple macro to implement
those methods.

A consequence of this work is that methods are now more
consistently available across more contexts; as an example,
`window_id` and `window` are now available from all contexts,
whereas they were previously missing from `PaintCtx` for no
particular reason.
@cmyr cmyr force-pushed the macro-for-context-methods branch from 2cb249b to 716a04c Compare May 31, 2020 02:57
@cmyr cmyr removed the S-blocked waits for another PR or dependency label May 31, 2020
@cmyr
Copy link
Member Author

cmyr commented May 31, 2020

this should now be gtg? It also has one cool property I hadn't really considered: you now need to think really consciously about what set of contexts a method should belong to, which means it will be much rarer for something to just randomly not be available on, say, LifeCycleCtx.

Copy link
Collaborator

@luleyleo luleyleo left a comment

Choose a reason for hiding this comment

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

I think this is a nice consistency and maintenance improvement, now that fmt works.
One typo but looks good overall.

druid/src/contexts.rs Outdated Show resolved Hide resolved
Copy link
Member

@xStrom xStrom left a comment

Choose a reason for hiding this comment

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

Looks good!

@xStrom xStrom removed the S-needs-review waits for review label May 31, 2020
Co-authored-by: Leopold Luley <git@leopoldluley.de>
@cmyr cmyr merged commit c1303e1 into master May 31, 2020
@cmyr cmyr deleted the macro-for-context-methods branch May 31, 2020 17:28
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.

None yet

3 participants