-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from ratmice/lending_refs
add `into_lending_refs` and `into_lending_refs_mut`
- Loading branch information
Showing
4 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use crate::LendingIterator; | ||
|
||
/// A lending iterator that given an iterator, lends | ||
/// references to the given iterator's items. | ||
pub struct LendRefs<I: Iterator> { | ||
item: Option<I::Item>, | ||
iter: I, | ||
} | ||
|
||
impl<I: Iterator> LendRefs<I> { | ||
pub(crate) fn new(iter: I) -> LendRefs<I> { | ||
LendRefs { item: None, iter } | ||
} | ||
} | ||
|
||
impl<I> LendingIterator for LendRefs<I> | ||
where | ||
I: Iterator, | ||
{ | ||
type Item<'a> = &'a I::Item where Self: 'a; | ||
|
||
fn next(&mut self) -> Option<Self::Item<'_>> { | ||
self.item = self.iter.next(); | ||
self.item.as_ref() | ||
} | ||
} | ||
#[cfg(test)] | ||
mod test { | ||
use crate::{LendingIterator, ToLendingIterator}; | ||
#[derive(Clone, Eq, PartialEq, Debug)] | ||
struct Foo(usize); | ||
struct W { | ||
x: Foo, | ||
} | ||
impl LendingIterator for W { | ||
type Item<'a> = &'a Foo where Self: 'a; | ||
fn next(&mut self) -> Option<Self::Item<'_>> { | ||
self.x.0 += 1; | ||
Some(&self.x) | ||
} | ||
} | ||
#[test] | ||
fn test() { | ||
let mut xs = Vec::new(); | ||
test_helper().take(3).for_each(|x: &Foo| { | ||
xs.push(x.clone()); | ||
}); | ||
assert_eq!(xs, vec![Foo(0), Foo(1), Foo(2)]); | ||
} | ||
|
||
fn test_helper() -> impl for<'a> LendingIterator<Item<'a> = &'a Foo> { | ||
let w = W { x: Foo(0) }; | ||
std::iter::once(Foo(0)).lend_refs().chain(w) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use crate::LendingIterator; | ||
|
||
/// A lending iterator that given an iterator, lends | ||
/// mutable references to the given iterator's items. | ||
pub struct LendRefsMut<I: Iterator> { | ||
item: Option<I::Item>, | ||
iter: I, | ||
} | ||
|
||
impl<I: Iterator> LendRefsMut<I> { | ||
pub(crate) fn new(iter: I) -> LendRefsMut<I> { | ||
LendRefsMut { item: None, iter } | ||
} | ||
} | ||
|
||
impl<I: Iterator> LendingIterator for LendRefsMut<I> { | ||
type Item<'a> = &'a mut I::Item where Self: 'a; | ||
|
||
fn next(&mut self) -> Option<Self::Item<'_>> { | ||
self.item = self.iter.next(); | ||
self.item.as_mut() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::{LendingIterator, ToLendingIterator}; | ||
#[derive(Clone, Eq, PartialEq, Debug)] | ||
struct Foo(usize); | ||
struct W { | ||
x: Foo, | ||
} | ||
|
||
impl LendingIterator for W { | ||
type Item<'a> = &'a mut Foo where Self: 'a; | ||
fn next(&mut self) -> Option<Self::Item<'_>> { | ||
self.x.0 += 1; | ||
Some(&mut self.x) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
let mut xs = Vec::new(); | ||
test_helper().take(3).for_each(|x: &mut Foo| { | ||
x.0 += 2; | ||
xs.push(x.clone()); | ||
}); | ||
assert_eq!(xs, vec![Foo(2), Foo(3), Foo(6)]); | ||
} | ||
|
||
fn test_helper() -> impl for<'a> LendingIterator<Item<'a> = &'a mut Foo> { | ||
let w = W { x: Foo(0) }; | ||
std::iter::once(Foo(0)).lend_refs_mut().chain(w) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
mod into_lending; | ||
mod lend_refs; | ||
mod lend_refs_mut; | ||
mod windows; | ||
mod windows_mut; | ||
pub use self::into_lending::IntoLending; | ||
pub use self::lend_refs::LendRefs; | ||
pub use self::lend_refs_mut::LendRefsMut; | ||
pub use self::windows::Windows; | ||
pub use self::windows_mut::WindowsMut; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters