Skip to content

make Option::cloned generic over Deref, make cloned's tests actually run, add Cloned iterator adaptor #19060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ use cmp;
use cmp::Ord;
use mem;
use num::{ToPrimitive, Int};
use ops::Add;
use ops::{Add, Deref};
use option::{Option, Some, None};
use uint;

#[deprecated = "renamed to Extend"] pub use self::Extend as Extendable;

/// Conversion from an `Iterator`
Expand Down Expand Up @@ -1021,6 +1022,44 @@ impl<T: Clone> MinMaxResult<T> {
}
}

/// A trait for iterators that contain cloneable elements
pub trait CloneIteratorExt<A> {
/// Creates an iterator that clones the elements it yields. Useful for converting an
/// Iterator<&T> to an Iterator<T>.
fn cloned(self) -> Cloned<Self>;
}


impl<A: Clone, D: Deref<A>, I: Iterator<D>> CloneIteratorExt<A> for I {
fn cloned(self) -> Cloned<I> {
Cloned { it: self }
}
}

/// An iterator that clones the elements of an underlying iterator
pub struct Cloned<I> {
it: I,
}

impl<A: Clone, D: Deref<A>, I: Iterator<D>> Iterator<A> for Cloned<I> {
fn next(&mut self) -> Option<A> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add #[inline] to this and other one-line functions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's generic, so it's already a candidate for cross-crate inlining. Otherwise I'd rather avoid adding more #[inline]s to std without any performance information.

self.it.next().cloned()
}

fn size_hint(&self) -> (uint, Option<uint>) {
self.it.size_hint()
}
}

impl<A: Clone, D: Deref<A>, I: DoubleEndedIterator<D>>
DoubleEndedIterator<A> for Cloned<I> {
fn next_back(&mut self) -> Option<A> {
self.it.next_back().cloned()
}
}

impl<A: Clone, D: Deref<A>, I: ExactSize<D>> ExactSize<A> for Cloned<I> {}

/// A trait for iterators that are cloneable.
pub trait CloneableIterator {
/// Repeats an iterator endlessly
Expand Down
8 changes: 5 additions & 3 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ use result::{Result, Ok, Err};
use slice;
use slice::AsSlice;
use clone::Clone;
use ops::Deref;

// Note that this is not a lang item per se, but it has a hidden dependency on
// `Iterator`, which is one. The compiler assumes that the `next` method of
Expand Down Expand Up @@ -694,11 +695,12 @@ impl<T> Option<T> {
}
}

impl<'a, T: Clone> Option<&'a T> {
/// Maps an Option<&T> to an Option<T> by cloning the contents of the Option<&T>.
impl<'a, T: Clone, D: Deref<T>> Option<D> {
/// Maps an Option<D> to an Option<T> by dereffing and cloning the contents of the Option.
/// Useful for converting an Option<&T> to an Option<T>.
#[unstable = "recently added as part of collections reform"]
pub fn cloned(self) -> Option<T> {
self.map(|t| t.clone())
self.map(|t| t.deref().clone())
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/libcoretest/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,23 @@ fn test_rev() {
vec![16, 14, 12, 10, 8, 6]);
}

#[test]
fn test_cloned() {
let xs = [2u8, 4, 6, 8];

let mut it = xs.iter().cloned();
assert_eq!(it.len(), 4);
assert_eq!(it.next(), Some(2));
assert_eq!(it.len(), 3);
assert_eq!(it.next(), Some(4));
assert_eq!(it.len(), 2);
assert_eq!(it.next_back(), Some(8));
assert_eq!(it.len(), 1);
assert_eq!(it.next_back(), Some(6));
assert_eq!(it.len(), 0);
assert_eq!(it.next_back(), None);
}

#[test]
fn test_double_ended_map() {
let xs = [1i, 2, 3, 4, 5, 6];
Expand Down
33 changes: 24 additions & 9 deletions src/libcoretest/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,29 @@ fn test_collect() {
assert!(v == None);
}

#[test]
fn test_cloned() {
let s = 1u32;
let n: Option<&'static u32> = None;
let o = Some(&s);

assert_eq!(o.clone(), Some(&s));
assert_eq!(o.cloned(), Some(1u32));

assert_eq!(n.clone(), None);
assert_eq!(n.cloned(), None);
let val1 = 1u32;
let mut val2 = 2u32;
let val1_ref = &val1;
let opt_none: Option<&'static u32> = None;
let opt_ref = Some(&val1);
let opt_ref_ref = Some(&val1_ref);
let opt_mut_ref = Some(&mut val2);

// None works
assert_eq!(opt_none.clone(), None);
assert_eq!(opt_none.cloned(), None);

// Mutable refs work
assert_eq!(opt_mut_ref.cloned(), Some(2u32));

// Immutable ref works
assert_eq!(opt_ref.clone(), Some(&val1));
assert_eq!(opt_ref.cloned(), Some(1u32));

// Double Immutable ref works
assert_eq!(opt_ref_ref.clone(), Some(&val1_ref));
assert_eq!(opt_ref_ref.clone().cloned(), Some(&val1));
assert_eq!(opt_ref_ref.cloned().cloned(), Some(1u32));
}