Skip to content

Commit 0951810

Browse files
committed
vec dequeを実装
1 parent a472b7d commit 0951810

File tree

6 files changed

+84
-31
lines changed

6 files changed

+84
-31
lines changed

README.md

+22-3
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,26 @@ fn main() -> anyhow::Result<()> {
241241
Ok(())
242242
}
243243
```
244+
# Iterator
245+
### IntoIter
246+
```rust
247+
fn main() -> anyhow::Result<()> {
248+
let ne_vec = NonEmptyVec::new(vec![1, 2, 3])?;
249+
let ne_vec: NonEmptyVec<i32> = ne_vec.into_iter().map(|n| n * 2).map(|n| n * 3).collect();
250+
assert_eq!(ne_vec.into_value(), vec![6, 12, 18]);
251+
Ok(())
252+
}
253+
```
254+
255+
### Iter
256+
```rust
257+
fn main() -> anyhow::Result<()> {
258+
let ne_vec = NonEmptyVec::new(vec![1, 2, 3])?;
259+
let ne_vec: NonEmptyVec<i32> = ne_vec.iter().map(|n| n * 2).map(|n| n * 3).collect();
260+
assert_eq!(ne_vec.into_value(), vec![6, 12, 18]);
261+
Ok(())
262+
}
263+
```
244264

245265
# Add Trait
246266
I have implemented the `Add` trait for a part of the `Refined` that I provided. Therefore, operations can be performed without downgrading the type level.
@@ -258,8 +278,7 @@ fn main() -> anyhow::Result<()> {
258278

259279
### NonEmptyVec
260280
```rust
261-
#[test]
262-
fn test_add_vec() -> anyhow::Result<()> {
281+
fn main() -> anyhow::Result<()> {
263282
let ne_vec_1 = NonEmptyVec::new(vec![1, 2, 3])?;
264283
let ne_vec_2 = NonEmptyVec::new(vec![4, 5, 6])?;
265284
let ne_vec = ne_vec_1 + ne_vec_2; // This is also `NonEmptyVec` type
@@ -271,7 +290,7 @@ fn test_add_vec() -> anyhow::Result<()> {
271290

272291
### Empty
273292
```rust
274-
fn test_add_empty() -> anyhow::Result<()> {
293+
fn main() -> anyhow::Result<()> {
275294
let empty_1 = Empty::new(0)?;
276295
let empty_2 = Empty::new(0)?;
277296
let empty = empty_1 + empty_2; // This is also `Empty` type

src/rule/empty.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ use crate::result::Error;
22
use crate::rule::Rule;
33
use crate::Refined;
44

5-
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
5+
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
66
use std::fmt::Debug;
77
use std::iter::Map;
88
use std::marker::PhantomData;
99
use std::ops::Add;
10-
use std::vec::IntoIter;
1110

1211
pub type Empty<T> = Refined<EmptyRule<T>>;
1312

@@ -61,13 +60,31 @@ impl<T> EmptyDefinition for Vec<T> {
6160
}
6261
}
6362

63+
impl<T> EmptyDefinition for std::vec::IntoIter<T> {
64+
fn empty(&self) -> bool {
65+
self.len() == 0
66+
}
67+
}
68+
6469
impl<'a, T> EmptyDefinition for std::slice::Iter<'a, T> {
6570
fn empty(&self) -> bool {
6671
self.len() == 0
6772
}
6873
}
6974

70-
impl<T> EmptyDefinition for IntoIter<T> {
75+
impl<T> EmptyDefinition for VecDeque<T> {
76+
fn empty(&self) -> bool {
77+
self.is_empty()
78+
}
79+
}
80+
81+
impl<T> EmptyDefinition for std::collections::vec_deque::IntoIter<T> {
82+
fn empty(&self) -> bool {
83+
self.len() == 0
84+
}
85+
}
86+
87+
impl<'a, T> EmptyDefinition for std::collections::vec_deque::Iter<'a, T> {
7188
fn empty(&self) -> bool {
7289
self.len() == 0
7390
}
@@ -82,16 +99,6 @@ where
8299
}
83100
}
84101

85-
// impl<I, F> EmptyDefinition for std::iter::Map<I, F>
86-
// where
87-
// Self: Sized,
88-
// I: Iterator + EmptyDefinition,
89-
// {
90-
// fn empty(&self) -> bool {
91-
// true
92-
// }
93-
// }
94-
95102
impl<T> EmptyDefinition for HashSet<T> {
96103
fn empty(&self) -> bool {
97104
self.is_empty()

src/rule/non_empty.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
mod non_empty_iter;
21
mod non_empty_map;
32
mod non_empty_string;
43
mod non_empty_vec;
4+
mod non_empty_vec_deque;
55

66
use crate::rule::composer::Not;
77
use crate::rule::{EmptyDefinition, EmptyRule};
88
use crate::Refined;
99

10-
pub use non_empty_iter::*;
1110
pub use non_empty_map::*;
1211
pub use non_empty_string::*;
1312
pub use non_empty_vec::*;

src/rule/non_empty/non_empty_iter.rs

-10
This file was deleted.

src/rule/non_empty/non_empty_vec.rs

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

44
use std::ops::Add;
@@ -11,14 +11,14 @@ where
1111
T: EmptyDefinition,
1212
{
1313
#[allow(clippy::should_implement_trait)]
14-
pub fn into_iter(self) -> NonEmptyIntoIter<T> {
14+
pub fn into_iter(self) -> NonEmpty<std::vec::IntoIter<T>> {
1515
Refined::new(self.into_value().into_iter())
1616
.ok()
1717
.expect("This error is always unreachable")
1818
}
1919

2020
#[allow(clippy::should_implement_trait)]
21-
pub fn iter(&self) -> NonEmptyIter<T> {
21+
pub fn iter(&self) -> NonEmpty<std::slice::Iter<T>> {
2222
Refined::new(self.value().iter())
2323
.ok()
2424
.expect("This error is always unreachable")
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use crate::rule::{EmptyDefinition, NonEmpty, NonEmptyRule};
2+
use crate::Refined;
3+
use std::collections::VecDeque;
4+
use std::ops::Add;
5+
6+
pub type NonEmptyVecDeque<T> = Refined<NonEmptyVecDequeRule<T>>;
7+
pub type NonEmptyVecDequeRule<T> = NonEmptyRule<VecDeque<T>>;
8+
9+
impl<T> NonEmptyVecDeque<T>
10+
where
11+
T: EmptyDefinition,
12+
{
13+
#[allow(clippy::should_implement_trait)]
14+
pub fn into_iter(self) -> NonEmpty<std::collections::vec_deque::IntoIter<T>> {
15+
Refined::new(self.into_value().into_iter())
16+
.ok()
17+
.expect("This error is always unreachable")
18+
}
19+
20+
#[allow(clippy::should_implement_trait)]
21+
pub fn iter(&self) -> NonEmpty<std::collections::vec_deque::Iter<T>> {
22+
Refined::new(self.value().iter())
23+
.ok()
24+
.expect("This error is always unreachable")
25+
}
26+
}
27+
28+
impl<T> Add for NonEmptyVecDeque<T> {
29+
type Output = Self;
30+
31+
fn add(self, rhs: Self) -> Self::Output {
32+
let mut result = self.into_value();
33+
result.append(&mut rhs.into_value());
34+
Refined::new(result)
35+
.ok()
36+
.expect("This error is always unreachable")
37+
}
38+
}

0 commit comments

Comments
 (0)