Skip to content

Commit a472b7d

Browse files
committed
refactoring
1 parent 12f8ff6 commit a472b7d

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/rule/non_empty/non_empty_iter.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use crate::rule::NonEmptyRule;
22
use crate::Refined;
3+
use std::slice::Iter;
34
use std::vec::IntoIter;
45

56
pub type NonEmptyIntoIter<T> = Refined<NonEmptyIntoIterRule<T>>;
67
pub type NonEmptyIntoIterRule<T> = NonEmptyRule<IntoIter<T>>;
8+
9+
pub type NonEmptyIter<'a, T> = Refined<NonEmptyIterRule<'a, T>>;
10+
pub type NonEmptyIterRule<'a, T> = NonEmptyRule<Iter<'a, T>>;

src/rule/non_empty/non_empty_vec.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::rule::{EmptyDefinition, NonEmptyIntoIter, NonEmptyRule};
1+
use crate::rule::{EmptyDefinition, NonEmptyIntoIter, NonEmptyIter, NonEmptyRule};
22
use crate::Refined;
33

44
use std::ops::Add;
@@ -16,6 +16,13 @@ where
1616
.ok()
1717
.expect("This error is always unreachable")
1818
}
19+
20+
#[allow(clippy::should_implement_trait)]
21+
pub fn iter(&self) -> NonEmptyIter<T> {
22+
Refined::new(self.value().iter())
23+
.ok()
24+
.expect("This error is always unreachable")
25+
}
1926
}
2027

2128
impl<T> Add for NonEmptyVec<T> {
@@ -57,4 +64,12 @@ mod test {
5764
assert_eq!(ne_vec.into_value(), vec![6, 12, 18]);
5865
Ok(())
5966
}
67+
68+
#[test]
69+
fn test_iter() -> anyhow::Result<()> {
70+
let ne_vec = NonEmptyVec::new(vec![1, 2, 3])?;
71+
let ne_vec: NonEmptyVec<i32> = ne_vec.iter().map(|n| n * 2).map(|n| n * 3).collect();
72+
assert_eq!(ne_vec.into_value(), vec![6, 12, 18]);
73+
Ok(())
74+
}
6075
}

0 commit comments

Comments
 (0)