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 Input for Iterator<Item = Input> #101

Open
soenkehahn opened this issue Jul 16, 2021 · 3 comments
Open

Implement Input for Iterator<Item = Input> #101

soenkehahn opened this issue Jul 16, 2021 · 3 comments

Comments

@soenkehahn
Copy link
Owner

I recently wanted to pass std::env::args().skip(1) to a child process. So I did std::env::args().skip(1).collect::<Vec<_>>(). Maybe there should be an easier way to pass in iterators?

@casey
Copy link
Collaborator

casey commented Sep 12, 2021

I think we would need a wrapper type, since we can't due blanket impls due to the impl on &I where I: Input.

This works:

struct Inputs<I: IntoIterator<Item = T>, T: Input>(I);

impl<I: IntoIterator<Item = T>, T: Input> Input for Inputs<I, T> {
    fn configure(self, config: &mut Config) {
        for input in self.0 {
            input.configure(config);
        }
    }
}

#[test]
fn can_pass_multiple_inputs_with_inputs() {
    let crate::StdoutTrimmed(s) = crate::run_output!("echo", Inputs(vec!["a", "b", "c"]));
    assert_eq!(s, "a b c");
}

@soenkehahn
Copy link
Owner Author

Yeah, I was also thinking that a wrapper type might be necessary.

Out of curiosity, what exactly is the advantage of using IntoIterator over Iterator here?

@casey
Copy link
Collaborator

casey commented Sep 12, 2021

Out of curiosity, what exactly is the advantage of using IntoIterator over Iterator here?

Usually, Iterator is implemented by a specialized iterator types, for example std::vec::Iter or std::collections::hash_map::Keys. Vec and HashMap themselves instead implement IntoIterator to return an iterator.

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

No branches or pull requests

2 participants