Skip to content

Commit 499b9ed

Browse files
committed
Move the iter methods back to the root for doc order
1 parent 2a37eba commit 499b9ed

File tree

2 files changed

+168
-170
lines changed

2 files changed

+168
-170
lines changed

src/iterator.rs

+6-167
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
use super::{for_both, Either, Left, Right};
22
use core::iter;
33

4-
macro_rules! map_either {
5-
($value:expr, $pattern:pat => $result:expr) => {
6-
match $value {
7-
Left($pattern) => Left($result),
8-
Right($pattern) => Right($result),
9-
}
10-
};
11-
}
12-
134
macro_rules! wrap_either {
145
($value:expr => $( $tail:tt )*) => {
156
match $value {
@@ -19,164 +10,6 @@ macro_rules! wrap_either {
1910
};
2011
}
2112

22-
impl<L, R> Either<L, R> {
23-
/// Convert the inner value to an iterator.
24-
///
25-
/// This requires the `Left` and `Right` iterators to have the same item type.
26-
/// See [`factor_into_iter`][Either::factor_into_iter] to iterate different types.
27-
///
28-
/// ```
29-
/// use either::*;
30-
///
31-
/// let left: Either<_, Vec<u32>> = Left(vec![1, 2, 3, 4, 5]);
32-
/// let mut right: Either<Vec<u32>, _> = Right(vec![]);
33-
/// right.extend(left.into_iter());
34-
/// assert_eq!(right, Right(vec![1, 2, 3, 4, 5]));
35-
/// ```
36-
#[allow(clippy::should_implement_trait)]
37-
pub fn into_iter(self) -> Either<L::IntoIter, R::IntoIter>
38-
where
39-
L: IntoIterator,
40-
R: IntoIterator<Item = L::Item>,
41-
{
42-
map_either!(self, inner => inner.into_iter())
43-
}
44-
45-
/// Borrow the inner value as an iterator.
46-
///
47-
/// This requires the `Left` and `Right` iterators to have the same item type.
48-
/// See [`factor_iter`][Either::factor_iter] to iterate different types.
49-
///
50-
/// ```
51-
/// use either::*;
52-
///
53-
/// let left: Either<_, &[u32]> = Left(vec![2, 3]);
54-
/// let mut right: Either<Vec<u32>, _> = Right(&[4, 5][..]);
55-
/// let mut all = vec![1];
56-
/// all.extend(left.iter());
57-
/// all.extend(right.iter());
58-
/// assert_eq!(all, vec![1, 2, 3, 4, 5]);
59-
/// ```
60-
pub fn iter(&self) -> Either<<&L as IntoIterator>::IntoIter, <&R as IntoIterator>::IntoIter>
61-
where
62-
for<'a> &'a L: IntoIterator,
63-
for<'a> &'a R: IntoIterator<Item = <&'a L as IntoIterator>::Item>,
64-
{
65-
map_either!(self, inner => inner.into_iter())
66-
}
67-
68-
/// Mutably borrow the inner value as an iterator.
69-
///
70-
/// This requires the `Left` and `Right` iterators to have the same item type.
71-
/// See [`factor_iter_mut`][Either::factor_iter_mut] to iterate different types.
72-
///
73-
/// ```
74-
/// use either::*;
75-
///
76-
/// let mut left: Either<_, &mut [u32]> = Left(vec![2, 3]);
77-
/// for l in left.iter_mut() {
78-
/// *l *= *l
79-
/// }
80-
/// assert_eq!(left, Left(vec![4, 9]));
81-
///
82-
/// let mut inner = [4, 5];
83-
/// let mut right: Either<Vec<u32>, _> = Right(&mut inner[..]);
84-
/// for r in right.iter_mut() {
85-
/// *r *= *r
86-
/// }
87-
/// assert_eq!(inner, [16, 25]);
88-
/// ```
89-
pub fn iter_mut(
90-
&mut self,
91-
) -> Either<<&mut L as IntoIterator>::IntoIter, <&mut R as IntoIterator>::IntoIter>
92-
where
93-
for<'a> &'a mut L: IntoIterator,
94-
for<'a> &'a mut R: IntoIterator<Item = <&'a mut L as IntoIterator>::Item>,
95-
{
96-
map_either!(self, inner => inner.into_iter())
97-
}
98-
99-
/// Converts an `Either` of `Iterator`s to be an `Iterator` of `Either`s
100-
///
101-
/// Unlike [`into_iter`][Either::into_iter], this does not require the
102-
/// `Left` and `Right` iterators to have the same item type.
103-
///
104-
/// ```
105-
/// use either::*;
106-
/// let left: Either<_, Vec<u8>> = Left(&["hello"]);
107-
/// assert_eq!(left.factor_into_iter().next(), Some(Left(&"hello")));
108-
109-
/// let right: Either<&[&str], _> = Right(vec![0, 1]);
110-
/// assert_eq!(right.factor_into_iter().collect::<Vec<_>>(), vec![Right(0), Right(1)]);
111-
///
112-
/// ```
113-
// TODO(MSRV): doc(alias) was stabilized in Rust 1.48
114-
// #[doc(alias = "transpose")]
115-
pub fn factor_into_iter(self) -> IterEither<L::IntoIter, R::IntoIter>
116-
where
117-
L: IntoIterator,
118-
R: IntoIterator,
119-
{
120-
IterEither {
121-
inner: map_either!(self, inner => inner.into_iter()),
122-
}
123-
}
124-
125-
/// Borrows an `Either` of `Iterator`s to be an `Iterator` of `Either`s
126-
///
127-
/// Unlike [`iter`][Either::iter], this does not require the
128-
/// `Left` and `Right` iterators to have the same item type.
129-
///
130-
/// ```
131-
/// use either::*;
132-
/// let left: Either<_, Vec<u8>> = Left(["hello"]);
133-
/// assert_eq!(left.factor_iter().next(), Some(Left(&"hello")));
134-
135-
/// let right: Either<[&str; 2], _> = Right(vec![0, 1]);
136-
/// assert_eq!(right.factor_iter().collect::<Vec<_>>(), vec![Right(&0), Right(&1)]);
137-
///
138-
/// ```
139-
pub fn factor_iter(
140-
&self,
141-
) -> IterEither<<&L as IntoIterator>::IntoIter, <&R as IntoIterator>::IntoIter>
142-
where
143-
for<'a> &'a L: IntoIterator,
144-
for<'a> &'a R: IntoIterator,
145-
{
146-
IterEither {
147-
inner: map_either!(self, inner => inner.into_iter()),
148-
}
149-
}
150-
151-
/// Mutably borrows an `Either` of `Iterator`s to be an `Iterator` of `Either`s
152-
///
153-
/// Unlike [`iter_mut`][Either::iter_mut], this does not require the
154-
/// `Left` and `Right` iterators to have the same item type.
155-
///
156-
/// ```
157-
/// use either::*;
158-
/// let mut left: Either<_, Vec<u8>> = Left(["hello"]);
159-
/// left.factor_iter_mut().for_each(|x| *x.unwrap_left() = "goodbye");
160-
/// assert_eq!(left, Left(["goodbye"]));
161-
162-
/// let mut right: Either<[&str; 2], _> = Right(vec![0, 1, 2]);
163-
/// right.factor_iter_mut().for_each(|x| if let Right(r) = x { *r = -*r; });
164-
/// assert_eq!(right, Right(vec![0, -1, -2]));
165-
///
166-
/// ```
167-
pub fn factor_iter_mut(
168-
&mut self,
169-
) -> IterEither<<&mut L as IntoIterator>::IntoIter, <&mut R as IntoIterator>::IntoIter>
170-
where
171-
for<'a> &'a mut L: IntoIterator,
172-
for<'a> &'a mut R: IntoIterator,
173-
{
174-
IterEither {
175-
inner: map_either!(self, inner => inner.into_iter()),
176-
}
177-
}
178-
}
179-
18013
/// Iterator that maps left or right iterators to corresponding `Either`-wrapped items.
18114
///
18215
/// This struct is created by the [`Either::factor_into_iter`],
@@ -187,6 +20,12 @@ pub struct IterEither<L, R> {
18720
inner: Either<L, R>,
18821
}
18922

23+
impl<L, R> IterEither<L, R> {
24+
pub(crate) fn new(inner: Either<L, R>) -> Self {
25+
IterEither { inner }
26+
}
27+
}
28+
19029
impl<L, R, A> Extend<A> for Either<L, R>
19130
where
19231
L: Extend<A>,

src/lib.rs

+162-3
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ macro_rules! try_right {
130130
};
131131
}
132132

133+
macro_rules! map_either {
134+
($value:expr, $pattern:pat => $result:expr) => {
135+
match $value {
136+
Left($pattern) => Left($result),
137+
Right($pattern) => Right($result),
138+
}
139+
};
140+
}
141+
142+
mod iterator;
143+
pub use self::iterator::IterEither;
144+
133145
impl<L: Clone, R: Clone> Clone for Either<L, R> {
134146
fn clone(&self) -> Self {
135147
match self {
@@ -505,6 +517,156 @@ impl<L, R> Either<L, R> {
505517
}
506518
}
507519

520+
/// Convert the inner value to an iterator.
521+
///
522+
/// This requires the `Left` and `Right` iterators to have the same item type.
523+
/// See [`factor_into_iter`][Either::factor_into_iter] to iterate different types.
524+
///
525+
/// ```
526+
/// use either::*;
527+
///
528+
/// let left: Either<_, Vec<u32>> = Left(vec![1, 2, 3, 4, 5]);
529+
/// let mut right: Either<Vec<u32>, _> = Right(vec![]);
530+
/// right.extend(left.into_iter());
531+
/// assert_eq!(right, Right(vec![1, 2, 3, 4, 5]));
532+
/// ```
533+
#[allow(clippy::should_implement_trait)]
534+
pub fn into_iter(self) -> Either<L::IntoIter, R::IntoIter>
535+
where
536+
L: IntoIterator,
537+
R: IntoIterator<Item = L::Item>,
538+
{
539+
map_either!(self, inner => inner.into_iter())
540+
}
541+
542+
/// Borrow the inner value as an iterator.
543+
///
544+
/// This requires the `Left` and `Right` iterators to have the same item type.
545+
/// See [`factor_iter`][Either::factor_iter] to iterate different types.
546+
///
547+
/// ```
548+
/// use either::*;
549+
///
550+
/// let left: Either<_, &[u32]> = Left(vec![2, 3]);
551+
/// let mut right: Either<Vec<u32>, _> = Right(&[4, 5][..]);
552+
/// let mut all = vec![1];
553+
/// all.extend(left.iter());
554+
/// all.extend(right.iter());
555+
/// assert_eq!(all, vec![1, 2, 3, 4, 5]);
556+
/// ```
557+
pub fn iter(&self) -> Either<<&L as IntoIterator>::IntoIter, <&R as IntoIterator>::IntoIter>
558+
where
559+
for<'a> &'a L: IntoIterator,
560+
for<'a> &'a R: IntoIterator<Item = <&'a L as IntoIterator>::Item>,
561+
{
562+
map_either!(self, inner => inner.into_iter())
563+
}
564+
565+
/// Mutably borrow the inner value as an iterator.
566+
///
567+
/// This requires the `Left` and `Right` iterators to have the same item type.
568+
/// See [`factor_iter_mut`][Either::factor_iter_mut] to iterate different types.
569+
///
570+
/// ```
571+
/// use either::*;
572+
///
573+
/// let mut left: Either<_, &mut [u32]> = Left(vec![2, 3]);
574+
/// for l in left.iter_mut() {
575+
/// *l *= *l
576+
/// }
577+
/// assert_eq!(left, Left(vec![4, 9]));
578+
///
579+
/// let mut inner = [4, 5];
580+
/// let mut right: Either<Vec<u32>, _> = Right(&mut inner[..]);
581+
/// for r in right.iter_mut() {
582+
/// *r *= *r
583+
/// }
584+
/// assert_eq!(inner, [16, 25]);
585+
/// ```
586+
pub fn iter_mut(
587+
&mut self,
588+
) -> Either<<&mut L as IntoIterator>::IntoIter, <&mut R as IntoIterator>::IntoIter>
589+
where
590+
for<'a> &'a mut L: IntoIterator,
591+
for<'a> &'a mut R: IntoIterator<Item = <&'a mut L as IntoIterator>::Item>,
592+
{
593+
map_either!(self, inner => inner.into_iter())
594+
}
595+
596+
/// Converts an `Either` of `Iterator`s to be an `Iterator` of `Either`s
597+
///
598+
/// Unlike [`into_iter`][Either::into_iter], this does not require the
599+
/// `Left` and `Right` iterators to have the same item type.
600+
///
601+
/// ```
602+
/// use either::*;
603+
/// let left: Either<_, Vec<u8>> = Left(&["hello"]);
604+
/// assert_eq!(left.factor_into_iter().next(), Some(Left(&"hello")));
605+
606+
/// let right: Either<&[&str], _> = Right(vec![0, 1]);
607+
/// assert_eq!(right.factor_into_iter().collect::<Vec<_>>(), vec![Right(0), Right(1)]);
608+
///
609+
/// ```
610+
// TODO(MSRV): doc(alias) was stabilized in Rust 1.48
611+
// #[doc(alias = "transpose")]
612+
pub fn factor_into_iter(self) -> IterEither<L::IntoIter, R::IntoIter>
613+
where
614+
L: IntoIterator,
615+
R: IntoIterator,
616+
{
617+
IterEither::new(map_either!(self, inner => inner.into_iter()))
618+
}
619+
620+
/// Borrows an `Either` of `Iterator`s to be an `Iterator` of `Either`s
621+
///
622+
/// Unlike [`iter`][Either::iter], this does not require the
623+
/// `Left` and `Right` iterators to have the same item type.
624+
///
625+
/// ```
626+
/// use either::*;
627+
/// let left: Either<_, Vec<u8>> = Left(["hello"]);
628+
/// assert_eq!(left.factor_iter().next(), Some(Left(&"hello")));
629+
630+
/// let right: Either<[&str; 2], _> = Right(vec![0, 1]);
631+
/// assert_eq!(right.factor_iter().collect::<Vec<_>>(), vec![Right(&0), Right(&1)]);
632+
///
633+
/// ```
634+
pub fn factor_iter(
635+
&self,
636+
) -> IterEither<<&L as IntoIterator>::IntoIter, <&R as IntoIterator>::IntoIter>
637+
where
638+
for<'a> &'a L: IntoIterator,
639+
for<'a> &'a R: IntoIterator,
640+
{
641+
IterEither::new(map_either!(self, inner => inner.into_iter()))
642+
}
643+
644+
/// Mutably borrows an `Either` of `Iterator`s to be an `Iterator` of `Either`s
645+
///
646+
/// Unlike [`iter_mut`][Either::iter_mut], this does not require the
647+
/// `Left` and `Right` iterators to have the same item type.
648+
///
649+
/// ```
650+
/// use either::*;
651+
/// let mut left: Either<_, Vec<u8>> = Left(["hello"]);
652+
/// left.factor_iter_mut().for_each(|x| *x.unwrap_left() = "goodbye");
653+
/// assert_eq!(left, Left(["goodbye"]));
654+
655+
/// let mut right: Either<[&str; 2], _> = Right(vec![0, 1, 2]);
656+
/// right.factor_iter_mut().for_each(|x| if let Right(r) = x { *r = -*r; });
657+
/// assert_eq!(right, Right(vec![0, -1, -2]));
658+
///
659+
/// ```
660+
pub fn factor_iter_mut(
661+
&mut self,
662+
) -> IterEither<<&mut L as IntoIterator>::IntoIter, <&mut R as IntoIterator>::IntoIter>
663+
where
664+
for<'a> &'a mut L: IntoIterator,
665+
for<'a> &'a mut R: IntoIterator,
666+
{
667+
IterEither::new(map_either!(self, inner => inner.into_iter()))
668+
}
669+
508670
/// Return left value or given value
509671
///
510672
/// Arguments passed to `left_or` are eagerly evaluated; if you are passing
@@ -928,9 +1090,6 @@ impl<T> Either<T, T> {
9281090
}
9291091
}
9301092

931-
mod iterator;
932-
pub use iterator::IterEither;
933-
9341093
/// Convert from `Result` to `Either` with `Ok => Right` and `Err => Left`.
9351094
impl<L, R> From<Result<R, L>> for Either<L, R> {
9361095
fn from(r: Result<R, L>) -> Self {

0 commit comments

Comments
 (0)