Skip to content

Conversation

AlexanderThaller
Copy link
Contributor

Reimplentation of #34 which allows to pass a iterator instead of a vector.

@AlexanderThaller AlexanderThaller changed the title Implement pipeline constructor from exec vector Implement pipeline constructor from exec iterator Aug 31, 2021
@hniksic
Copy link
Owner

hniksic commented Sep 11, 2021

Pipeline::from_iter() should check that the iterator produced at least two Execs. For example, Pipeline::popen() expects it, and asserts that self.cmds.len() >= 2.

Note that even without this PR it's possible to create pipelines with a dynamic number of components, it's just less convenient:

let cmds: Vec<Exec> = ...;
cmds.reverse();  // easier to pop from the back
let mut pipeline = cmds.pop().unwrap() | cmds.pop.unwrap();
while let Some(cmd) = cmds.pop() {
    pipeline = pipeline | cmd;
}

@AlexanderThaller
Copy link
Contributor Author

Pipeline::from_iter() should check that the iterator produced at least two Execs. For example, Pipeline::popen() expects it, and asserts that self.cmds.len() >= 2.

Note that even without this PR it's possible to create pipelines with a dynamic number of components, it's just less convenient:

let cmds: Vec<Exec> = ...;
cmds.reverse();  // easier to pop from the back
let mut pipeline = cmds.pop().unwrap() | cmds.pop.unwrap();
while let Some(cmd) = cmds.pop() {
    pipeline = pipeline | cmd;
}

I added an assert like the one in Pipeline::popen(). Maybe it would be nicer to introduce a Result instead of panicing but that is probably outside of the scope of this pr.

@hniksic
Copy link
Owner

hniksic commented Sep 13, 2021

Thanks. If you don't mind, let's make it an explicit panic!() rather than assert(), which should be reserved for "impossible" conditions which indicate a bug in the crate. For example, the assert() in popen() should never happen simply because there is (prior to the introduction of from_iter()) no public API to create a Pipeline with less than two commands.

@AlexanderThaller
Copy link
Contributor Author

AlexanderThaller commented Sep 14, 2021

I changed the assert into a panic and also added a documentation example that panics.

@AlexanderThaller
Copy link
Contributor Author

AlexanderThaller commented Sep 14, 2021

Probably should also use a different name for the function as clippy complains: https://rust-lang.github.io/rust-clippy/master/index.html#should_implement_trait

Not sure if from_iter trait would be the right choice here as we have the length limitation.

The clippy lint:

warning: method `from_iter` can be confused for the standard trait method `std::iter::FromIterator::from_iter`
   --> src/builder.rs:834:9
    |
834 | /         pub fn from_iter<I>(iterator: I) -> Pipeline
835 | |         where
836 | |             I: Iterator<Item = Exec>,
837 | |         {
...   |
850 | |             }
851 | |         }
    | |_________^
    |
    = note: `#[warn(clippy::should_implement_trait)]` on by default
    = help: consider implementing the trait `std::iter::FromIterator` or choosing a less ambiguous method name
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#should_implement_trait

Maybe new_from_iter would be a better name.

@hniksic
Copy link
Owner

hniksic commented Sep 14, 2021

from_exec_iter might be a good choice for the name.

Also, it could take IntoIterator<Item = Exec>, so it can be called with actual Vec (and also with iterators, etc.).

@AlexanderThaller
Copy link
Contributor Author

from_exec_iter might be a good choice for the name.

Also, it could take IntoIterator<Item = Exec>, so it can be called with actual Vec (and also with iterators, etc.).

Renamed it to from_exec_iter and switchted to the IntoIterator trait.

@hniksic
Copy link
Owner

hniksic commented Sep 16, 2021

Looks great, thanks for the patience. I've additionally renamed the iterator parameter to iterable. If there're no further changes, I'll merge soon.

At some point it's worth removing the panic for less than two Execs - at least one should be reasonable, and possibly even zero. But that might come in a separate PR.

@AlexanderThaller
Copy link
Contributor Author

Thanks for helping with the pull request! I guess its ready to be merged :D

@hniksic hniksic merged commit 83fc9ad into hniksic:master Sep 16, 2021
@AlexanderThaller AlexanderThaller deleted the implement-pipeline-constructor-from-exec-vector branch September 27, 2021 11:19
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.

2 participants