From 0f7f6b8cd3077ac7d3c0a67a7ee5f82532b7143b Mon Sep 17 00:00:00 2001 From: waterlubber Date: Sat, 22 Oct 2022 14:53:09 -0400 Subject: [PATCH 1/3] Rewrite several prompts to take `IntoIterator` instead of `&[T]` This allows for much more flexibility when generating prompts. The user can pass in, for example, the output of a `.map` without having to collect it into a vector and provide a slice. Since all these functions do is run a simple for loop over the input, it's an easy change to make. Existing code should continue to work, as &[T] implements IntoIterator by default. --- src/prompts/fuzzy_select.rs | 6 +++++- src/prompts/multi_select.rs | 14 +++++++++++--- src/prompts/select.rs | 6 +++++- src/prompts/sort.rs | 6 +++++- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/prompts/fuzzy_select.rs b/src/prompts/fuzzy_select.rs index 37a1ee5d..aa95d21c 100644 --- a/src/prompts/fuzzy_select.rs +++ b/src/prompts/fuzzy_select.rs @@ -79,7 +79,11 @@ impl FuzzySelect<'_> { } /// Adds multiple items to the fuzzy selector. - pub fn items(&mut self, items: &[T]) -> &mut Self { + pub fn items(&mut self, items: T) -> &mut Self + where + T: IntoIterator, + T::Item: std::string::ToString, + { for item in items { self.items.push(item.to_string()); } diff --git a/src/prompts/multi_select.rs b/src/prompts/multi_select.rs index eed55a13..ea3de56a 100644 --- a/src/prompts/multi_select.rs +++ b/src/prompts/multi_select.rs @@ -91,7 +91,11 @@ impl MultiSelect<'_> { } /// Adds multiple items to the selector. - pub fn items(&mut self, items: &[T]) -> &mut Self { + pub fn items(&mut self, items: T) -> &mut Self + where + T: IntoIterator, + T::Item: std::string::ToString, + { for item in items { self.items.push(item.to_string()); self.defaults.push(false); @@ -100,8 +104,12 @@ impl MultiSelect<'_> { } /// Adds multiple items to the selector with checked state - pub fn items_checked(&mut self, items: &[(T, bool)]) -> &mut Self { - for &(ref item, checked) in items { + pub fn items_checked(&mut self, items: T) -> &mut Self + where + I: std::string::ToString, + T: IntoIterator, + { + for (item, checked) in items { self.items.push(item.to_string()); self.defaults.push(checked); } diff --git a/src/prompts/select.rs b/src/prompts/select.rs index 5e4335fc..2fb92b20 100644 --- a/src/prompts/select.rs +++ b/src/prompts/select.rs @@ -123,7 +123,11 @@ impl Select<'_> { /// Ok(()) /// } /// ``` - pub fn items(&mut self, items: &[T]) -> &mut Self { + pub fn items(&mut self, items: T) -> &mut Self + where + T: IntoIterator, + T::Item: std::string::ToString, + { for item in items { self.items.push(item.to_string()); } diff --git a/src/prompts/sort.rs b/src/prompts/sort.rs index 03152bf4..3a03f87b 100644 --- a/src/prompts/sort.rs +++ b/src/prompts/sort.rs @@ -74,7 +74,11 @@ impl Sort<'_> { } /// Adds multiple items to the selector. - pub fn items(&mut self, items: &[T]) -> &mut Self { + pub fn items(&mut self, items: T) -> &mut Self + where + T: IntoIterator, + T::Item: std::string::ToString, + { for item in items { self.items.push(item.to_string()); } From 54181b0c87755d3b2d32f75c8d18215fa6db1b98 Mon Sep 17 00:00:00 2001 From: waterlubber Date: Sat, 22 Oct 2022 15:05:17 -0400 Subject: [PATCH 2/3] Reorder some where clauses and remove unnecessary path qualifications. --- src/prompts/fuzzy_select.rs | 2 +- src/prompts/multi_select.rs | 4 ++-- src/prompts/select.rs | 4 ++-- src/prompts/sort.rs | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/prompts/fuzzy_select.rs b/src/prompts/fuzzy_select.rs index aa95d21c..297f02c9 100644 --- a/src/prompts/fuzzy_select.rs +++ b/src/prompts/fuzzy_select.rs @@ -81,8 +81,8 @@ impl FuzzySelect<'_> { /// Adds multiple items to the fuzzy selector. pub fn items(&mut self, items: T) -> &mut Self where + T::Item: ToString, T: IntoIterator, - T::Item: std::string::ToString, { for item in items { self.items.push(item.to_string()); diff --git a/src/prompts/multi_select.rs b/src/prompts/multi_select.rs index ea3de56a..9f2baf5a 100644 --- a/src/prompts/multi_select.rs +++ b/src/prompts/multi_select.rs @@ -93,8 +93,8 @@ impl MultiSelect<'_> { /// Adds multiple items to the selector. pub fn items(&mut self, items: T) -> &mut Self where + T::Item: ToString, T: IntoIterator, - T::Item: std::string::ToString, { for item in items { self.items.push(item.to_string()); @@ -106,7 +106,7 @@ impl MultiSelect<'_> { /// Adds multiple items to the selector with checked state pub fn items_checked(&mut self, items: T) -> &mut Self where - I: std::string::ToString, + I: ToString, T: IntoIterator, { for (item, checked) in items { diff --git a/src/prompts/select.rs b/src/prompts/select.rs index 2fb92b20..09327d6c 100644 --- a/src/prompts/select.rs +++ b/src/prompts/select.rs @@ -123,10 +123,10 @@ impl Select<'_> { /// Ok(()) /// } /// ``` - pub fn items(&mut self, items: T) -> &mut Self + pub fn items(&mut self, items: T) -> &mut Self where + T::Item: ToString, T: IntoIterator, - T::Item: std::string::ToString, { for item in items { self.items.push(item.to_string()); diff --git a/src/prompts/sort.rs b/src/prompts/sort.rs index 3a03f87b..39c54cfe 100644 --- a/src/prompts/sort.rs +++ b/src/prompts/sort.rs @@ -76,8 +76,8 @@ impl Sort<'_> { /// Adds multiple items to the selector. pub fn items(&mut self, items: T) -> &mut Self where + T::Item: ToString, T: IntoIterator, - T::Item: std::string::ToString, { for item in items { self.items.push(item.to_string()); From 2e280e6db877142e71895e7b97709f0de3030bee Mon Sep 17 00:00:00 2001 From: waterlubber Date: Sat, 22 Oct 2022 15:10:47 -0400 Subject: [PATCH 3/3] Reorder IntoIterator and ToString on where clauses again By mistake I reordered every clause except the required one. This should fix that. --- src/prompts/fuzzy_select.rs | 2 +- src/prompts/multi_select.rs | 4 ++-- src/prompts/select.rs | 2 +- src/prompts/sort.rs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/prompts/fuzzy_select.rs b/src/prompts/fuzzy_select.rs index 297f02c9..a35c4929 100644 --- a/src/prompts/fuzzy_select.rs +++ b/src/prompts/fuzzy_select.rs @@ -81,8 +81,8 @@ impl FuzzySelect<'_> { /// Adds multiple items to the fuzzy selector. pub fn items(&mut self, items: T) -> &mut Self where - T::Item: ToString, T: IntoIterator, + T::Item: ToString, { for item in items { self.items.push(item.to_string()); diff --git a/src/prompts/multi_select.rs b/src/prompts/multi_select.rs index 9f2baf5a..8f426a2a 100644 --- a/src/prompts/multi_select.rs +++ b/src/prompts/multi_select.rs @@ -93,8 +93,8 @@ impl MultiSelect<'_> { /// Adds multiple items to the selector. pub fn items(&mut self, items: T) -> &mut Self where - T::Item: ToString, T: IntoIterator, + T::Item: ToString, { for item in items { self.items.push(item.to_string()); @@ -106,8 +106,8 @@ impl MultiSelect<'_> { /// Adds multiple items to the selector with checked state pub fn items_checked(&mut self, items: T) -> &mut Self where - I: ToString, T: IntoIterator, + I: ToString, { for (item, checked) in items { self.items.push(item.to_string()); diff --git a/src/prompts/select.rs b/src/prompts/select.rs index 09327d6c..2f48adf8 100644 --- a/src/prompts/select.rs +++ b/src/prompts/select.rs @@ -125,8 +125,8 @@ impl Select<'_> { /// ``` pub fn items(&mut self, items: T) -> &mut Self where - T::Item: ToString, T: IntoIterator, + T::Item: ToString, { for item in items { self.items.push(item.to_string()); diff --git a/src/prompts/sort.rs b/src/prompts/sort.rs index 39c54cfe..49f8db76 100644 --- a/src/prompts/sort.rs +++ b/src/prompts/sort.rs @@ -76,8 +76,8 @@ impl Sort<'_> { /// Adds multiple items to the selector. pub fn items(&mut self, items: T) -> &mut Self where - T::Item: ToString, T: IntoIterator, + T::Item: ToString, { for item in items { self.items.push(item.to_string());