1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//! Provider of [`RefIterator`].
use crate::closures::FnIFlatMap;
use crate::prelude::*;
use crate::sub::{ICloned, IFlatMap, IMap};
use crate::util::msg;
use core::ops::DerefMut;
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
/// Immutable dynamic borrowing iterator.
#[must_use = msg::iter_must_use!()]
pub trait RefIterator: RefIteratorBase {
/// The type of the elements being iterated over.
type Item: ?Sized;
/// Advances the iterator and returns the next value.
///
/// # Examples
///
/// ```
/// # use ref_iter::prelude::*;
/// # use std::cell::RefCell;
/// #
/// let samples = vec![1, 2];
/// let src = RefCell::new(samples.clone());
/// let mut iter = RefIter::new(src.borrow(), |x| x.iter());
/// assert_eq!(iter.next(), Some(&1));
/// assert_eq!(iter.next(), Some(&2));
/// assert_eq!(iter.next(), None);
/// ```
fn next(&mut self) -> Option<&Self::Item>;
/// Creates an iterator that clone dynamic borrowing elements.
///
/// # Examples
///
/// ```
/// # use ref_iter::prelude::*;
/// # use std::cell::RefCell;
/// #
/// let samples = vec![1, 2, 3];
/// let src = RefCell::new(samples.clone());
/// let iter = RefIter::new(src.borrow(), |x| x.iter());
/// assert!(iter.icloned().eq(samples.iter().cloned()));
/// ```
fn icloned(self) -> ICloned<Self>
where
Self: Sized,
Self::Item: Clone,
{
ICloned::new(self)
}
/// Creates an iterator that flattens mapped each dynamic borrowing iterators.
///
/// # Examples
///
/// ```
/// # use ref_iter::prelude::*;
/// # use std::cell::RefCell;
/// #
/// let samples = vec![vec![1, 2], vec![3, 4, 5]];
/// let expecteds = vec![1, 2, 3, 4, 5];
/// let src = RefCell::new(samples.clone());
/// let iter = RefIter::new(src.borrow(), |x| x.iter());
/// let iter = iter.iflat_map(vec_iter);
/// assert!(iter.eq(expecteds.iter().cloned()));
///
/// fn vec_iter(x: &Vec<i32>) -> impl Iterator<Item = i32> + '_ {
/// x.iter().cloned()
/// }
/// ```
fn iflat_map<'call, F>(self, f: F) -> IFlatMap<'call, Self, F>
where
Self: Sized,
F: for<'a> FnIFlatMap<&'a Self::Item>,
{
IFlatMap::new(self, f)
}
/// Creates an iterator that maps dynamic borrowing elements.
///
/// # Examples
///
/// ```
/// # use ref_iter::prelude::*;
/// # use std::cell::RefCell;
/// #
/// let samples = vec![1, 2, 3];
/// let src = RefCell::new(samples.clone());
/// let iter = RefIter::new(src.borrow(), |x| x.iter());
/// let iter = iter.imap(|x: &_| x + 1);
/// assert!(iter.eq(samples.iter().map(|x| x + 1)));
/// ```
fn imap<B, F>(self, f: F) -> IMap<Self, F>
where
Self: Sized,
F: FnMut(&Self::Item) -> B,
{
IMap::new(self, f)
}
/// Determines if the elements of this is equal to those of another.
///
/// # Examples
///
/// ```
/// # use ref_iter::prelude::*;
/// # use std::cell::RefCell;
/// #
/// let samples = vec![1, 2, 3];
/// let src = RefCell::new(samples.clone());
/// let iter1 = RefIter::new(src.borrow(), |x| x.iter());
/// let iter2 = RefIter::new(src.borrow(), |x| x.iter());
/// assert!(iter1.eq(iter2));
/// ```
fn eq<I>(mut self, other: I) -> bool
where
Self: Sized,
Self::Item: PartialEq<I::Item>,
I: IntoRefIterator,
{
let mut iter = other.into_ref_iter();
loop {
let x = self.next();
let y = iter.next();
match (x, y) {
(None, None) => return true,
(None, Some(_)) => return false,
(Some(_), None) => return false,
(Some(x), Some(y)) => {
if x != y {
return false;
}
}
}
}
}
}
impl<I> RefIterator for Box<I>
where
I: RefIterator + ?Sized,
{
type Item = I::Item;
fn next(&mut self) -> Option<&Self::Item> {
self.deref_mut().next()
}
}