-
Notifications
You must be signed in to change notification settings - Fork 3
fix(join): always put comma at the end of tuple statement #29
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
Conversation
|
More context @dodomorandi and why this is necessary: there are cases in which we need to generate a one-element tuple. The current implementation doesn't leave a trailing comma, so what gets generated is |
benzina-derive/src/join/mod.rs
Outdated
| let mut accumulator = ::benzina::__private::indexmap::map::Entry::or_insert( | ||
| ::benzina::__private::IndexMap::entry(&mut #accumulator_index, #id), | ||
| (#(#or_insert),*) | ||
| (#(#or_insert),*,) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The base idea is perfect, the only thing that is not convincing me is what happens when or_insert is empty. In that case the expression (,) is being created, but that is not valid.
Do we have any guarantee or_insert is never empty? If not we could just create a branch that creates a token stream with the current expansion if or_insert is not empty and, in the other case, it just creates a unit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we make some kind of helper for generating tuples that also handles the unit case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea indeed!
eb7b659 to
0c25b19
Compare
paolobarbolini
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
benzina-derive/src/join/mod.rs
Outdated
| fn tuple_from_vec(vec: &[impl ToTokens]) -> TokenStream { | ||
| if vec.is_empty() { | ||
| quote! { () } | ||
| } else { | ||
| quote! { (#(#vec),*,) } | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd move this to utils.rs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extreme nitpick mode on: I would propose a couple of renames and a change of input type:
fn tuple_from_tokenizables<I>(tokenizables: I) -> TokenStream
where
I: IntoIterator<Item: ToTokens, IntoIter: ExactSizeIterator>,
{
let tokenizables = tokenizables.into_iter();
if tokenizables.len() == 0 {
quote! { () }
} else {
quote! { (#(#tokenizables),*,) }
}
}tokenizables is not the best name I could think of, if you have a better proposal feel free to go on.
Unfortunately ExactSizeIterator::is_empty is still unstable, the following approach could be possible but I don't think it's worth it.
fn tuple_from_tokenizables<I>(tokenizables: I) -> TokenStream
where
I: IntoIterator<Item: ToTokens>,
{
let mut tokenizables = tokenizables.into_iter();
if let Some(tokenizable) = tokenizables.next() {
quote! { (#tokenizable, #(#tokenizables),*,) }
} else {
quote! { () }
}
}Said that, you can also go on with the current implementation. My proposal is useful in case you do not have a slice of tokenizable elements.
0c25b19 to
06c0083
Compare
…ize a _one elemnt tuple_
06c0083 to
3223481
Compare
No description provided.