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

Add lifetime param to ParseElem #269

Closed
wants to merge 2 commits into from

Conversation

zsol
Copy link
Contributor

@zsol zsol commented Sep 1, 2021

This PR adds an 'input lifetime parameter to ParseElem and changes changes the signature of parse_elem to take self by &'input.

This change allows ParseElem implementations that return Elements by reference, avoiding a potentially expensive clone(). Case in point, the implementation of parse_elem in ParseElem for [T] now returns &'input T.

Both of these are breaking changes.

Closes #268.

@@ -15,12 +15,12 @@ impl<T> Parse for [T] {
}
}

impl<T: Clone> ParseElem for [T] {
type Element = T;
impl<'input, T: 'input + Clone> ParseElem<'input> for [T] {
Copy link

Choose a reason for hiding this comment

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

It seems that Clone bound is not required anymore

@kevinmehall
Copy link
Owner

I'm not sure about the second commit -- it seems weird that [u8] would give you &u8 while str gives you char. This is actually covered up quite well by match ergonomics automatically dereferencing in the examples from the tests, but are there other cases that expose the reference type and might be confusing?

Perhaps that Clone bound should be replaced with Copy? Then if you're using some non-Copy token type where you want borrowing, recommend a newtype wrapper and a custom ParseElem impl that can return whatever you want, enabled by the new lifetime parameter.

@kevinmehall
Copy link
Owner

I cherry-picked the first commit adding the lifetime param.

Instead of the second commit, I made the possibly-controversial choice to require ParseElem::Element: Copy for all implementations, not just for [T]. This unlocks improvements like #234, as well as better representing the expectation that they are cheap to copy, because they're passed around a lot.

As far as I'm aware, LibCST is the only user of rust-peg this will break, but I assume you wanted to return &Rc<...> anyway to avoid a lot of refcount manipulation, and were waiting for the lifetime param in order to enable that.

@kevinmehall kevinmehall closed this Jan 2, 2022
@zsol
Copy link
Contributor Author

zsol commented Jan 3, 2022

assume you wanted to return &Rc<...> anyway to avoid a lot of refcount manipulation,

Correct. That was the motivation for this pr

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.

ParseElem missing lifetime specifiers
3 participants