Skip to content

Commit 97a840f

Browse files
authored
Merge pull request #299 from jacobtread-contrib/master
Accept items by iterator instead of slice
2 parents 3186228 + c834f0e commit 97a840f

File tree

4 files changed

+71
-25
lines changed

4 files changed

+71
-25
lines changed

src/prompts/fuzzy_select.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,14 @@ impl FuzzySelect<'_> {
8181
}
8282

8383
/// Adds multiple items to the fuzzy selector.
84-
pub fn items<T: ToString>(mut self, items: &[T]) -> Self {
85-
for item in items {
86-
self.items.push(item.to_string());
87-
}
84+
pub fn items<T, I>(mut self, items: I) -> Self
85+
where
86+
T: ToString,
87+
I: IntoIterator<Item = T>,
88+
{
89+
self.items
90+
.extend(items.into_iter().map(|item| item.to_string()));
91+
8892
self
8993
}
9094

@@ -404,4 +408,12 @@ mod tests {
404408

405409
let _ = fuzzy_select.clone();
406410
}
411+
412+
#[test]
413+
fn test_iterator() {
414+
let items = ["First", "Second", "Third"];
415+
let iterator = items.iter().skip(1);
416+
417+
assert_eq!(FuzzySelect::new().items(iterator).items, &items[1..]);
418+
}
407419
}

src/prompts/multi_select.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,21 @@ impl MultiSelect<'_> {
101101
}
102102

103103
/// Adds multiple items to the selector.
104-
pub fn items<T: ToString>(mut self, items: &[T]) -> Self {
105-
for item in items {
106-
self.items.push(item.to_string());
107-
self.defaults.push(false);
108-
}
109-
self
104+
pub fn items<T, I>(self, items: I) -> Self
105+
where
106+
T: ToString,
107+
I: IntoIterator<Item = T>,
108+
{
109+
self.items_checked(items.into_iter().map(|item| (item, false)))
110110
}
111111

112112
/// Adds multiple items to the selector with checked state
113-
pub fn items_checked<T: ToString>(mut self, items: &[(T, bool)]) -> Self {
114-
for &(ref item, checked) in items {
113+
pub fn items_checked<T, I>(mut self, items: I) -> Self
114+
where
115+
T: ToString,
116+
I: IntoIterator<Item = (T, bool)>,
117+
{
118+
for (item, checked) in items.into_iter() {
115119
self.items.push(item.to_string());
116120
self.defaults.push(checked);
117121
}
@@ -382,4 +386,12 @@ mod tests {
382386

383387
let _ = multi_select.clone();
384388
}
389+
390+
#[test]
391+
fn test_iterator() {
392+
let items = ["First", "Second", "Third"];
393+
let iterator = items.iter().skip(1);
394+
395+
assert_eq!(MultiSelect::new().items(iterator).items, &items[1..]);
396+
}
385397
}

src/prompts/select.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,19 @@ impl Select<'_> {
9999
/// ```
100100
pub fn item<T: ToString>(mut self, item: T) -> Self {
101101
self.items.push(item.to_string());
102+
102103
self
103104
}
104105

105106
/// Adds multiple items to the selector.
106-
pub fn items<T: ToString>(mut self, items: &[T]) -> Self {
107-
for item in items {
108-
self.items.push(item.to_string());
109-
}
107+
pub fn items<T, I>(mut self, items: I) -> Self
108+
where
109+
T: ToString,
110+
I: IntoIterator<Item = T>,
111+
{
112+
self.items
113+
.extend(items.into_iter().map(|item| item.to_string()));
114+
110115
self
111116
}
112117

@@ -363,7 +368,7 @@ mod tests {
363368
let selections = vec!["a".to_string(), "b".to_string()];
364369

365370
assert_eq!(
366-
Select::new().default(0).items(&selections[..]).items,
371+
Select::new().default(0).items(&selections).items,
367372
selections
368373
);
369374
}
@@ -375,9 +380,14 @@ mod tests {
375380

376381
let selections = &[a, b];
377382

378-
assert_eq!(
379-
Select::new().default(0).items(&selections[..]).items,
380-
selections
381-
);
383+
assert_eq!(Select::new().default(0).items(selections).items, selections);
384+
}
385+
386+
#[test]
387+
fn test_iterator() {
388+
let items = ["First", "Second", "Third"];
389+
let iterator = items.iter().skip(1);
390+
391+
assert_eq!(Select::new().default(0).items(iterator).items, &items[1..]);
382392
}
383393
}

src/prompts/sort.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,14 @@ impl Sort<'_> {
8383
}
8484

8585
/// Adds multiple items to the selector.
86-
pub fn items<T: ToString>(mut self, items: &[T]) -> Self {
87-
for item in items {
88-
self.items.push(item.to_string());
89-
}
86+
pub fn items<T, I>(mut self, items: I) -> Self
87+
where
88+
T: ToString,
89+
I: IntoIterator<Item = T>,
90+
{
91+
self.items
92+
.extend(items.into_iter().map(|item| item.to_string()));
93+
9094
self
9195
}
9296

@@ -373,4 +377,12 @@ mod tests {
373377

374378
let _ = sort.clone();
375379
}
380+
381+
#[test]
382+
fn test_iterator() {
383+
let items = ["First", "Second", "Third"];
384+
let iterator = items.iter().skip(1);
385+
386+
assert_eq!(Sort::new().items(iterator).items, &items[1..]);
387+
}
376388
}

0 commit comments

Comments
 (0)