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

Fix 611 [Pt 1] #640

Closed
wants to merge 8 commits into from
Closed

Fix 611 [Pt 1] #640

wants to merge 8 commits into from

Conversation

scrabsha
Copy link
Contributor

@scrabsha scrabsha commented Jun 7, 2020

The following changes were made:

  • some members of Document where changed: String was replaced by Cow<'a, str>,
  • a copy was removed in the implementation of Documentable for &str.

These two little changes triggered a lot of errors which are now fixed.

A PhantomData was added somewhere in the definition of Document. This caused
a lot of lifetimes to be manually specified in various functions prototypes.
These lifetimes deliberately always 'static, this will be fixed in next commits.

Next commits will focus on adding proper Cow<'a, str> for needed variants. Once
this is done, the PhantomData will be removed.

Tests have been updated and still pass. This was expectable since there is no
change in how the program behaves yet.
All occurences of String in the declaration of Document<'a> have been replaced
with Cow<'a, str>. The rest of the code has been updated, tests still pass.

There is still a lot of changes to be done. Currently, the Owned variant is
always used. Next commits will focus on using the Borrowed variant as much as
possible.
Tests in the pretty module always converted their &'static str to Strings,
last commits converted it to an Owned variant. This commit changes it to the
Borrowed variant, which allows us to remove useless clone.
Previous commit was focused on putting 'static everywhere. This was a mistake
here since it would prevent us from using methods avalaible for Document.
This triggered a lot of errors in the codebase. They have all been fixed.
This is useless now, since Document contains other references.
@scrabsha scrabsha marked this pull request as ready for review June 7, 2020 14:40
Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

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

Thank you for this work, it looks like it has been really fiddly.

I'm not sure I'm happy with what Cow has done to the code here, the amount of duplicated code is extreme and will be quite detrimental to the editing and maintenance experience. Are there combinators that could be used to reduce the duplication?

@scrabsha
Copy link
Contributor Author

scrabsha commented Jun 8, 2020

Thanks for the time you took to review my changes. I did my very best and tried to change code as conservatively as possible.

I understand your concerns about the huge amount of complexity added by this PR. So far, I have noticed three patterns that could be fixed with the use of a procedural macro. They are:

  • destructuring structs wrapped in Cow forces us to match over the two Cow variants (see an example here),
  • matching on enums wrapped in Cow nearly doubles the amount of match arms, resulting in a lot of duplicate code (see here),
  • creating an iterator from a Cow<T> where T implements IntoIterator forces us to match over the two Cow variants, resulting again in a lot of duplication (see here).

For these three patterns, I have a solution which I am implementing on a crate I entitled butcher. The fix for patterns 1 and 2 will remove a lot of boilerplate at no runtime cost. The fix for pattern 3 will introduce a match at each iteration step.

There is also some complexity that can't (as far as I know) be simplified. For example, many lifetimes have to be written manually now. They are not hard to understand, but definitely add some complexity.

This list may not be complete. Don't hesitate to report every pattern introduced here that you want to disappear, so I can investigate to find a fix.

I'll work on butcher to make it usable, and then remove as much duplicate as possible.

@scrabsha scrabsha marked this pull request as draft June 9, 2020 10:40
@lpil lpil closed this Jun 12, 2020
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

2 participants