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

Implement Accumulate for tuples (e.g. Accumulate<(T, T)> for Vec<T>) #483

Open
2 tasks done
axelkar opened this issue Feb 25, 2024 · 3 comments
Open
2 tasks done
Labels
A-combinator Area: combinators C-enhancement Category: Raise on the bar on expectations

Comments

@axelkar
Copy link

axelkar commented Feb 25, 2024

Please complete the following tasks

winnow version

0.6.2

Describe your use case

Here, let me show you:

fn node(input: &mut Stream) -> Item;
/// whitespace token
fn wst(input: &mut Stream) -> Item;

let a: Vec<Item> = repeat(0.., (node, wst))
    .map(|vec: Vec<_>| vec.into_iter().map(|(a, b)| [a, b]).flatten().collect()) // make it possible without this line!!
    .parse("whatever").unwrap();

Describe the solution you'd like

#[cfg(feature = "alloc")]
impl<T> Accumulate<(T, T)> for Vec<T> {
    #[inline(always)]
    fn initial(capacity: Option<usize>) -> Self {
        match capacity {
            Some(capacity) => Vec::with_capacity(clamp_capacity::<T>(capacity)),
            None => Vec::new(),
        }
    }
    #[inline(always)]
    fn accumulate(&mut self, acc: (T, T)) {
        self.push(acc.0);
        self.push(acc.1);
    }
}
// the same for (T, T, T), (T, T, T, T) and so on until what? 12 items?

Alternatives, if applicable

maybe you could instead use impl Into<[T; N]>:
https://doc.rust-lang.org/std/primitive.tuple.html#impl-From%3C(T,)%3E-for-%5BT;+1%5D

Additional Context

No response

@axelkar axelkar added the C-enhancement Category: Raise on the bar on expectations label Feb 25, 2024
@axelkar
Copy link
Author

axelkar commented Feb 25, 2024

Oh and Option in any of the tuple's fields would be great too. Just noticed that I only optionally need the whitespace token.

@epage
Copy link
Collaborator

epage commented Feb 26, 2024

I worry about the number of combinations we'd be dealing with and how that would affect users in different ways (reading docs, compile times, etc).

Is there a reason to not use repeat().fold() instead?

@epage epage added the A-combinator Area: combinators label Feb 26, 2024
@axelkar
Copy link
Author

axelkar commented Feb 27, 2024

I didn't even think of making a Vec in fold. Thanks! After that this issue doesn't concern me nearly as much, but it's still manual.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-combinator Area: combinators C-enhancement Category: Raise on the bar on expectations
Projects
None yet
Development

No branches or pull requests

2 participants