2014-01-11 04:13:06 -06:00
|
|
|
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-04-09 09:54:32 -05:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
//! Composable external iterators
|
|
|
|
//!
|
|
|
|
//! # The `Iterator` trait
|
|
|
|
//!
|
|
|
|
//! This module defines Rust's core iteration trait. The `Iterator` trait has one
|
|
|
|
//! unimplemented method, `next`. All other methods are derived through default
|
|
|
|
//! methods to perform operations such as `zip`, `chain`, `enumerate`, and `fold`.
|
|
|
|
//!
|
|
|
|
//! The goal of this module is to unify iteration across all containers in Rust.
|
|
|
|
//! An iterator can be considered as a state machine which is used to track which
|
|
|
|
//! element will be yielded next.
|
|
|
|
//!
|
|
|
|
//! There are various extensions also defined in this module to assist with various
|
|
|
|
//! types of iteration, such as the `DoubleEndedIterator` for iterating in reverse,
|
|
|
|
//! the `FromIterator` trait for creating a container from an iterator, and much
|
|
|
|
//! more.
|
|
|
|
//!
|
|
|
|
//! ## Rust's `for` loop
|
|
|
|
//!
|
|
|
|
//! The special syntax used by rust's `for` loop is based around the `Iterator`
|
|
|
|
//! trait defined in this module. For loops can be viewed as a syntactical expansion
|
|
|
|
//! into a `loop`, for example, the `for` loop in this example is essentially
|
|
|
|
//! translated to the `loop` below.
|
|
|
|
//!
|
|
|
|
//! ```rust
|
|
|
|
//! let values = vec![1i, 2, 3];
|
|
|
|
//!
|
|
|
|
//! // "Syntactical sugar" taking advantage of an iterator
|
|
|
|
//! for &x in values.iter() {
|
|
|
|
//! println!("{}", x);
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! // Rough translation of the iteration without a `for` iterator.
|
|
|
|
//! let mut it = values.iter();
|
|
|
|
//! loop {
|
|
|
|
//! match it.next() {
|
|
|
|
//! Some(&x) => {
|
|
|
|
//! println!("{}", x);
|
|
|
|
//! }
|
|
|
|
//! None => { break }
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! This `for` loop syntax can be applied to any iterator over any type.
|
2013-04-09 09:54:32 -05:00
|
|
|
|
2014-11-06 02:05:53 -06:00
|
|
|
pub use self::MinMaxResult::*;
|
|
|
|
|
2014-07-06 17:50:53 -05:00
|
|
|
use clone::Clone;
|
2013-05-24 21:35:29 -05:00
|
|
|
use cmp;
|
2014-11-09 16:35:53 -06:00
|
|
|
use cmp::Ord;
|
2014-07-06 17:50:53 -05:00
|
|
|
use mem;
|
2014-11-09 16:35:53 -06:00
|
|
|
use num::{ToPrimitive, Int};
|
2014-12-02 12:59:08 -06:00
|
|
|
use ops::{Add, Deref, FnMut};
|
2014-11-28 10:57:41 -06:00
|
|
|
use option::Option;
|
|
|
|
use option::Option::{Some, None};
|
2013-07-02 22:59:26 -05:00
|
|
|
use uint;
|
2014-11-17 20:00:07 -06:00
|
|
|
|
2014-11-07 18:39:39 -06:00
|
|
|
#[deprecated = "renamed to Extend"] pub use self::Extend as Extendable;
|
2013-04-09 09:54:32 -05:00
|
|
|
|
2013-06-21 06:57:22 -05:00
|
|
|
/// Conversion from an `Iterator`
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "may be replaced by a more general conversion trait"]
|
std: Move the iterator param on FromIterator and Extendable to the method.
If they are on the trait then it is extremely annoying to use them as
generic parameters to a function, e.g. with the iterator param on the trait
itself, if one was to pass an Extendable<int> to a function that filled it
either from a Range or a Map<VecIterator>, one needs to write something
like:
fn foo<E: Extendable<int, Range<int>> +
Extendable<int, Map<&'self int, int, VecIterator<int>>>
(e: &mut E, ...) { ... }
since using a generic, i.e. `foo<E: Extendable<int, I>, I: Iterator<int>>`
means that `foo` takes 2 type parameters, and the caller has to specify them
(which doesn't work anyway, as they'll mismatch with the iterators used in
`foo` itself).
This patch changes it to:
fn foo<E: Extendable<int>>(e: &mut E, ...) { ... }
2013-08-13 08:08:14 -05:00
|
|
|
pub trait FromIterator<A> {
|
2013-06-21 06:57:22 -05:00
|
|
|
/// Build a container with elements from an external iterator.
|
2014-03-30 23:45:55 -05:00
|
|
|
fn from_iter<T: Iterator<A>>(iterator: T) -> Self;
|
2013-06-21 06:57:22 -05:00
|
|
|
}
|
|
|
|
|
2013-07-27 16:41:30 -05:00
|
|
|
/// A type growable from an `Iterator` implementation
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "just renamed as part of collections reform"]
|
2014-11-07 18:39:39 -06:00
|
|
|
pub trait Extend<A> {
|
|
|
|
/// Extend a container with the elements yielded by an arbitrary iterator
|
2014-03-20 08:12:56 -05:00
|
|
|
fn extend<T: Iterator<A>>(&mut self, iterator: T);
|
2013-07-27 16:41:30 -05:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// An interface for dealing with "external iterators". These types of iterators
|
|
|
|
/// can be resumed at any time as all state is stored internally as opposed to
|
|
|
|
/// being located on the call stack.
|
2013-08-03 15:51:49 -05:00
|
|
|
///
|
|
|
|
/// The Iterator protocol states that an iterator yields a (potentially-empty,
|
|
|
|
/// potentially-infinite) sequence of values, and returns `None` to signal that
|
|
|
|
/// it's finished. The Iterator protocol does not define behavior after `None`
|
|
|
|
/// is returned. A concrete Iterator implementation may choose to behave however
|
|
|
|
/// it wishes, either by returning `None` infinitely, or by doing something
|
|
|
|
/// else.
|
2014-07-21 22:54:28 -05:00
|
|
|
#[lang="iterator"]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "just split up for object safety"]
|
2013-04-15 09:30:16 -05:00
|
|
|
pub trait Iterator<A> {
|
2013-04-09 09:54:32 -05:00
|
|
|
/// Advance the iterator and return the next value. Return `None` when the end is reached.
|
2013-04-15 09:30:16 -05:00
|
|
|
fn next(&mut self) -> Option<A>;
|
2013-06-21 05:12:01 -05:00
|
|
|
|
2014-07-21 23:32:49 -05:00
|
|
|
/// Returns a lower and upper bound on the remaining length of the iterator.
|
2013-06-21 05:12:01 -05:00
|
|
|
///
|
2014-07-21 23:32:49 -05:00
|
|
|
/// An upper bound of `None` means either there is no known upper bound, or the upper bound
|
|
|
|
/// does not fit within a `uint`.
|
2013-08-01 17:35:46 -05:00
|
|
|
#[inline]
|
2013-07-02 20:40:46 -05:00
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) { (0, None) }
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
}
|
2013-07-10 23:23:59 -05:00
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "new convention for extension traits"]
|
|
|
|
/// An extension trait providing numerous methods applicable to all iterators.
|
|
|
|
pub trait IteratorExt<A>: Iterator<A> {
|
2014-12-15 03:22:49 -06:00
|
|
|
/// Chain this iterator with another, returning a new iterator that will
|
|
|
|
/// finish iterating over the current iterator, and then iterate
|
2013-05-28 16:35:52 -05:00
|
|
|
/// over the other specified iterator.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = [0i];
|
|
|
|
/// let b = [1i];
|
2013-08-09 22:22:59 -05:00
|
|
|
/// let mut it = a.iter().chain(b.iter());
|
2013-11-17 12:31:50 -06:00
|
|
|
/// assert_eq!(it.next().unwrap(), &0);
|
|
|
|
/// assert_eq!(it.next().unwrap(), &1);
|
2013-05-28 16:35:52 -05:00
|
|
|
/// assert!(it.next().is_none());
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2013-08-09 22:22:59 -05:00
|
|
|
fn chain<U: Iterator<A>>(self, other: U) -> Chain<Self, U> {
|
2013-08-09 09:19:23 -05:00
|
|
|
Chain{a: self, b: other, flag: false}
|
|
|
|
}
|
2013-05-28 16:35:52 -05:00
|
|
|
|
2014-12-15 03:22:49 -06:00
|
|
|
/// Creates an iterator that iterates over both this and the specified
|
2013-05-28 16:35:52 -05:00
|
|
|
/// iterators simultaneously, yielding the two elements as pairs. When
|
|
|
|
/// either iterator returns None, all further invocations of next() will
|
|
|
|
/// return None.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = [0i];
|
|
|
|
/// let b = [1i];
|
2013-05-28 16:35:52 -05:00
|
|
|
/// let mut it = a.iter().zip(b.iter());
|
2014-06-25 20:18:13 -05:00
|
|
|
/// let (x0, x1) = (0i, 1i);
|
|
|
|
/// assert_eq!(it.next().unwrap(), (&x0, &x1));
|
2013-05-28 16:35:52 -05:00
|
|
|
/// assert!(it.next().is_none());
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2013-08-09 09:19:23 -05:00
|
|
|
fn zip<B, U: Iterator<B>>(self, other: U) -> Zip<Self, U> {
|
|
|
|
Zip{a: self, b: other}
|
|
|
|
}
|
2013-05-28 16:35:52 -05:00
|
|
|
|
2014-12-15 03:22:49 -06:00
|
|
|
/// Creates a new iterator that will apply the specified function to each
|
2013-06-29 00:05:50 -05:00
|
|
|
/// element returned by the first, yielding the mapped element instead.
|
2013-05-28 16:35:52 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = [1i, 2];
|
2013-08-09 22:09:47 -05:00
|
|
|
/// let mut it = a.iter().map(|&x| 2 * x);
|
2013-11-17 12:31:50 -06:00
|
|
|
/// assert_eq!(it.next().unwrap(), 2);
|
|
|
|
/// assert_eq!(it.next().unwrap(), 4);
|
2013-05-28 16:35:52 -05:00
|
|
|
/// assert!(it.next().is_none());
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "waiting for unboxed closures"]
|
2014-12-02 12:59:08 -06:00
|
|
|
fn map<B, F: FnMut(A) -> B>(self, f: F) -> Map<A, B, Self, F> {
|
2013-08-09 09:19:23 -05:00
|
|
|
Map{iter: self, f: f}
|
|
|
|
}
|
2013-05-28 16:35:52 -05:00
|
|
|
|
2014-12-15 03:22:49 -06:00
|
|
|
/// Creates an iterator that applies the predicate to each element returned
|
|
|
|
/// by this iterator. Only elements that have the predicate evaluate to
|
2013-05-28 16:35:52 -05:00
|
|
|
/// `true` will be yielded.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = [1i, 2];
|
2013-05-28 16:35:52 -05:00
|
|
|
/// let mut it = a.iter().filter(|&x| *x > 1);
|
2013-11-17 12:31:50 -06:00
|
|
|
/// assert_eq!(it.next().unwrap(), &2);
|
2013-05-28 16:35:52 -05:00
|
|
|
/// assert!(it.next().is_none());
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "waiting for unboxed closures"]
|
2014-12-02 16:49:42 -06:00
|
|
|
fn filter<P>(self, predicate: P) -> Filter<A, Self, P> where P: FnMut(&A) -> bool {
|
2013-08-09 09:19:23 -05:00
|
|
|
Filter{iter: self, predicate: predicate}
|
|
|
|
}
|
2013-05-28 16:35:52 -05:00
|
|
|
|
2014-12-15 03:22:49 -06:00
|
|
|
/// Creates an iterator that both filters and maps elements.
|
2013-05-28 16:35:52 -05:00
|
|
|
/// If the specified function returns None, the element is skipped.
|
|
|
|
/// Otherwise the option is unwrapped and the new value is yielded.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = [1i, 2];
|
2013-05-28 16:35:52 -05:00
|
|
|
/// let mut it = a.iter().filter_map(|&x| if x > 1 {Some(2 * x)} else {None});
|
2013-11-17 12:31:50 -06:00
|
|
|
/// assert_eq!(it.next().unwrap(), 4);
|
2013-05-28 16:35:52 -05:00
|
|
|
/// assert!(it.next().is_none());
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "waiting for unboxed closures"]
|
2014-12-02 20:00:07 -06:00
|
|
|
fn filter_map<B, F>(self, f: F) -> FilterMap<A, B, Self, F> where F: FnMut(A) -> Option<B> {
|
2013-08-09 09:19:23 -05:00
|
|
|
FilterMap { iter: self, f: f }
|
|
|
|
}
|
2013-05-28 16:35:52 -05:00
|
|
|
|
2014-12-15 03:22:49 -06:00
|
|
|
/// Creates an iterator that yields a pair of the value returned by this
|
2013-05-28 16:35:52 -05:00
|
|
|
/// iterator plus the current index of iteration.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = [100i, 200];
|
2013-05-28 16:35:52 -05:00
|
|
|
/// let mut it = a.iter().enumerate();
|
2014-06-25 20:18:13 -05:00
|
|
|
/// let (x100, x200) = (100i, 200i);
|
|
|
|
/// assert_eq!(it.next().unwrap(), (0, &x100));
|
|
|
|
/// assert_eq!(it.next().unwrap(), (1, &x200));
|
2013-05-28 16:35:52 -05:00
|
|
|
/// assert!(it.next().is_none());
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2013-08-09 09:19:23 -05:00
|
|
|
fn enumerate(self) -> Enumerate<Self> {
|
|
|
|
Enumerate{iter: self, count: 0}
|
|
|
|
}
|
2013-05-28 16:35:52 -05:00
|
|
|
|
2013-08-08 14:46:36 -05:00
|
|
|
|
|
|
|
/// Creates an iterator that has a `.peek()` method
|
2014-01-30 12:29:35 -06:00
|
|
|
/// that returns an optional reference to the next element.
|
2013-08-08 14:46:36 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let xs = [100i, 200, 300];
|
2013-12-22 15:31:23 -06:00
|
|
|
/// let mut it = xs.iter().map(|x| *x).peekable();
|
2014-06-25 20:18:13 -05:00
|
|
|
/// assert_eq!(*it.peek().unwrap(), 100);
|
2013-08-08 14:46:36 -05:00
|
|
|
/// assert_eq!(it.next().unwrap(), 100);
|
|
|
|
/// assert_eq!(it.next().unwrap(), 200);
|
2014-06-25 20:18:13 -05:00
|
|
|
/// assert_eq!(*it.peek().unwrap(), 300);
|
|
|
|
/// assert_eq!(*it.peek().unwrap(), 300);
|
2013-08-08 14:46:36 -05:00
|
|
|
/// assert_eq!(it.next().unwrap(), 300);
|
|
|
|
/// assert!(it.peek().is_none());
|
|
|
|
/// assert!(it.next().is_none());
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-13 20:41:50 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2013-08-08 14:46:36 -05:00
|
|
|
fn peekable(self) -> Peekable<A, Self> {
|
|
|
|
Peekable{iter: self, peeked: None}
|
|
|
|
}
|
|
|
|
|
2014-12-15 03:22:49 -06:00
|
|
|
/// Creates an iterator that invokes the predicate on elements until it
|
2013-06-11 18:34:03 -05:00
|
|
|
/// returns false. Once the predicate returns false, all further elements are
|
2013-05-28 16:35:52 -05:00
|
|
|
/// yielded.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = [1i, 2, 3, 2, 1];
|
2013-05-28 16:35:52 -05:00
|
|
|
/// let mut it = a.iter().skip_while(|&a| *a < 3);
|
2013-11-17 12:31:50 -06:00
|
|
|
/// assert_eq!(it.next().unwrap(), &3);
|
|
|
|
/// assert_eq!(it.next().unwrap(), &2);
|
|
|
|
/// assert_eq!(it.next().unwrap(), &1);
|
2013-05-28 16:35:52 -05:00
|
|
|
/// assert!(it.next().is_none());
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "waiting for unboxed closures"]
|
2014-12-02 22:28:32 -06:00
|
|
|
fn skip_while<P>(self, predicate: P) -> SkipWhile<A, Self, P> where P: FnMut(&A) -> bool {
|
2013-08-09 09:19:23 -05:00
|
|
|
SkipWhile{iter: self, flag: false, predicate: predicate}
|
|
|
|
}
|
2013-05-28 16:35:52 -05:00
|
|
|
|
2014-12-15 03:22:49 -06:00
|
|
|
/// Creates an iterator that yields elements so long as the predicate
|
2013-05-28 16:35:52 -05:00
|
|
|
/// returns true. After the predicate returns false for the first time, no
|
|
|
|
/// further elements will be yielded.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = [1i, 2, 3, 2, 1];
|
2013-05-28 16:35:52 -05:00
|
|
|
/// let mut it = a.iter().take_while(|&a| *a < 3);
|
2013-11-17 12:31:50 -06:00
|
|
|
/// assert_eq!(it.next().unwrap(), &1);
|
|
|
|
/// assert_eq!(it.next().unwrap(), &2);
|
2013-05-28 16:35:52 -05:00
|
|
|
/// assert!(it.next().is_none());
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "waiting for unboxed closures, may want to require peek"]
|
2014-12-02 23:42:28 -06:00
|
|
|
fn take_while<P>(self, predicate: P) -> TakeWhile<A, Self, P> where P: FnMut(&A) -> bool {
|
2013-08-09 09:19:23 -05:00
|
|
|
TakeWhile{iter: self, flag: false, predicate: predicate}
|
|
|
|
}
|
2013-05-28 16:35:52 -05:00
|
|
|
|
2014-12-15 03:22:49 -06:00
|
|
|
/// Creates an iterator that skips the first `n` elements of this iterator,
|
|
|
|
/// and then yields all further items.
|
2013-05-28 16:35:52 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = [1i, 2, 3, 4, 5];
|
2013-05-28 16:35:52 -05:00
|
|
|
/// let mut it = a.iter().skip(3);
|
2013-11-17 12:31:50 -06:00
|
|
|
/// assert_eq!(it.next().unwrap(), &4);
|
|
|
|
/// assert_eq!(it.next().unwrap(), &5);
|
2013-05-28 16:35:52 -05:00
|
|
|
/// assert!(it.next().is_none());
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2013-08-09 09:19:23 -05:00
|
|
|
fn skip(self, n: uint) -> Skip<Self> {
|
|
|
|
Skip{iter: self, n: n}
|
|
|
|
}
|
2013-05-28 16:35:52 -05:00
|
|
|
|
2014-12-15 03:22:49 -06:00
|
|
|
/// Creates an iterator that yields the first `n` elements of this
|
|
|
|
/// iterator, and then will always return None.
|
2013-05-28 16:35:52 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = [1i, 2, 3, 4, 5];
|
2013-08-09 22:16:07 -05:00
|
|
|
/// let mut it = a.iter().take(3);
|
2013-11-17 12:31:50 -06:00
|
|
|
/// assert_eq!(it.next().unwrap(), &1);
|
|
|
|
/// assert_eq!(it.next().unwrap(), &2);
|
|
|
|
/// assert_eq!(it.next().unwrap(), &3);
|
2013-05-28 16:35:52 -05:00
|
|
|
/// assert!(it.next().is_none());
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2013-08-09 22:16:07 -05:00
|
|
|
fn take(self, n: uint) -> Take<Self> {
|
2013-08-09 09:19:23 -05:00
|
|
|
Take{iter: self, n: n}
|
|
|
|
}
|
2013-05-28 16:35:52 -05:00
|
|
|
|
2014-12-15 03:22:49 -06:00
|
|
|
/// Creates a new iterator that behaves in a similar fashion to fold.
|
2013-05-28 16:35:52 -05:00
|
|
|
/// There is a state which is passed between each iteration and can be
|
|
|
|
/// mutated as necessary. The yielded values from the closure are yielded
|
2013-07-27 16:41:20 -05:00
|
|
|
/// from the Scan instance when not None.
|
2013-05-28 16:35:52 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = [1i, 2, 3, 4, 5];
|
2013-05-28 16:35:52 -05:00
|
|
|
/// let mut it = a.iter().scan(1, |fac, &x| {
|
|
|
|
/// *fac = *fac * x;
|
|
|
|
/// Some(*fac)
|
|
|
|
/// });
|
2013-11-17 12:31:50 -06:00
|
|
|
/// assert_eq!(it.next().unwrap(), 1);
|
|
|
|
/// assert_eq!(it.next().unwrap(), 2);
|
|
|
|
/// assert_eq!(it.next().unwrap(), 6);
|
|
|
|
/// assert_eq!(it.next().unwrap(), 24);
|
|
|
|
/// assert_eq!(it.next().unwrap(), 120);
|
2013-05-28 16:35:52 -05:00
|
|
|
/// assert!(it.next().is_none());
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "waiting for unboxed closures"]
|
2014-12-03 00:59:31 -06:00
|
|
|
fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<A, B, Self, St, F> where
|
|
|
|
F: FnMut(&mut St, A) -> Option<B>,
|
|
|
|
{
|
2013-08-09 09:19:23 -05:00
|
|
|
Scan{iter: self, f: f, state: initial_state}
|
|
|
|
}
|
2013-05-28 16:35:52 -05:00
|
|
|
|
2013-06-23 19:34:38 -05:00
|
|
|
/// Creates an iterator that maps each element to an iterator,
|
|
|
|
/// and yields the elements of the produced iterators
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2013-12-22 15:31:23 -06:00
|
|
|
/// use std::iter::count;
|
|
|
|
///
|
2013-06-23 19:34:38 -05:00
|
|
|
/// let xs = [2u, 3];
|
|
|
|
/// let ys = [0u, 1, 0, 1, 2];
|
2013-08-09 22:20:05 -05:00
|
|
|
/// let mut it = xs.iter().flat_map(|&x| count(0u, 1).take(x));
|
2013-06-23 19:34:38 -05:00
|
|
|
/// // Check that `it` has the same elements as `ys`
|
|
|
|
/// let mut i = 0;
|
2013-12-22 15:31:23 -06:00
|
|
|
/// for x in it {
|
2013-06-23 19:34:38 -05:00
|
|
|
/// assert_eq!(x, ys[i]);
|
|
|
|
/// i += 1;
|
|
|
|
/// }
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "waiting for unboxed closures"]
|
2014-12-03 02:07:16 -06:00
|
|
|
fn flat_map<B, U, F>(self, f: F) -> FlatMap<A, B, Self, U, F> where
|
|
|
|
U: Iterator<B>,
|
|
|
|
F: FnMut(A) -> U,
|
|
|
|
{
|
2013-08-09 09:19:23 -05:00
|
|
|
FlatMap{iter: self, f: f, frontiter: None, backiter: None }
|
|
|
|
}
|
2013-06-23 19:34:38 -05:00
|
|
|
|
2013-08-03 15:51:49 -05:00
|
|
|
/// Creates an iterator that yields `None` forever after the underlying
|
|
|
|
/// iterator yields `None`. Random-access iterator behavior is not
|
|
|
|
/// affected, only single and double-ended iterator behavior.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2013-08-03 15:51:49 -05:00
|
|
|
/// fn process<U: Iterator<int>>(it: U) -> int {
|
|
|
|
/// let mut it = it.fuse();
|
|
|
|
/// let mut sum = 0;
|
|
|
|
/// for x in it {
|
|
|
|
/// if x > 5 {
|
2014-09-26 01:02:28 -05:00
|
|
|
/// break;
|
2013-08-03 15:51:49 -05:00
|
|
|
/// }
|
|
|
|
/// sum += x;
|
|
|
|
/// }
|
|
|
|
/// // did we exhaust the iterator?
|
|
|
|
/// if it.next().is_none() {
|
|
|
|
/// sum += 1000;
|
|
|
|
/// }
|
|
|
|
/// sum
|
|
|
|
/// }
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let x = vec![1i,2,3,7,8,9];
|
2014-09-26 01:02:28 -05:00
|
|
|
/// assert_eq!(process(x.into_iter()), 6);
|
|
|
|
/// let x = vec![1i,2,3];
|
2014-09-14 22:27:36 -05:00
|
|
|
/// assert_eq!(process(x.into_iter()), 1006);
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-03 15:51:49 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2013-08-03 15:51:49 -05:00
|
|
|
fn fuse(self) -> Fuse<Self> {
|
|
|
|
Fuse{iter: self, done: false}
|
|
|
|
}
|
|
|
|
|
2013-07-05 12:00:11 -05:00
|
|
|
/// Creates an iterator that calls a function with a reference to each
|
|
|
|
/// element before yielding it. This is often useful for debugging an
|
|
|
|
/// iterator pipeline.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2013-12-22 15:31:23 -06:00
|
|
|
/// use std::iter::AdditiveIterator;
|
|
|
|
///
|
|
|
|
/// let xs = [1u, 4, 2, 3, 8, 9, 6];
|
|
|
|
/// let sum = xs.iter()
|
|
|
|
/// .map(|&x| x)
|
log: Introduce liblog, the old std::logging
This commit moves all logging out of the standard library into an external
crate. This crate is the new crate which is responsible for all logging macros
and logging implementation. A few reasons for this change are:
* The crate map has always been a bit of a code smell among rust programs. It
has difficulty being loaded on almost all platforms, and it's used almost
exclusively for logging and only logging. Removing the crate map is one of the
end goals of this movement.
* The compiler has a fair bit of special support for logging. It has the
__log_level() expression as well as generating a global word per module
specifying the log level. This is unfairly favoring the built-in logging
system, and is much better done purely in libraries instead of the compiler
itself.
* Initialization of logging is much easier to do if there is no reliance on a
magical crate map being available to set module log levels.
* If the logging library can be written outside of the standard library, there's
no reason that it shouldn't be. It's likely that we're not going to build the
highest quality logging library of all time, so third-party libraries should
be able to provide just as high-quality logging systems as the default one
provided in the rust distribution.
With a migration such as this, the change does not come for free. There are some
subtle changes in the behavior of liblog vs the previous logging macros:
* The core change of this migration is that there is no longer a physical
log-level per module. This concept is still emulated (it is quite useful), but
there is now only a global log level, not a local one. This global log level
is a reflection of the maximum of all log levels specified. The previously
generated logging code looked like:
if specified_level <= __module_log_level() {
println!(...)
}
The newly generated code looks like:
if specified_level <= ::log::LOG_LEVEL {
if ::log::module_enabled(module_path!()) {
println!(...)
}
}
Notably, the first layer of checking is still intended to be "super fast" in
that it's just a load of a global word and a compare. The second layer of
checking is executed to determine if the current module does indeed have
logging turned on.
This means that if any module has a debug log level turned on, all modules
with debug log levels get a little bit slower (they all do more expensive
dynamic checks to determine if they're turned on or not).
Semantically, this migration brings no change in this respect, but
runtime-wise, this will have a perf impact on some code.
* A `RUST_LOG=::help` directive will no longer print out a list of all modules
that can be logged. This is because the crate map will no longer specify the
log levels of all modules, so the list of modules is not known. Additionally,
warnings can no longer be provided if a malformed logging directive was
supplied.
The new "hello world" for logging looks like:
#[phase(syntax, link)]
extern crate log;
fn main() {
debug!("Hello, world!");
}
2014-03-09 00:11:44 -06:00
|
|
|
/// .inspect(|&x| println!("filtering {}", x))
|
2013-12-22 15:31:23 -06:00
|
|
|
/// .filter(|&x| x % 2 == 0)
|
log: Introduce liblog, the old std::logging
This commit moves all logging out of the standard library into an external
crate. This crate is the new crate which is responsible for all logging macros
and logging implementation. A few reasons for this change are:
* The crate map has always been a bit of a code smell among rust programs. It
has difficulty being loaded on almost all platforms, and it's used almost
exclusively for logging and only logging. Removing the crate map is one of the
end goals of this movement.
* The compiler has a fair bit of special support for logging. It has the
__log_level() expression as well as generating a global word per module
specifying the log level. This is unfairly favoring the built-in logging
system, and is much better done purely in libraries instead of the compiler
itself.
* Initialization of logging is much easier to do if there is no reliance on a
magical crate map being available to set module log levels.
* If the logging library can be written outside of the standard library, there's
no reason that it shouldn't be. It's likely that we're not going to build the
highest quality logging library of all time, so third-party libraries should
be able to provide just as high-quality logging systems as the default one
provided in the rust distribution.
With a migration such as this, the change does not come for free. There are some
subtle changes in the behavior of liblog vs the previous logging macros:
* The core change of this migration is that there is no longer a physical
log-level per module. This concept is still emulated (it is quite useful), but
there is now only a global log level, not a local one. This global log level
is a reflection of the maximum of all log levels specified. The previously
generated logging code looked like:
if specified_level <= __module_log_level() {
println!(...)
}
The newly generated code looks like:
if specified_level <= ::log::LOG_LEVEL {
if ::log::module_enabled(module_path!()) {
println!(...)
}
}
Notably, the first layer of checking is still intended to be "super fast" in
that it's just a load of a global word and a compare. The second layer of
checking is executed to determine if the current module does indeed have
logging turned on.
This means that if any module has a debug log level turned on, all modules
with debug log levels get a little bit slower (they all do more expensive
dynamic checks to determine if they're turned on or not).
Semantically, this migration brings no change in this respect, but
runtime-wise, this will have a perf impact on some code.
* A `RUST_LOG=::help` directive will no longer print out a list of all modules
that can be logged. This is because the crate map will no longer specify the
log levels of all modules, so the list of modules is not known. Additionally,
warnings can no longer be provided if a malformed logging directive was
supplied.
The new "hello world" for logging looks like:
#[phase(syntax, link)]
extern crate log;
fn main() {
debug!("Hello, world!");
}
2014-03-09 00:11:44 -06:00
|
|
|
/// .inspect(|&x| println!("{} made it through", x))
|
2014-11-13 22:35:44 -06:00
|
|
|
/// .sum();
|
2014-01-09 04:06:55 -06:00
|
|
|
/// println!("{}", sum);
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "waiting for unboxed closures"]
|
2014-12-03 03:04:27 -06:00
|
|
|
fn inspect<F>(self, f: F) -> Inspect<A, Self, F> where F: FnMut(&A) {
|
2013-08-10 18:55:34 -05:00
|
|
|
Inspect{iter: self, f: f}
|
2013-08-09 09:19:23 -05:00
|
|
|
}
|
2013-07-05 12:00:11 -05:00
|
|
|
|
2013-09-30 13:59:46 -05:00
|
|
|
/// Creates a wrapper around a mutable reference to the iterator.
|
|
|
|
///
|
|
|
|
/// This is useful to allow applying iterator adaptors while still
|
|
|
|
/// retaining ownership of the original iterator value.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let mut xs = range(0u, 10);
|
2013-09-30 13:59:46 -05:00
|
|
|
/// // sum the first five values
|
|
|
|
/// let partial_sum = xs.by_ref().take(5).fold(0, |a, b| a + b);
|
|
|
|
/// assert!(partial_sum == 10);
|
|
|
|
/// // xs.next() is now `5`
|
|
|
|
/// assert!(xs.next() == Some(5));
|
|
|
|
/// ```
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2013-09-30 13:59:46 -05:00
|
|
|
fn by_ref<'r>(&'r mut self) -> ByRef<'r, Self> {
|
|
|
|
ByRef{iter: self}
|
|
|
|
}
|
|
|
|
|
2013-06-03 16:48:52 -05:00
|
|
|
/// Loops through the entire iterator, collecting all of the elements into
|
2013-06-24 00:21:32 -05:00
|
|
|
/// a container implementing `FromIterator`.
|
2013-06-03 16:48:52 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = [1i, 2, 3, 4, 5];
|
2014-05-04 22:54:02 -05:00
|
|
|
/// let b: Vec<int> = a.iter().map(|&x| x).collect();
|
|
|
|
/// assert!(a.as_slice() == b.as_slice());
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "waiting for general conversion traits, just changed to take self by value"]
|
|
|
|
fn collect<B: FromIterator<A>>(self) -> B {
|
|
|
|
FromIterator::from_iter(self)
|
2013-08-09 09:19:23 -05:00
|
|
|
}
|
2013-06-03 16:48:52 -05:00
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Loops through `n` iterations, returning the `n`th element of the
|
|
|
|
/// iterator.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = [1i, 2, 3, 4, 5];
|
2013-05-28 16:35:52 -05:00
|
|
|
/// let mut it = a.iter();
|
2013-11-17 12:31:50 -06:00
|
|
|
/// assert!(it.nth(2).unwrap() == &3);
|
2013-05-28 16:35:52 -05:00
|
|
|
/// assert!(it.nth(2) == None);
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2013-08-09 09:19:23 -05:00
|
|
|
fn nth(&mut self, mut n: uint) -> Option<A> {
|
2014-08-06 05:20:37 -05:00
|
|
|
for x in *self {
|
|
|
|
if n == 0 { return Some(x) }
|
2013-08-09 09:19:23 -05:00
|
|
|
n -= 1;
|
|
|
|
}
|
2014-08-06 05:20:37 -05:00
|
|
|
None
|
2013-08-09 09:19:23 -05:00
|
|
|
}
|
2013-05-28 16:35:52 -05:00
|
|
|
|
|
|
|
/// Loops through the entire iterator, returning the last element of the
|
|
|
|
/// iterator.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = [1i, 2, 3, 4, 5];
|
2013-11-17 12:31:50 -06:00
|
|
|
/// assert!(a.iter().last().unwrap() == &5);
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "just changed to take self by value"]
|
|
|
|
fn last(mut self) -> Option<A> {
|
2013-08-09 09:19:23 -05:00
|
|
|
let mut last = None;
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
for x in self { last = Some(x); }
|
2013-08-09 09:19:23 -05:00
|
|
|
last
|
|
|
|
}
|
2013-05-28 16:35:52 -05:00
|
|
|
|
|
|
|
/// Performs a fold operation over the entire iterator, returning the
|
|
|
|
/// eventual state at the end of the iteration.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = [1i, 2, 3, 4, 5];
|
2013-05-28 16:35:52 -05:00
|
|
|
/// assert!(a.iter().fold(0, |a, &b| a + b) == 15);
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "waiting for unboxed closures, just changed to take self by value"]
|
2014-12-03 18:43:25 -06:00
|
|
|
fn fold<B, F>(mut self, init: B, mut f: F) -> B where F: FnMut(B, A) -> B {
|
2013-08-09 09:19:23 -05:00
|
|
|
let mut accum = init;
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
for x in self {
|
2014-08-06 05:20:37 -05:00
|
|
|
accum = f(accum, x);
|
2013-08-09 09:19:23 -05:00
|
|
|
}
|
|
|
|
accum
|
|
|
|
}
|
2013-05-28 16:35:52 -05:00
|
|
|
|
|
|
|
/// Counts the number of elements in this iterator.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = [1i, 2, 3, 4, 5];
|
2013-05-28 16:35:52 -05:00
|
|
|
/// let mut it = a.iter();
|
2014-06-06 01:18:51 -05:00
|
|
|
/// assert!(it.count() == 5);
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "just changed to take self by value"]
|
|
|
|
fn count(self) -> uint {
|
2013-08-09 09:19:23 -05:00
|
|
|
self.fold(0, |cnt, _x| cnt + 1)
|
|
|
|
}
|
2013-05-28 16:35:52 -05:00
|
|
|
|
|
|
|
/// Tests whether the predicate holds true for all elements in the iterator.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = [1i, 2, 3, 4, 5];
|
2013-12-22 15:31:23 -06:00
|
|
|
/// assert!(a.iter().all(|x| *x > 0));
|
|
|
|
/// assert!(!a.iter().all(|x| *x > 2));
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "waiting for unboxed closures, just changed to take self by value"]
|
2014-12-03 18:43:25 -06:00
|
|
|
fn all<F>(mut self, mut f: F) -> bool where F: FnMut(A) -> bool {
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
for x in self { if !f(x) { return false; } }
|
2013-08-09 09:19:23 -05:00
|
|
|
true
|
|
|
|
}
|
2013-05-28 16:35:52 -05:00
|
|
|
|
|
|
|
/// Tests whether any element of an iterator satisfies the specified
|
|
|
|
/// predicate.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = [1i, 2, 3, 4, 5];
|
2013-05-28 16:35:52 -05:00
|
|
|
/// let mut it = a.iter();
|
2013-12-22 15:31:23 -06:00
|
|
|
/// assert!(it.any(|x| *x == 3));
|
|
|
|
/// assert!(!it.any(|x| *x == 3));
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "waiting for unboxed closures"]
|
2014-12-03 18:43:25 -06:00
|
|
|
fn any<F>(&mut self, mut f: F) -> bool where F: FnMut(A) -> bool {
|
2013-08-03 11:45:23 -05:00
|
|
|
for x in *self { if f(x) { return true; } }
|
2013-06-15 16:42:31 -05:00
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2014-11-20 12:05:46 -06:00
|
|
|
/// Returns the first element satisfying the specified predicate.
|
|
|
|
///
|
|
|
|
/// Does not consume the iterator past the first found element.
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "waiting for unboxed closures"]
|
2014-12-03 18:43:25 -06:00
|
|
|
fn find<P>(&mut self, mut predicate: P) -> Option<A> where P: FnMut(&A) -> bool {
|
2013-08-03 11:45:23 -05:00
|
|
|
for x in *self {
|
2013-06-15 16:42:31 -05:00
|
|
|
if predicate(&x) { return Some(x) }
|
|
|
|
}
|
|
|
|
None
|
2013-05-17 10:24:43 -05:00
|
|
|
}
|
2013-06-15 16:56:26 -05:00
|
|
|
|
|
|
|
/// Return the index of the first element satisfying the specified predicate
|
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "waiting for unboxed closures"]
|
2014-12-03 18:43:25 -06:00
|
|
|
fn position<P>(&mut self, mut predicate: P) -> Option<uint> where P: FnMut(A) -> bool {
|
2013-06-15 16:56:26 -05:00
|
|
|
let mut i = 0;
|
2013-08-03 11:45:23 -05:00
|
|
|
for x in *self {
|
2013-06-15 16:56:26 -05:00
|
|
|
if predicate(x) {
|
|
|
|
return Some(i);
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2013-06-17 18:43:22 -05:00
|
|
|
|
2013-08-17 17:28:04 -05:00
|
|
|
/// Return the element that gives the maximum value from the
|
|
|
|
/// specified function.
|
2013-08-09 09:19:23 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-11-12 07:02:42 -06:00
|
|
|
/// use core::num::SignedInt;
|
|
|
|
///
|
2013-12-22 15:31:23 -06:00
|
|
|
/// let xs = [-3i, 0, 1, 5, -10];
|
2013-08-09 09:19:23 -05:00
|
|
|
/// assert_eq!(*xs.iter().max_by(|x| x.abs()).unwrap(), -10);
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-06-26 16:51:04 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "waiting for unboxed closures, just changed to take self by value"]
|
2014-12-03 18:43:25 -06:00
|
|
|
fn max_by<B: Ord, F>(self, mut f: F) -> Option<A> where F: FnMut(&A) -> B {
|
2013-06-26 16:51:04 -05:00
|
|
|
self.fold(None, |max: Option<(A, B)>, x| {
|
|
|
|
let x_val = f(&x);
|
|
|
|
match max {
|
|
|
|
None => Some((x, x_val)),
|
|
|
|
Some((y, y_val)) => if x_val > y_val {
|
|
|
|
Some((x, x_val))
|
|
|
|
} else {
|
|
|
|
Some((y, y_val))
|
|
|
|
}
|
|
|
|
}
|
2013-09-20 01:08:47 -05:00
|
|
|
}).map(|(x, _)| x)
|
2013-06-26 16:51:04 -05:00
|
|
|
}
|
|
|
|
|
2013-08-17 17:28:04 -05:00
|
|
|
/// Return the element that gives the minimum value from the
|
|
|
|
/// specified function.
|
2013-08-09 09:19:23 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-11-12 07:02:42 -06:00
|
|
|
/// use core::num::SignedInt;
|
|
|
|
///
|
2013-12-22 15:31:23 -06:00
|
|
|
/// let xs = [-3i, 0, 1, 5, -10];
|
2013-08-09 09:19:23 -05:00
|
|
|
/// assert_eq!(*xs.iter().min_by(|x| x.abs()).unwrap(), 0);
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
2013-06-26 16:51:04 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "waiting for unboxed closures, just changed to take self by value"]
|
2014-12-03 18:43:25 -06:00
|
|
|
fn min_by<B: Ord, F>(self, mut f: F) -> Option<A> where F: FnMut(&A) -> B {
|
2013-06-26 16:51:04 -05:00
|
|
|
self.fold(None, |min: Option<(A, B)>, x| {
|
|
|
|
let x_val = f(&x);
|
|
|
|
match min {
|
|
|
|
None => Some((x, x_val)),
|
|
|
|
Some((y, y_val)) => if x_val < y_val {
|
|
|
|
Some((x, x_val))
|
|
|
|
} else {
|
|
|
|
Some((y, y_val))
|
|
|
|
}
|
|
|
|
}
|
2013-09-20 01:08:47 -05:00
|
|
|
}).map(|(x, _)| x)
|
2013-06-26 16:51:04 -05:00
|
|
|
}
|
2013-04-09 09:54:32 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
|
|
|
impl<A, I> IteratorExt<A> for I where I: Iterator<A> {}
|
|
|
|
|
2013-08-09 09:19:23 -05:00
|
|
|
/// A range iterator able to yield elements from both ends
|
2014-07-21 23:32:49 -05:00
|
|
|
///
|
|
|
|
/// A `DoubleEndedIterator` can be thought of as a deque in that `next()` and `next_back()` exhaust
|
|
|
|
/// elements from the *same* range, and do not work independently of each other.
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "recently split into two traits"]
|
2013-08-09 09:19:23 -05:00
|
|
|
pub trait DoubleEndedIterator<A>: Iterator<A> {
|
|
|
|
/// Yield an element from the end of the range, returning `None` if the range is empty.
|
|
|
|
fn next_back(&mut self) -> Option<A>;
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
}
|
2013-08-09 09:19:23 -05:00
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
/// Extension methods for double-ended iterators.
|
|
|
|
#[unstable = "new extension trait convention"]
|
|
|
|
pub trait DoubleEndedIteratorExt<A>: DoubleEndedIterator<A> {
|
2014-01-23 13:41:57 -06:00
|
|
|
/// Change the direction of the iterator
|
2013-08-09 09:19:23 -05:00
|
|
|
///
|
2014-01-20 04:23:23 -06:00
|
|
|
/// The flipped iterator swaps the ends on an iterator that can already
|
2013-08-09 09:19:23 -05:00
|
|
|
/// be iterated from the front and from the back.
|
|
|
|
///
|
|
|
|
///
|
2014-01-20 04:23:23 -06:00
|
|
|
/// If the iterator also implements RandomAccessIterator, the flipped
|
2013-08-09 09:19:23 -05:00
|
|
|
/// iterator is also random access, with the indices starting at the back
|
|
|
|
/// of the original iterator.
|
|
|
|
///
|
2014-01-20 04:23:23 -06:00
|
|
|
/// Note: Random access with flipped indices still only applies to the first
|
2014-01-25 01:37:51 -06:00
|
|
|
/// `uint::MAX` elements of the original iterator.
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2014-01-23 13:41:57 -06:00
|
|
|
fn rev(self) -> Rev<Self> {
|
|
|
|
Rev{iter: self}
|
2013-08-09 09:19:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
|
|
|
impl<A, I> DoubleEndedIteratorExt<A> for I where I: DoubleEndedIterator<A> {}
|
|
|
|
|
2013-08-19 16:52:20 -05:00
|
|
|
/// A double-ended iterator yielding mutable references
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental = "not widely used"]
|
2013-08-19 16:52:20 -05:00
|
|
|
pub trait MutableDoubleEndedIterator {
|
|
|
|
// FIXME: #5898: should be called `reverse`
|
|
|
|
/// Use an iterator to reverse a container in-place
|
|
|
|
fn reverse_(&mut self);
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental = "trait is experimental"]
|
2014-08-27 20:46:52 -05:00
|
|
|
impl<'a, A:'a, T: DoubleEndedIterator<&'a mut A>> MutableDoubleEndedIterator for T {
|
2013-08-19 16:52:20 -05:00
|
|
|
// FIXME: #5898: should be called `reverse`
|
|
|
|
/// Use an iterator to reverse a container in-place
|
|
|
|
fn reverse_(&mut self) {
|
|
|
|
loop {
|
|
|
|
match (self.next(), self.next_back()) {
|
2014-01-31 14:35:36 -06:00
|
|
|
(Some(x), Some(y)) => mem::swap(x, y),
|
2013-08-19 16:52:20 -05:00
|
|
|
_ => break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-01 09:46:12 -05:00
|
|
|
|
2013-08-09 09:19:23 -05:00
|
|
|
/// An object implementing random access indexing by `uint`
|
|
|
|
///
|
|
|
|
/// A `RandomAccessIterator` should be either infinite or a `DoubleEndedIterator`.
|
2014-07-21 23:32:49 -05:00
|
|
|
/// Calling `next()` or `next_back()` on a `RandomAccessIterator`
|
|
|
|
/// reduces the indexable range accordingly. That is, `it.idx(1)` will become `it.idx(0)`
|
|
|
|
/// after `it.next()` is called.
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental = "not widely used, may be better decomposed into Index and ExactSizeIterator"]
|
2013-08-09 09:19:23 -05:00
|
|
|
pub trait RandomAccessIterator<A>: Iterator<A> {
|
2014-01-25 01:37:51 -06:00
|
|
|
/// Return the number of indexable elements. At most `std::uint::MAX`
|
2013-08-09 09:19:23 -05:00
|
|
|
/// elements are indexable, even if the iterator represents a longer range.
|
|
|
|
fn indexable(&self) -> uint;
|
|
|
|
|
2014-07-21 23:32:49 -05:00
|
|
|
/// Return an element at an index, or `None` if the index is out of bounds
|
2014-04-22 00:15:42 -05:00
|
|
|
fn idx(&mut self, index: uint) -> Option<A>;
|
2013-08-09 09:19:23 -05:00
|
|
|
}
|
|
|
|
|
2013-09-01 09:46:12 -05:00
|
|
|
/// An iterator that knows its exact length
|
|
|
|
///
|
|
|
|
/// This trait is a helper for iterators like the vector iterator, so that
|
|
|
|
/// it can support double-ended enumeration.
|
|
|
|
///
|
|
|
|
/// `Iterator::size_hint` *must* return the exact size of the iterator.
|
|
|
|
/// Note that the size must fit in `uint`.
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "could move DoubleEndedIterator bound onto rposition with method-level where clauses"]
|
|
|
|
pub trait ExactSizeIterator<A> : DoubleEndedIterator<A> {
|
2013-08-30 13:00:14 -05:00
|
|
|
/// Return the index of the last element satisfying the specified predicate
|
|
|
|
///
|
|
|
|
/// If no element matches, None is returned.
|
|
|
|
#[inline]
|
2014-12-04 15:13:12 -06:00
|
|
|
fn rposition<P>(&mut self, mut predicate: P) -> Option<uint> where P: FnMut(A) -> bool {
|
2014-08-06 05:20:37 -05:00
|
|
|
let len = self.len();
|
|
|
|
for i in range(0, len).rev() {
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
if predicate(self.next_back().expect("rposition: incorrect ExactSizeIterator")) {
|
2014-08-06 05:20:37 -05:00
|
|
|
return Some(i);
|
2013-08-30 13:00:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2014-06-06 01:18:51 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Return the exact length of the iterator.
|
|
|
|
fn len(&self) -> uint {
|
|
|
|
let (lower, upper) = self.size_hint();
|
2014-08-24 08:04:28 -05:00
|
|
|
// Note: This assertion is overly defensive, but it checks the invariant
|
|
|
|
// guaranteed by the trait. If this trait were rust-internal,
|
|
|
|
// we could use debug_assert!; assert_eq! will check all Rust user
|
|
|
|
// implementations too.
|
2014-08-06 05:20:37 -05:00
|
|
|
assert_eq!(upper, Some(lower));
|
2014-06-06 01:18:51 -05:00
|
|
|
lower
|
|
|
|
}
|
2013-08-30 13:00:14 -05:00
|
|
|
}
|
|
|
|
|
2013-08-30 12:59:49 -05:00
|
|
|
// All adaptors that preserve the size of the wrapped iterator are fine
|
|
|
|
// Adaptors that may overflow in `size_hint` are not, i.e. `Chain`.
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
|
|
|
impl<A, T: ExactSizeIterator<A>> ExactSizeIterator<(uint, A)> for Enumerate<T> {}
|
|
|
|
#[unstable = "trait is unstable"]
|
2014-12-03 03:04:27 -06:00
|
|
|
impl<A, I, F> ExactSizeIterator<A> for Inspect<A, I, F> where
|
|
|
|
I: ExactSizeIterator<A>,
|
|
|
|
F: FnMut(&A),
|
|
|
|
{}
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
|
|
|
impl<A, T: ExactSizeIterator<A>> ExactSizeIterator<A> for Rev<T> {}
|
|
|
|
#[unstable = "trait is unstable"]
|
2014-12-02 12:59:08 -06:00
|
|
|
impl<A, B, I, F> ExactSizeIterator<B> for Map<A, B, I, F> where
|
|
|
|
I: ExactSizeIterator<A>,
|
|
|
|
F: FnMut(A) -> B,
|
|
|
|
{}
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
|
|
|
impl<A, B, T, U> ExactSizeIterator<(A, B)> for Zip<T, U>
|
|
|
|
where T: ExactSizeIterator<A>, U: ExactSizeIterator<B> {}
|
2013-08-30 12:59:49 -05:00
|
|
|
|
2013-08-09 09:19:23 -05:00
|
|
|
/// An double-ended iterator with the direction inverted
|
|
|
|
#[deriving(Clone)]
|
2014-07-09 07:13:14 -05:00
|
|
|
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2014-01-23 13:41:57 -06:00
|
|
|
pub struct Rev<T> {
|
2014-03-27 17:09:47 -05:00
|
|
|
iter: T
|
2013-08-09 09:19:23 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-01-23 13:41:57 -06:00
|
|
|
impl<A, T: DoubleEndedIterator<A>> Iterator<A> for Rev<T> {
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> { self.iter.next_back() }
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-01-23 13:41:57 -06:00
|
|
|
impl<A, T: DoubleEndedIterator<A>> DoubleEndedIterator<A> for Rev<T> {
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<A> { self.iter.next() }
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental = "trait is experimental"]
|
2013-08-09 09:19:23 -05:00
|
|
|
impl<A, T: DoubleEndedIterator<A> + RandomAccessIterator<A>> RandomAccessIterator<A>
|
2014-01-23 13:41:57 -06:00
|
|
|
for Rev<T> {
|
2013-08-09 09:19:23 -05:00
|
|
|
#[inline]
|
|
|
|
fn indexable(&self) -> uint { self.iter.indexable() }
|
|
|
|
#[inline]
|
2014-04-22 00:15:42 -05:00
|
|
|
fn idx(&mut self, index: uint) -> Option<A> {
|
|
|
|
let amt = self.indexable();
|
|
|
|
self.iter.idx(amt - index - 1)
|
2013-08-09 09:19:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-30 13:59:46 -05:00
|
|
|
/// A mutable reference to an iterator
|
2014-07-09 07:13:14 -05:00
|
|
|
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2014-08-27 20:46:52 -05:00
|
|
|
pub struct ByRef<'a, T:'a> {
|
|
|
|
iter: &'a mut T
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-08-27 20:46:52 -05:00
|
|
|
impl<'a, A, T: Iterator<A>+'a> Iterator<A> for ByRef<'a, T> {
|
2013-09-30 13:59:46 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> { self.iter.next() }
|
2013-12-11 20:37:45 -06:00
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
|
2013-09-30 13:59:46 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-08-27 20:46:52 -05:00
|
|
|
impl<'a, A, T: DoubleEndedIterator<A>+'a> DoubleEndedIterator<A> for ByRef<'a, T> {
|
2013-09-30 13:59:46 -05:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<A> { self.iter.next_back() }
|
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// A trait for iterators over elements which can be added together
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental = "needs to be re-evaluated as part of numerics reform"]
|
2013-05-17 10:18:09 -05:00
|
|
|
pub trait AdditiveIterator<A> {
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Iterates over the entire iterator, summing up all the elements
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2013-12-22 15:31:23 -06:00
|
|
|
/// use std::iter::AdditiveIterator;
|
|
|
|
///
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = [1i, 2, 3, 4, 5];
|
2013-08-09 22:09:47 -05:00
|
|
|
/// let mut it = a.iter().map(|&x| x);
|
2014-11-13 22:35:44 -06:00
|
|
|
/// assert!(it.sum() == 15);
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
fn sum(self) -> A;
|
2013-05-17 10:18:09 -05:00
|
|
|
}
|
|
|
|
|
2014-11-13 22:35:44 -06:00
|
|
|
macro_rules! impl_additive {
|
|
|
|
($A:ty, $init:expr) => {
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental = "trait is experimental"]
|
2014-11-13 22:35:44 -06:00
|
|
|
impl<T: Iterator<$A>> AdditiveIterator<$A> for T {
|
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
fn sum(self) -> $A {
|
2014-11-13 22:35:44 -06:00
|
|
|
self.fold($init, |acc, x| acc + x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2013-05-17 10:18:09 -05:00
|
|
|
}
|
2014-11-14 11:18:10 -06:00
|
|
|
impl_additive! { i8, 0 }
|
|
|
|
impl_additive! { i16, 0 }
|
|
|
|
impl_additive! { i32, 0 }
|
|
|
|
impl_additive! { i64, 0 }
|
|
|
|
impl_additive! { int, 0 }
|
|
|
|
impl_additive! { u8, 0 }
|
|
|
|
impl_additive! { u16, 0 }
|
|
|
|
impl_additive! { u32, 0 }
|
|
|
|
impl_additive! { u64, 0 }
|
|
|
|
impl_additive! { uint, 0 }
|
|
|
|
impl_additive! { f32, 0.0 }
|
|
|
|
impl_additive! { f64, 0.0 }
|
2013-05-17 10:18:09 -05:00
|
|
|
|
2014-09-23 22:59:26 -05:00
|
|
|
/// A trait for iterators over elements which can be multiplied together.
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental = "needs to be re-evaluated as part of numerics reform"]
|
2013-05-17 10:18:09 -05:00
|
|
|
pub trait MultiplicativeIterator<A> {
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Iterates over the entire iterator, multiplying all the elements
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2013-12-22 15:31:23 -06:00
|
|
|
/// use std::iter::{count, MultiplicativeIterator};
|
2013-05-28 16:35:52 -05:00
|
|
|
///
|
|
|
|
/// fn factorial(n: uint) -> uint {
|
2014-11-13 22:35:44 -06:00
|
|
|
/// count(1u, 1).take_while(|&i| i <= n).product()
|
2013-05-28 16:35:52 -05:00
|
|
|
/// }
|
|
|
|
/// assert!(factorial(0) == 1);
|
|
|
|
/// assert!(factorial(1) == 1);
|
|
|
|
/// assert!(factorial(5) == 120);
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
fn product(self) -> A;
|
2013-05-17 10:18:09 -05:00
|
|
|
}
|
|
|
|
|
2014-11-13 22:35:44 -06:00
|
|
|
macro_rules! impl_multiplicative {
|
|
|
|
($A:ty, $init:expr) => {
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental = "trait is experimental"]
|
2014-11-13 22:35:44 -06:00
|
|
|
impl<T: Iterator<$A>> MultiplicativeIterator<$A> for T {
|
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
fn product(self) -> $A {
|
2014-11-13 22:35:44 -06:00
|
|
|
self.fold($init, |acc, x| acc * x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2013-05-17 10:18:09 -05:00
|
|
|
}
|
2014-11-14 11:18:10 -06:00
|
|
|
impl_multiplicative! { i8, 1 }
|
|
|
|
impl_multiplicative! { i16, 1 }
|
|
|
|
impl_multiplicative! { i32, 1 }
|
|
|
|
impl_multiplicative! { i64, 1 }
|
|
|
|
impl_multiplicative! { int, 1 }
|
|
|
|
impl_multiplicative! { u8, 1 }
|
|
|
|
impl_multiplicative! { u16, 1 }
|
|
|
|
impl_multiplicative! { u32, 1 }
|
|
|
|
impl_multiplicative! { u64, 1 }
|
|
|
|
impl_multiplicative! { uint, 1 }
|
|
|
|
impl_multiplicative! { f32, 1.0 }
|
|
|
|
impl_multiplicative! { f64, 1.0 }
|
2013-05-17 10:18:09 -05:00
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// A trait for iterators over elements which can be compared to one another.
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "recently renamed for new extension trait conventions"]
|
|
|
|
pub trait IteratorOrdExt<A> {
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Consumes the entire iterator to return the maximum element.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = [1i, 2, 3, 4, 5];
|
2013-11-17 12:31:50 -06:00
|
|
|
/// assert!(a.iter().max().unwrap() == &5);
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
fn max(self) -> Option<A>;
|
2013-05-28 16:35:52 -05:00
|
|
|
|
|
|
|
/// Consumes the entire iterator to return the minimum element.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = [1i, 2, 3, 4, 5];
|
2013-11-17 12:31:50 -06:00
|
|
|
/// assert!(a.iter().min().unwrap() == &1);
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
fn min(self) -> Option<A>;
|
2014-01-25 00:40:54 -06:00
|
|
|
|
2014-02-17 05:53:45 -06:00
|
|
|
/// `min_max` finds the minimum and maximum elements in the iterator.
|
2014-01-25 00:40:54 -06:00
|
|
|
///
|
|
|
|
/// The return type `MinMaxResult` is an enum of three variants:
|
2014-05-22 07:50:31 -05:00
|
|
|
///
|
2014-01-25 00:40:54 -06:00
|
|
|
/// - `NoElements` if the iterator is empty.
|
|
|
|
/// - `OneElement(x)` if the iterator has exactly one element.
|
2014-05-22 07:50:31 -05:00
|
|
|
/// - `MinMax(x, y)` is returned otherwise, where `x <= y`. Two
|
|
|
|
/// values are equal if and only if there is more than one
|
|
|
|
/// element in the iterator and all elements are equal.
|
2014-01-25 00:40:54 -06:00
|
|
|
///
|
|
|
|
/// On an iterator of length `n`, `min_max` does `1.5 * n` comparisons,
|
2014-10-14 05:11:07 -05:00
|
|
|
/// and so is faster than calling `min` and `max` separately which does `2 * n` comparisons.
|
2014-01-25 00:40:54 -06:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::iter::{NoElements, OneElement, MinMax};
|
|
|
|
///
|
|
|
|
/// let v: [int, ..0] = [];
|
|
|
|
/// assert_eq!(v.iter().min_max(), NoElements);
|
|
|
|
///
|
|
|
|
/// let v = [1i];
|
|
|
|
/// assert!(v.iter().min_max() == OneElement(&1));
|
|
|
|
///
|
|
|
|
/// let v = [1i, 2, 3, 4, 5];
|
|
|
|
/// assert!(v.iter().min_max() == MinMax(&1, &5));
|
|
|
|
///
|
|
|
|
/// let v = [1i, 2, 3, 4, 5, 6];
|
|
|
|
/// assert!(v.iter().min_max() == MinMax(&1, &6));
|
|
|
|
///
|
|
|
|
/// let v = [1i, 1, 1, 1];
|
|
|
|
/// assert!(v.iter().min_max() == MinMax(&1, &1));
|
|
|
|
/// ```
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
fn min_max(self) -> MinMaxResult<A>;
|
2013-05-17 10:18:09 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
|
|
|
impl<A: Ord, T: Iterator<A>> IteratorOrdExt<A> for T {
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
fn max(self) -> Option<A> {
|
2013-05-17 10:18:09 -05:00
|
|
|
self.fold(None, |max, x| {
|
|
|
|
match max {
|
|
|
|
None => Some(x),
|
|
|
|
Some(y) => Some(cmp::max(x, y))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
fn min(self) -> Option<A> {
|
2013-05-17 10:18:09 -05:00
|
|
|
self.fold(None, |min, x| {
|
|
|
|
match min {
|
|
|
|
None => Some(x),
|
|
|
|
Some(y) => Some(cmp::min(x, y))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2014-01-25 00:40:54 -06:00
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
fn min_max(mut self) -> MinMaxResult<A> {
|
2014-01-25 00:40:54 -06:00
|
|
|
let (mut min, mut max) = match self.next() {
|
|
|
|
None => return NoElements,
|
|
|
|
Some(x) => {
|
|
|
|
match self.next() {
|
|
|
|
None => return OneElement(x),
|
|
|
|
Some(y) => if x < y {(x, y)} else {(y,x)}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
loop {
|
|
|
|
// `first` and `second` are the two next elements we want to look at.
|
|
|
|
// We first compare `first` and `second` (#1). The smaller one is then compared to
|
2014-04-20 23:49:39 -05:00
|
|
|
// current minimum (#2). The larger one is compared to current maximum (#3). This
|
2014-01-25 00:40:54 -06:00
|
|
|
// way we do 3 comparisons for 2 elements.
|
|
|
|
let first = match self.next() {
|
|
|
|
None => break,
|
|
|
|
Some(x) => x
|
|
|
|
};
|
|
|
|
let second = match self.next() {
|
|
|
|
None => {
|
|
|
|
if first < min {
|
|
|
|
min = first;
|
|
|
|
} else if first > max {
|
|
|
|
max = first;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Some(x) => x
|
|
|
|
};
|
|
|
|
if first < second {
|
|
|
|
if first < min {min = first;}
|
|
|
|
if max < second {max = second;}
|
|
|
|
} else {
|
|
|
|
if second < min {min = second;}
|
|
|
|
if max < first {max = first;}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MinMax(min, max)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
/// `MinMaxResult` is an enum returned by `min_max`. See `IteratorOrdExt::min_max` for more detail.
|
2014-05-29 19:45:07 -05:00
|
|
|
#[deriving(Clone, PartialEq, Show)]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "waiting on namespaced enum conventions"]
|
2014-01-25 00:40:54 -06:00
|
|
|
pub enum MinMaxResult<T> {
|
|
|
|
/// Empty iterator
|
|
|
|
NoElements,
|
|
|
|
|
|
|
|
/// Iterator with one element, so the minimum and maximum are the same
|
|
|
|
OneElement(T),
|
|
|
|
|
|
|
|
/// More than one element in the iterator, the first element is not larger than the second
|
|
|
|
MinMax(T, T)
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2014-01-25 00:40:54 -06:00
|
|
|
impl<T: Clone> MinMaxResult<T> {
|
|
|
|
/// `into_option` creates an `Option` of type `(T,T)`. The returned `Option` has variant
|
|
|
|
/// `None` if and only if the `MinMaxResult` has variant `NoElements`. Otherwise variant
|
|
|
|
/// `Some(x,y)` is returned where `x <= y`. If `MinMaxResult` has variant `OneElement(x)`,
|
|
|
|
/// performing this operation will make one clone of `x`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::iter::{NoElements, OneElement, MinMax, MinMaxResult};
|
|
|
|
///
|
|
|
|
/// let r: MinMaxResult<int> = NoElements;
|
2014-11-14 11:18:10 -06:00
|
|
|
/// assert_eq!(r.into_option(), None);
|
2014-01-25 00:40:54 -06:00
|
|
|
///
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let r = OneElement(1i);
|
2014-01-25 00:40:54 -06:00
|
|
|
/// assert_eq!(r.into_option(), Some((1,1)));
|
|
|
|
///
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let r = MinMax(1i,2i);
|
2014-01-25 00:40:54 -06:00
|
|
|
/// assert_eq!(r.into_option(), Some((1,2)));
|
|
|
|
/// ```
|
|
|
|
pub fn into_option(self) -> Option<(T,T)> {
|
|
|
|
match self {
|
|
|
|
NoElements => None,
|
|
|
|
OneElement(x) => Some((x.clone(), x)),
|
|
|
|
MinMax(x, y) => Some((x, y))
|
|
|
|
}
|
|
|
|
}
|
2013-05-17 10:18:09 -05:00
|
|
|
}
|
|
|
|
|
2014-11-17 20:00:07 -06:00
|
|
|
/// A trait for iterators that contain cloneable elements
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "recently renamed for extension trait conventions"]
|
|
|
|
pub trait IteratorCloneExt<A> {
|
2014-11-17 20:00:07 -06:00
|
|
|
/// Creates an iterator that clones the elements it yields. Useful for converting an
|
|
|
|
/// Iterator<&T> to an Iterator<T>.
|
|
|
|
fn cloned(self) -> Cloned<Self>;
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
|
|
|
impl<A: Clone, D: Deref<A>, I: Iterator<D>> IteratorCloneExt<A> for I {
|
2014-11-17 20:00:07 -06:00
|
|
|
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> {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
|
|
|
impl<A: Clone, D: Deref<A>, I: ExactSizeIterator<D>> ExactSizeIterator<A> for Cloned<I> {}
|
2014-11-17 20:00:07 -06:00
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "recently renamed for extension trait conventions"]
|
|
|
|
/// An extension trait for cloneable iterators.
|
|
|
|
pub trait CloneIteratorExt {
|
2013-07-18 10:09:08 -05:00
|
|
|
/// Repeats an iterator endlessly
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```rust
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
/// use std::iter::{CloneIteratorExt, count};
|
2013-12-22 15:31:23 -06:00
|
|
|
///
|
2014-04-21 16:58:52 -05:00
|
|
|
/// let a = count(1i,1i).take(1);
|
2013-07-18 10:09:08 -05:00
|
|
|
/// let mut cy = a.cycle();
|
|
|
|
/// assert_eq!(cy.next(), Some(1));
|
|
|
|
/// assert_eq!(cy.next(), Some(1));
|
2013-09-23 19:20:36 -05:00
|
|
|
/// ```
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2013-07-27 16:41:20 -05:00
|
|
|
fn cycle(self) -> Cycle<Self>;
|
2013-07-18 10:09:08 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
impl<A, I> CloneIteratorExt for I where I: Iterator<A> + Clone {
|
2013-07-18 10:09:08 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
fn cycle(self) -> Cycle<I> {
|
2013-07-27 16:41:20 -05:00
|
|
|
Cycle{orig: self.clone(), iter: self}
|
2013-07-18 10:09:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An iterator that repeats endlessly
|
2014-12-14 21:35:22 -06:00
|
|
|
#[deriving(Clone, Copy)]
|
2014-07-09 07:13:14 -05:00
|
|
|
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2013-07-27 16:41:20 -05:00
|
|
|
pub struct Cycle<T> {
|
2014-03-27 17:09:47 -05:00
|
|
|
orig: T,
|
|
|
|
iter: T,
|
2013-07-18 10:09:08 -05:00
|
|
|
}
|
|
|
|
|
2013-07-27 16:41:20 -05:00
|
|
|
impl<A, T: Clone + Iterator<A>> Iterator<A> for Cycle<T> {
|
2013-07-18 10:09:08 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> {
|
|
|
|
match self.iter.next() {
|
|
|
|
None => { self.iter = self.orig.clone(); self.iter.next() }
|
|
|
|
y => y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
|
|
|
// the cycle iterator is either empty or infinite
|
|
|
|
match self.orig.size_hint() {
|
|
|
|
sz @ (0, Some(0)) => sz,
|
|
|
|
(0, _) => (0, None),
|
2014-01-25 01:37:51 -06:00
|
|
|
_ => (uint::MAX, None)
|
2013-07-18 10:09:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental = "trait is experimental"]
|
2013-07-29 10:44:45 -05:00
|
|
|
impl<A, T: Clone + RandomAccessIterator<A>> RandomAccessIterator<A> for Cycle<T> {
|
|
|
|
#[inline]
|
|
|
|
fn indexable(&self) -> uint {
|
|
|
|
if self.orig.indexable() > 0 {
|
2014-01-25 01:37:51 -06:00
|
|
|
uint::MAX
|
2013-07-29 10:44:45 -05:00
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-04-22 00:15:42 -05:00
|
|
|
fn idx(&mut self, index: uint) -> Option<A> {
|
2013-07-29 10:44:45 -05:00
|
|
|
let liter = self.iter.indexable();
|
|
|
|
let lorig = self.orig.indexable();
|
|
|
|
if lorig == 0 {
|
|
|
|
None
|
|
|
|
} else if index < liter {
|
|
|
|
self.iter.idx(index)
|
|
|
|
} else {
|
|
|
|
self.orig.idx((index - liter) % lorig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-15 03:22:49 -06:00
|
|
|
/// An iterator that strings two iterators together
|
2013-07-18 10:31:33 -05:00
|
|
|
#[deriving(Clone)]
|
2014-07-09 07:13:14 -05:00
|
|
|
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2013-07-27 16:41:20 -05:00
|
|
|
pub struct Chain<T, U> {
|
2014-03-27 17:09:47 -05:00
|
|
|
a: T,
|
|
|
|
b: U,
|
2014-05-01 20:06:59 -05:00
|
|
|
flag: bool,
|
2013-04-19 10:29:38 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2013-07-27 16:41:20 -05:00
|
|
|
impl<A, T: Iterator<A>, U: Iterator<A>> Iterator<A> for Chain<T, U> {
|
2013-04-19 10:29:38 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> {
|
|
|
|
if self.flag {
|
|
|
|
self.b.next()
|
|
|
|
} else {
|
|
|
|
match self.a.next() {
|
|
|
|
Some(x) => return Some(x),
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
self.flag = true;
|
|
|
|
self.b.next()
|
|
|
|
}
|
|
|
|
}
|
2013-06-21 05:12:01 -05:00
|
|
|
|
|
|
|
#[inline]
|
2013-07-02 20:40:46 -05:00
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
2013-06-21 05:12:01 -05:00
|
|
|
let (a_lower, a_upper) = self.a.size_hint();
|
|
|
|
let (b_lower, b_upper) = self.b.size_hint();
|
|
|
|
|
2013-08-05 22:04:52 -05:00
|
|
|
let lower = a_lower.saturating_add(b_lower);
|
2013-06-21 05:12:01 -05:00
|
|
|
|
|
|
|
let upper = match (a_upper, b_upper) {
|
2014-11-09 07:11:28 -06:00
|
|
|
(Some(x), Some(y)) => x.checked_add(y),
|
2013-06-21 05:12:01 -05:00
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
|
|
|
|
(lower, upper)
|
|
|
|
}
|
2013-04-19 10:29:38 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2013-07-18 18:50:51 -05:00
|
|
|
impl<A, T: DoubleEndedIterator<A>, U: DoubleEndedIterator<A>> DoubleEndedIterator<A>
|
2013-07-27 16:41:20 -05:00
|
|
|
for Chain<T, U> {
|
2013-07-18 18:50:51 -05:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<A> {
|
|
|
|
match self.b.next_back() {
|
|
|
|
Some(x) => Some(x),
|
|
|
|
None => self.a.next_back()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental = "trait is experimental"]
|
2013-07-22 19:11:24 -05:00
|
|
|
impl<A, T: RandomAccessIterator<A>, U: RandomAccessIterator<A>> RandomAccessIterator<A>
|
2013-07-27 16:41:20 -05:00
|
|
|
for Chain<T, U> {
|
2013-07-22 19:11:24 -05:00
|
|
|
#[inline]
|
|
|
|
fn indexable(&self) -> uint {
|
|
|
|
let (a, b) = (self.a.indexable(), self.b.indexable());
|
2013-08-05 22:04:52 -05:00
|
|
|
a.saturating_add(b)
|
2013-07-22 19:11:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-04-22 00:15:42 -05:00
|
|
|
fn idx(&mut self, index: uint) -> Option<A> {
|
2013-07-22 19:11:24 -05:00
|
|
|
let len = self.a.indexable();
|
|
|
|
if index < len {
|
|
|
|
self.a.idx(index)
|
|
|
|
} else {
|
|
|
|
self.b.idx(index - len)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-15 03:22:49 -06:00
|
|
|
/// An iterator that iterates two other iterators simultaneously
|
2013-07-18 10:31:33 -05:00
|
|
|
#[deriving(Clone)]
|
2014-07-09 07:13:14 -05:00
|
|
|
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2013-07-27 16:41:20 -05:00
|
|
|
pub struct Zip<T, U> {
|
2014-03-27 17:09:47 -05:00
|
|
|
a: T,
|
|
|
|
b: U
|
2013-04-09 09:54:32 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2013-07-27 16:41:20 -05:00
|
|
|
impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<(A, B)> for Zip<T, U> {
|
2013-04-09 09:54:32 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<(A, B)> {
|
2013-08-03 17:41:29 -05:00
|
|
|
match self.a.next() {
|
|
|
|
None => None,
|
|
|
|
Some(x) => match self.b.next() {
|
|
|
|
None => None,
|
|
|
|
Some(y) => Some((x, y))
|
|
|
|
}
|
2013-04-09 09:54:32 -05:00
|
|
|
}
|
|
|
|
}
|
2013-07-02 22:59:26 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
|
|
|
let (a_lower, a_upper) = self.a.size_hint();
|
|
|
|
let (b_lower, b_upper) = self.b.size_hint();
|
|
|
|
|
|
|
|
let lower = cmp::min(a_lower, b_lower);
|
|
|
|
|
|
|
|
let upper = match (a_upper, b_upper) {
|
|
|
|
(Some(x), Some(y)) => Some(cmp::min(x,y)),
|
|
|
|
(Some(x), None) => Some(x),
|
|
|
|
(None, Some(y)) => Some(y),
|
|
|
|
(None, None) => None
|
|
|
|
};
|
|
|
|
|
|
|
|
(lower, upper)
|
|
|
|
}
|
2013-04-09 09:54:32 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
|
|
|
impl<A, B, T: ExactSizeIterator<A>, U: ExactSizeIterator<B>> DoubleEndedIterator<(A, B)>
|
2013-08-30 13:00:07 -05:00
|
|
|
for Zip<T, U> {
|
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<(A, B)> {
|
2014-08-24 08:04:28 -05:00
|
|
|
let a_sz = self.a.len();
|
|
|
|
let b_sz = self.b.len();
|
|
|
|
if a_sz != b_sz {
|
|
|
|
// Adjust a, b to equal length
|
|
|
|
if a_sz > b_sz {
|
|
|
|
for _ in range(0, a_sz - b_sz) { self.a.next_back(); }
|
|
|
|
} else {
|
|
|
|
for _ in range(0, b_sz - a_sz) { self.b.next_back(); }
|
|
|
|
}
|
2013-08-30 13:00:07 -05:00
|
|
|
}
|
|
|
|
match (self.a.next_back(), self.b.next_back()) {
|
|
|
|
(Some(x), Some(y)) => Some((x, y)),
|
2014-08-24 08:04:28 -05:00
|
|
|
(None, None) => None,
|
|
|
|
_ => unreachable!(),
|
2013-08-30 13:00:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental = "trait is experimental"]
|
2013-07-29 10:44:45 -05:00
|
|
|
impl<A, B, T: RandomAccessIterator<A>, U: RandomAccessIterator<B>>
|
|
|
|
RandomAccessIterator<(A, B)> for Zip<T, U> {
|
|
|
|
#[inline]
|
|
|
|
fn indexable(&self) -> uint {
|
|
|
|
cmp::min(self.a.indexable(), self.b.indexable())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-04-22 00:15:42 -05:00
|
|
|
fn idx(&mut self, index: uint) -> Option<(A, B)> {
|
2013-08-03 17:41:29 -05:00
|
|
|
match self.a.idx(index) {
|
|
|
|
None => None,
|
|
|
|
Some(x) => match self.b.idx(index) {
|
|
|
|
None => None,
|
|
|
|
Some(y) => Some((x, y))
|
|
|
|
}
|
2013-07-29 10:44:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-15 03:22:49 -06:00
|
|
|
/// An iterator that maps the values of `iter` with `f`
|
2014-07-09 07:13:14 -05:00
|
|
|
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2014-12-02 12:59:08 -06:00
|
|
|
pub struct Map<A, B, I: Iterator<A>, F: FnMut(A) -> B> {
|
|
|
|
iter: I,
|
|
|
|
f: F,
|
2013-04-19 08:18:22 -05:00
|
|
|
}
|
|
|
|
|
2014-12-13 21:04:23 -06:00
|
|
|
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
|
2014-12-20 02:35:06 -06:00
|
|
|
#[stable]
|
2014-12-13 21:04:23 -06:00
|
|
|
impl<A, B, I, F> Clone for Map<A, B, I, F> where
|
|
|
|
I: Clone + Iterator<A>,
|
|
|
|
F: Clone + FnMut(A) -> B,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Map<A, B, I, F> {
|
|
|
|
Map {
|
|
|
|
iter: self.iter.clone(),
|
|
|
|
f: self.f.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-02 12:59:08 -06:00
|
|
|
impl<A, B, I, F> Map<A, B, I, F> where I: Iterator<A>, F: FnMut(A) -> B {
|
2013-04-19 08:18:22 -05:00
|
|
|
#[inline]
|
2014-04-22 01:25:18 -05:00
|
|
|
fn do_map(&mut self, elt: Option<A>) -> Option<B> {
|
2013-07-29 10:44:45 -05:00
|
|
|
match elt {
|
2013-04-19 08:18:22 -05:00
|
|
|
Some(a) => Some((self.f)(a)),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
2013-07-29 10:44:45 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-12-02 12:59:08 -06:00
|
|
|
impl<A, B, I, F> Iterator<B> for Map<A, B, I, F> where I: Iterator<A>, F: FnMut(A) -> B {
|
2013-07-29 10:44:45 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<B> {
|
|
|
|
let next = self.iter.next();
|
|
|
|
self.do_map(next)
|
|
|
|
}
|
2013-06-21 05:12:01 -05:00
|
|
|
|
|
|
|
#[inline]
|
2013-07-02 20:40:46 -05:00
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
2013-06-21 05:12:01 -05:00
|
|
|
self.iter.size_hint()
|
|
|
|
}
|
2013-04-19 08:18:22 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-12-02 12:59:08 -06:00
|
|
|
impl<A, B, I, F> DoubleEndedIterator<B> for Map<A, B, I, F> where
|
|
|
|
I: DoubleEndedIterator<A>,
|
|
|
|
F: FnMut(A) -> B,
|
|
|
|
{
|
2013-07-18 18:50:51 -05:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<B> {
|
2013-07-29 10:44:45 -05:00
|
|
|
let next = self.iter.next_back();
|
|
|
|
self.do_map(next)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental = "trait is experimental"]
|
2014-12-02 12:59:08 -06:00
|
|
|
impl<A, B, I, F> RandomAccessIterator<B> for Map<A, B, I, F> where
|
|
|
|
I: RandomAccessIterator<A>,
|
|
|
|
F: FnMut(A) -> B,
|
|
|
|
{
|
2013-07-29 10:44:45 -05:00
|
|
|
#[inline]
|
|
|
|
fn indexable(&self) -> uint {
|
|
|
|
self.iter.indexable()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-04-22 00:15:42 -05:00
|
|
|
fn idx(&mut self, index: uint) -> Option<B> {
|
|
|
|
let elt = self.iter.idx(index);
|
|
|
|
self.do_map(elt)
|
2013-07-18 18:50:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-15 03:22:49 -06:00
|
|
|
/// An iterator that filters the elements of `iter` with `predicate`
|
2014-07-09 07:13:14 -05:00
|
|
|
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2014-12-02 16:49:42 -06:00
|
|
|
pub struct Filter<A, I, P> where I: Iterator<A>, P: FnMut(&A) -> bool {
|
|
|
|
iter: I,
|
|
|
|
predicate: P,
|
2013-04-09 09:54:32 -05:00
|
|
|
}
|
|
|
|
|
2014-12-13 21:04:23 -06:00
|
|
|
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
|
2014-12-20 02:35:06 -06:00
|
|
|
#[stable]
|
2014-12-13 21:04:23 -06:00
|
|
|
impl<A, I, P> Clone for Filter<A, I, P> where
|
|
|
|
I: Clone + Iterator<A>,
|
|
|
|
P: Clone + FnMut(&A) -> bool,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Filter<A, I, P> {
|
|
|
|
Filter {
|
|
|
|
iter: self.iter.clone(),
|
|
|
|
predicate: self.predicate.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-12-02 16:49:42 -06:00
|
|
|
impl<A, I, P> Iterator<A> for Filter<A, I, P> where I: Iterator<A>, P: FnMut(&A) -> bool {
|
2013-04-09 09:54:32 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> {
|
2013-08-03 11:45:23 -05:00
|
|
|
for x in self.iter {
|
2013-04-09 09:54:32 -05:00
|
|
|
if (self.predicate)(&x) {
|
|
|
|
return Some(x);
|
|
|
|
} else {
|
2013-10-01 16:31:03 -05:00
|
|
|
continue
|
2013-04-09 09:54:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2013-06-21 05:12:01 -05:00
|
|
|
|
|
|
|
#[inline]
|
2013-07-02 20:40:46 -05:00
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
2013-06-21 05:12:01 -05:00
|
|
|
let (_, upper) = self.iter.size_hint();
|
2013-07-02 20:40:46 -05:00
|
|
|
(0, upper) // can't know a lower bound, due to the predicate
|
2013-06-21 05:12:01 -05:00
|
|
|
}
|
2013-04-09 09:54:32 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-12-02 16:49:42 -06:00
|
|
|
impl<A, I, P> DoubleEndedIterator<A> for Filter<A, I, P> where
|
|
|
|
I: DoubleEndedIterator<A>,
|
|
|
|
P: FnMut(&A) -> bool,
|
|
|
|
{
|
2013-07-18 18:50:51 -05:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<A> {
|
2014-08-06 05:20:37 -05:00
|
|
|
for x in self.iter.by_ref().rev() {
|
|
|
|
if (self.predicate)(&x) {
|
|
|
|
return Some(x);
|
2013-07-18 18:50:51 -05:00
|
|
|
}
|
|
|
|
}
|
2014-08-06 05:20:37 -05:00
|
|
|
None
|
2013-07-18 18:50:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-15 03:22:49 -06:00
|
|
|
/// An iterator that uses `f` to both filter and map elements from `iter`
|
2014-07-09 07:13:14 -05:00
|
|
|
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2014-12-02 20:00:07 -06:00
|
|
|
pub struct FilterMap<A, B, I, F> where I: Iterator<A>, F: FnMut(A) -> Option<B> {
|
|
|
|
iter: I,
|
|
|
|
f: F,
|
2013-05-17 09:00:48 -05:00
|
|
|
}
|
|
|
|
|
2014-12-13 21:04:23 -06:00
|
|
|
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
|
2014-12-20 02:35:06 -06:00
|
|
|
#[stable]
|
2014-12-13 21:04:23 -06:00
|
|
|
impl<A, B, I, F> Clone for FilterMap<A, B, I, F> where
|
|
|
|
I: Clone + Iterator<A>,
|
|
|
|
F: Clone + FnMut(A) -> Option<B>,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> FilterMap<A, B, I, F> {
|
|
|
|
FilterMap {
|
|
|
|
iter: self.iter.clone(),
|
|
|
|
f: self.f.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-12-02 20:00:07 -06:00
|
|
|
impl<A, B, I, F> Iterator<B> for FilterMap<A, B, I, F> where
|
|
|
|
I: Iterator<A>,
|
|
|
|
F: FnMut(A) -> Option<B>,
|
|
|
|
{
|
2013-05-17 09:00:48 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<B> {
|
2013-08-03 11:45:23 -05:00
|
|
|
for x in self.iter {
|
2013-05-18 03:48:22 -05:00
|
|
|
match (self.f)(x) {
|
|
|
|
Some(y) => return Some(y),
|
|
|
|
None => ()
|
2013-05-17 09:00:48 -05:00
|
|
|
}
|
|
|
|
}
|
2013-05-18 03:48:22 -05:00
|
|
|
None
|
2013-05-17 09:00:48 -05:00
|
|
|
}
|
2013-06-21 05:12:01 -05:00
|
|
|
|
|
|
|
#[inline]
|
2013-07-02 20:40:46 -05:00
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
2013-06-21 05:12:01 -05:00
|
|
|
let (_, upper) = self.iter.size_hint();
|
2013-07-02 20:40:46 -05:00
|
|
|
(0, upper) // can't know a lower bound, due to the predicate
|
2013-06-21 05:12:01 -05:00
|
|
|
}
|
2013-05-17 09:00:48 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-12-02 20:00:07 -06:00
|
|
|
impl<A, B, I, F> DoubleEndedIterator<B> for FilterMap<A, B, I, F> where
|
|
|
|
I: DoubleEndedIterator<A>,
|
|
|
|
F: FnMut(A) -> Option<B>,
|
|
|
|
{
|
2013-07-18 18:50:51 -05:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<B> {
|
2014-08-06 05:20:37 -05:00
|
|
|
for x in self.iter.by_ref().rev() {
|
|
|
|
match (self.f)(x) {
|
|
|
|
Some(y) => return Some(y),
|
|
|
|
None => ()
|
2013-07-18 18:50:51 -05:00
|
|
|
}
|
|
|
|
}
|
2014-08-06 05:20:37 -05:00
|
|
|
None
|
2013-07-18 18:50:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-15 03:22:49 -06:00
|
|
|
/// An iterator that yields the current count and the element during iteration
|
2013-07-18 10:31:33 -05:00
|
|
|
#[deriving(Clone)]
|
2014-07-09 07:13:14 -05:00
|
|
|
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2013-07-27 16:41:20 -05:00
|
|
|
pub struct Enumerate<T> {
|
2014-03-27 17:09:47 -05:00
|
|
|
iter: T,
|
|
|
|
count: uint
|
2013-04-18 04:34:07 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2013-07-27 16:41:20 -05:00
|
|
|
impl<A, T: Iterator<A>> Iterator<(uint, A)> for Enumerate<T> {
|
2013-04-18 04:34:07 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<(uint, A)> {
|
|
|
|
match self.iter.next() {
|
|
|
|
Some(a) => {
|
|
|
|
let ret = Some((self.count, a));
|
|
|
|
self.count += 1;
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
2013-07-02 22:59:26 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
|
|
|
self.iter.size_hint()
|
|
|
|
}
|
2013-04-18 04:34:07 -05:00
|
|
|
}
|
2013-04-18 07:15:40 -05:00
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
|
|
|
impl<A, T: ExactSizeIterator<A>> DoubleEndedIterator<(uint, A)> for Enumerate<T> {
|
2013-08-30 12:59:49 -05:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<(uint, A)> {
|
|
|
|
match self.iter.next_back() {
|
|
|
|
Some(a) => {
|
2014-08-24 08:04:28 -05:00
|
|
|
let len = self.iter.len();
|
|
|
|
Some((self.count + len, a))
|
2013-08-30 12:59:49 -05:00
|
|
|
}
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental = "trait is experimental"]
|
2013-07-29 10:44:45 -05:00
|
|
|
impl<A, T: RandomAccessIterator<A>> RandomAccessIterator<(uint, A)> for Enumerate<T> {
|
|
|
|
#[inline]
|
|
|
|
fn indexable(&self) -> uint {
|
|
|
|
self.iter.indexable()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-04-22 00:15:42 -05:00
|
|
|
fn idx(&mut self, index: uint) -> Option<(uint, A)> {
|
2013-07-29 10:44:45 -05:00
|
|
|
match self.iter.idx(index) {
|
|
|
|
Some(a) => Some((self.count + index, a)),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-08 14:46:36 -05:00
|
|
|
/// An iterator with a `peek()` that returns an optional reference to the next element.
|
2014-07-09 07:13:14 -05:00
|
|
|
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2014-12-14 21:35:22 -06:00
|
|
|
#[deriving(Copy)]
|
2013-08-08 14:46:36 -05:00
|
|
|
pub struct Peekable<A, T> {
|
2014-03-27 17:09:47 -05:00
|
|
|
iter: T,
|
|
|
|
peeked: Option<A>,
|
2013-08-08 14:46:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, T: Iterator<A>> Iterator<A> for Peekable<A, T> {
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> {
|
|
|
|
if self.peeked.is_some() { self.peeked.take() }
|
|
|
|
else { self.iter.next() }
|
|
|
|
}
|
2013-08-18 23:47:43 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
|
|
|
let (lo, hi) = self.iter.size_hint();
|
|
|
|
if self.peeked.is_some() {
|
|
|
|
let lo = lo.saturating_add(1);
|
|
|
|
let hi = match hi {
|
2014-11-09 07:11:28 -06:00
|
|
|
Some(x) => x.checked_add(1),
|
2013-08-18 23:47:43 -05:00
|
|
|
None => None
|
|
|
|
};
|
|
|
|
(lo, hi)
|
|
|
|
} else {
|
|
|
|
(lo, hi)
|
|
|
|
}
|
|
|
|
}
|
2013-08-08 14:46:36 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a, A, T: Iterator<A>> Peekable<A, T> {
|
2013-08-08 14:46:36 -05:00
|
|
|
/// Return a reference to the next element of the iterator with out advancing it,
|
|
|
|
/// or None if the iterator is exhausted.
|
|
|
|
#[inline]
|
2013-12-10 01:16:18 -06:00
|
|
|
pub fn peek(&'a mut self) -> Option<&'a A> {
|
2013-08-18 23:47:43 -05:00
|
|
|
if self.peeked.is_none() {
|
|
|
|
self.peeked = self.iter.next();
|
|
|
|
}
|
2013-08-08 14:46:36 -05:00
|
|
|
match self.peeked {
|
|
|
|
Some(ref value) => Some(value),
|
2013-08-18 23:47:43 -05:00
|
|
|
None => None,
|
2013-08-08 14:46:36 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-11 04:13:06 -06:00
|
|
|
|
|
|
|
/// Check whether peekable iterator is empty or not.
|
|
|
|
#[inline]
|
2014-01-14 08:38:06 -06:00
|
|
|
pub fn is_empty(&mut self) -> bool {
|
2014-01-25 01:44:06 -06:00
|
|
|
self.peek().is_none()
|
2014-01-11 04:13:06 -06:00
|
|
|
}
|
2013-08-08 14:46:36 -05:00
|
|
|
}
|
|
|
|
|
2014-12-15 03:22:49 -06:00
|
|
|
/// An iterator that rejects elements while `predicate` is true
|
2014-07-09 07:13:14 -05:00
|
|
|
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2014-12-02 22:28:32 -06:00
|
|
|
pub struct SkipWhile<A, I, P> where I: Iterator<A>, P: FnMut(&A) -> bool {
|
|
|
|
iter: I,
|
2014-03-27 17:09:47 -05:00
|
|
|
flag: bool,
|
2014-12-02 22:28:32 -06:00
|
|
|
predicate: P,
|
2013-04-18 07:15:40 -05:00
|
|
|
}
|
|
|
|
|
2014-12-13 21:04:23 -06:00
|
|
|
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
|
2014-12-20 02:35:06 -06:00
|
|
|
#[stable]
|
2014-12-13 21:04:23 -06:00
|
|
|
impl<A, I, P> Clone for SkipWhile<A, I, P> where
|
|
|
|
I: Clone + Iterator<A>,
|
|
|
|
P: Clone + FnMut(&A) -> bool,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> SkipWhile<A, I, P> {
|
|
|
|
SkipWhile {
|
|
|
|
iter: self.iter.clone(),
|
|
|
|
flag: self.flag,
|
|
|
|
predicate: self.predicate.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-12-02 22:28:32 -06:00
|
|
|
impl<A, I, P> Iterator<A> for SkipWhile<A, I, P> where I: Iterator<A>, P: FnMut(&A) -> bool {
|
2013-04-18 07:15:40 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> {
|
2014-08-06 05:20:37 -05:00
|
|
|
for x in self.iter {
|
|
|
|
if self.flag || !(self.predicate)(&x) {
|
|
|
|
self.flag = true;
|
|
|
|
return Some(x);
|
2013-04-18 07:15:40 -05:00
|
|
|
}
|
|
|
|
}
|
2014-08-06 05:20:37 -05:00
|
|
|
None
|
2013-04-18 07:15:40 -05:00
|
|
|
}
|
2013-07-02 22:59:26 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
|
|
|
let (_, upper) = self.iter.size_hint();
|
|
|
|
(0, upper) // can't know a lower bound, due to the predicate
|
|
|
|
}
|
2013-04-18 07:15:40 -05:00
|
|
|
}
|
|
|
|
|
2014-12-15 03:22:49 -06:00
|
|
|
/// An iterator that only accepts elements while `predicate` is true
|
2014-07-09 07:13:14 -05:00
|
|
|
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2014-12-02 23:42:28 -06:00
|
|
|
pub struct TakeWhile<A, I, P> where I: Iterator<A>, P: FnMut(&A) -> bool {
|
|
|
|
iter: I,
|
2014-03-27 17:09:47 -05:00
|
|
|
flag: bool,
|
2014-12-02 23:42:28 -06:00
|
|
|
predicate: P,
|
2013-04-18 07:15:40 -05:00
|
|
|
}
|
|
|
|
|
2014-12-13 21:04:23 -06:00
|
|
|
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
|
2014-12-20 02:35:06 -06:00
|
|
|
#[stable]
|
2014-12-13 21:04:23 -06:00
|
|
|
impl<A, I, P> Clone for TakeWhile<A, I, P> where
|
|
|
|
I: Clone + Iterator<A>,
|
|
|
|
P: Clone + FnMut(&A) -> bool,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> TakeWhile<A, I, P> {
|
|
|
|
TakeWhile {
|
|
|
|
iter: self.iter.clone(),
|
|
|
|
flag: self.flag,
|
|
|
|
predicate: self.predicate.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-12-02 23:42:28 -06:00
|
|
|
impl<A, I, P> Iterator<A> for TakeWhile<A, I, P> where I: Iterator<A>, P: FnMut(&A) -> bool {
|
2013-04-18 07:15:40 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> {
|
|
|
|
if self.flag {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
match self.iter.next() {
|
|
|
|
Some(x) => {
|
|
|
|
if (self.predicate)(&x) {
|
|
|
|
Some(x)
|
|
|
|
} else {
|
|
|
|
self.flag = true;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-02 22:59:26 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
|
|
|
let (_, upper) = self.iter.size_hint();
|
|
|
|
(0, upper) // can't know a lower bound, due to the predicate
|
|
|
|
}
|
2013-04-18 07:15:40 -05:00
|
|
|
}
|
2013-04-19 05:06:33 -05:00
|
|
|
|
2014-12-15 03:22:49 -06:00
|
|
|
/// An iterator that skips over `n` elements of `iter`.
|
2013-07-18 10:31:33 -05:00
|
|
|
#[deriving(Clone)]
|
2014-07-09 07:13:14 -05:00
|
|
|
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2013-07-27 16:41:20 -05:00
|
|
|
pub struct Skip<T> {
|
2014-03-27 17:09:47 -05:00
|
|
|
iter: T,
|
|
|
|
n: uint
|
2013-04-19 05:06:33 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2013-07-27 16:41:20 -05:00
|
|
|
impl<A, T: Iterator<A>> Iterator<A> for Skip<T> {
|
2013-04-19 05:06:33 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> {
|
|
|
|
let mut next = self.iter.next();
|
|
|
|
if self.n == 0 {
|
|
|
|
next
|
|
|
|
} else {
|
2013-07-31 21:18:19 -05:00
|
|
|
let mut n = self.n;
|
|
|
|
while n > 0 {
|
|
|
|
n -= 1;
|
2013-04-19 05:06:33 -05:00
|
|
|
match next {
|
|
|
|
Some(_) => {
|
|
|
|
next = self.iter.next();
|
2013-10-01 16:31:03 -05:00
|
|
|
continue
|
2013-04-19 05:06:33 -05:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
self.n = 0;
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.n = 0;
|
|
|
|
next
|
|
|
|
}
|
|
|
|
}
|
2013-07-02 22:59:26 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
|
|
|
let (lower, upper) = self.iter.size_hint();
|
|
|
|
|
2013-08-05 22:04:52 -05:00
|
|
|
let lower = lower.saturating_sub(self.n);
|
2013-07-02 22:59:26 -05:00
|
|
|
|
|
|
|
let upper = match upper {
|
2013-08-05 22:04:52 -05:00
|
|
|
Some(x) => Some(x.saturating_sub(self.n)),
|
2013-07-02 22:59:26 -05:00
|
|
|
None => None
|
|
|
|
};
|
|
|
|
|
|
|
|
(lower, upper)
|
|
|
|
}
|
2013-04-19 05:06:33 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental = "trait is experimental"]
|
2013-07-29 10:44:45 -05:00
|
|
|
impl<A, T: RandomAccessIterator<A>> RandomAccessIterator<A> for Skip<T> {
|
|
|
|
#[inline]
|
|
|
|
fn indexable(&self) -> uint {
|
2013-08-05 22:04:52 -05:00
|
|
|
self.iter.indexable().saturating_sub(self.n)
|
2013-07-29 10:44:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-04-22 00:15:42 -05:00
|
|
|
fn idx(&mut self, index: uint) -> Option<A> {
|
2013-07-29 10:44:45 -05:00
|
|
|
if index >= self.indexable() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
self.iter.idx(index + self.n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-15 03:22:49 -06:00
|
|
|
/// An iterator that only iterates over the first `n` iterations of `iter`.
|
2013-07-18 10:31:33 -05:00
|
|
|
#[deriving(Clone)]
|
2014-07-09 07:13:14 -05:00
|
|
|
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2013-07-27 16:41:20 -05:00
|
|
|
pub struct Take<T> {
|
2014-03-27 17:09:47 -05:00
|
|
|
iter: T,
|
|
|
|
n: uint
|
2013-04-19 05:06:33 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2013-07-27 16:41:20 -05:00
|
|
|
impl<A, T: Iterator<A>> Iterator<A> for Take<T> {
|
2013-04-19 05:06:33 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> {
|
|
|
|
if self.n != 0 {
|
|
|
|
self.n -= 1;
|
2013-08-05 18:23:29 -05:00
|
|
|
self.iter.next()
|
2013-04-19 05:06:33 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2013-07-02 22:59:26 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
|
|
|
let (lower, upper) = self.iter.size_hint();
|
|
|
|
|
|
|
|
let lower = cmp::min(lower, self.n);
|
|
|
|
|
|
|
|
let upper = match upper {
|
|
|
|
Some(x) if x < self.n => Some(x),
|
|
|
|
_ => Some(self.n)
|
|
|
|
};
|
|
|
|
|
|
|
|
(lower, upper)
|
|
|
|
}
|
2013-04-19 05:06:33 -05:00
|
|
|
}
|
2013-04-19 06:30:22 -05:00
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental = "trait is experimental"]
|
2013-07-29 10:44:45 -05:00
|
|
|
impl<A, T: RandomAccessIterator<A>> RandomAccessIterator<A> for Take<T> {
|
|
|
|
#[inline]
|
|
|
|
fn indexable(&self) -> uint {
|
|
|
|
cmp::min(self.iter.indexable(), self.n)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-04-22 00:15:42 -05:00
|
|
|
fn idx(&mut self, index: uint) -> Option<A> {
|
2013-07-29 10:44:45 -05:00
|
|
|
if index >= self.n {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
self.iter.idx(index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// An iterator to maintain state while iterating another iterator
|
2014-07-09 07:13:14 -05:00
|
|
|
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "waiting for unboxed closures"]
|
2014-12-03 00:59:31 -06:00
|
|
|
pub struct Scan<A, B, I, St, F> where I: Iterator<A>, F: FnMut(&mut St, A) -> Option<B> {
|
|
|
|
iter: I,
|
|
|
|
f: F,
|
2013-05-28 16:35:52 -05:00
|
|
|
|
|
|
|
/// The current internal state to be passed to the closure next.
|
2014-03-27 17:09:47 -05:00
|
|
|
pub state: St,
|
2013-04-24 18:54:13 -05:00
|
|
|
}
|
|
|
|
|
2014-12-13 21:04:23 -06:00
|
|
|
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
|
2014-12-20 02:35:06 -06:00
|
|
|
#[stable]
|
2014-12-13 21:04:23 -06:00
|
|
|
impl<A, B, I, St, F> Clone for Scan<A, B, I, St, F> where
|
|
|
|
I: Clone + Iterator<A>,
|
|
|
|
St: Clone,
|
|
|
|
F: Clone + FnMut(&mut St, A) -> Option<B>,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Scan<A, B, I, St, F> {
|
|
|
|
Scan {
|
|
|
|
iter: self.iter.clone(),
|
|
|
|
f: self.f.clone(),
|
|
|
|
state: self.state.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-12-03 00:59:31 -06:00
|
|
|
impl<A, B, I, St, F> Iterator<B> for Scan<A, B, I, St, F> where
|
|
|
|
I: Iterator<A>,
|
|
|
|
F: FnMut(&mut St, A) -> Option<B>,
|
|
|
|
{
|
2013-04-24 18:54:13 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<B> {
|
2013-09-11 14:52:17 -05:00
|
|
|
self.iter.next().and_then(|a| (self.f)(&mut self.state, a))
|
2013-04-24 18:54:13 -05:00
|
|
|
}
|
2013-07-02 22:59:26 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
|
|
|
let (_, upper) = self.iter.size_hint();
|
|
|
|
(0, upper) // can't know a lower bound, due to the scan function
|
|
|
|
}
|
2013-04-24 18:54:13 -05:00
|
|
|
}
|
|
|
|
|
2013-06-23 19:34:38 -05:00
|
|
|
/// An iterator that maps each element to an iterator,
|
|
|
|
/// and yields the elements of the produced iterators
|
|
|
|
///
|
2014-07-09 07:13:14 -05:00
|
|
|
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "waiting for unboxed closures"]
|
2014-12-03 02:07:16 -06:00
|
|
|
pub struct FlatMap<A, B, I, U, F> where I: Iterator<A>, U: Iterator<B>, F: FnMut(A) -> U {
|
|
|
|
iter: I,
|
|
|
|
f: F,
|
2014-03-27 17:09:47 -05:00
|
|
|
frontiter: Option<U>,
|
|
|
|
backiter: Option<U>,
|
2013-06-23 19:34:38 -05:00
|
|
|
}
|
|
|
|
|
2014-12-13 21:04:23 -06:00
|
|
|
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
|
2014-12-20 02:35:06 -06:00
|
|
|
#[stable]
|
2014-12-13 21:04:23 -06:00
|
|
|
impl<A, B, I, U, F> Clone for FlatMap<A, B, I, U, F> where
|
|
|
|
I: Clone + Iterator<A>,
|
|
|
|
U: Clone + Iterator<B>,
|
|
|
|
F: Clone + FnMut(A) -> U,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> FlatMap<A, B, I, U, F> {
|
|
|
|
FlatMap {
|
|
|
|
iter: self.iter.clone(),
|
|
|
|
f: self.f.clone(),
|
|
|
|
frontiter: self.frontiter.clone(),
|
|
|
|
backiter: self.backiter.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-12-03 02:07:16 -06:00
|
|
|
impl<A, B, I, U, F> Iterator<B> for FlatMap<A, B, I, U, F> where
|
|
|
|
I: Iterator<A>,
|
|
|
|
U: Iterator<B>,
|
|
|
|
F: FnMut(A) -> U,
|
|
|
|
{
|
2013-06-23 19:34:38 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<B> {
|
|
|
|
loop {
|
2014-09-14 22:27:36 -05:00
|
|
|
for inner in self.frontiter.iter_mut() {
|
2013-08-03 11:45:23 -05:00
|
|
|
for x in *inner {
|
2013-06-23 19:34:38 -05:00
|
|
|
return Some(x)
|
|
|
|
}
|
|
|
|
}
|
2013-09-20 01:08:47 -05:00
|
|
|
match self.iter.next().map(|x| (self.f)(x)) {
|
|
|
|
None => return self.backiter.as_mut().and_then(|it| it.next()),
|
2013-07-19 20:11:28 -05:00
|
|
|
next => self.frontiter = next,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-20 10:10:00 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
2013-12-06 12:51:10 -06:00
|
|
|
let (flo, fhi) = self.frontiter.as_ref().map_or((0, Some(0)), |it| it.size_hint());
|
|
|
|
let (blo, bhi) = self.backiter.as_ref().map_or((0, Some(0)), |it| it.size_hint());
|
2013-08-05 22:04:52 -05:00
|
|
|
let lo = flo.saturating_add(blo);
|
2013-07-20 10:10:00 -05:00
|
|
|
match (self.iter.size_hint(), fhi, bhi) {
|
2014-11-09 07:11:28 -06:00
|
|
|
((0, Some(0)), Some(a), Some(b)) => (lo, a.checked_add(b)),
|
2013-08-05 22:04:52 -05:00
|
|
|
_ => (lo, None)
|
2013-07-20 10:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
2013-07-19 20:11:28 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-12-03 02:07:16 -06:00
|
|
|
impl<A, B, I, U, F> DoubleEndedIterator<B> for FlatMap<A, B, I, U, F> where
|
|
|
|
I: DoubleEndedIterator<A>,
|
|
|
|
U: DoubleEndedIterator<B>,
|
|
|
|
F: FnMut(A) -> U,
|
|
|
|
{
|
2013-07-19 20:11:28 -05:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<B> {
|
|
|
|
loop {
|
2014-09-14 22:27:36 -05:00
|
|
|
for inner in self.backiter.iter_mut() {
|
2013-07-19 20:11:28 -05:00
|
|
|
match inner.next_back() {
|
|
|
|
None => (),
|
|
|
|
y => return y
|
|
|
|
}
|
|
|
|
}
|
2013-09-20 01:08:47 -05:00
|
|
|
match self.iter.next_back().map(|x| (self.f)(x)) {
|
|
|
|
None => return self.frontiter.as_mut().and_then(|it| it.next_back()),
|
2013-07-19 20:11:28 -05:00
|
|
|
next => self.backiter = next,
|
2013-06-23 19:34:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-03 15:51:49 -05:00
|
|
|
/// An iterator that yields `None` forever after the underlying iterator
|
|
|
|
/// yields `None` once.
|
2014-03-05 00:19:14 -06:00
|
|
|
#[deriving(Clone)]
|
2014-07-09 07:13:14 -05:00
|
|
|
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2013-08-03 15:51:49 -05:00
|
|
|
pub struct Fuse<T> {
|
2014-03-27 17:09:47 -05:00
|
|
|
iter: T,
|
|
|
|
done: bool
|
2013-08-03 15:51:49 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2013-08-03 15:51:49 -05:00
|
|
|
impl<A, T: Iterator<A>> Iterator<A> for Fuse<T> {
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> {
|
|
|
|
if self.done {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
match self.iter.next() {
|
|
|
|
None => {
|
|
|
|
self.done = true;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
x => x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
|
|
|
if self.done {
|
|
|
|
(0, Some(0))
|
|
|
|
} else {
|
|
|
|
self.iter.size_hint()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2013-08-03 15:51:49 -05:00
|
|
|
impl<A, T: DoubleEndedIterator<A>> DoubleEndedIterator<A> for Fuse<T> {
|
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<A> {
|
|
|
|
if self.done {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
match self.iter.next_back() {
|
|
|
|
None => {
|
|
|
|
self.done = true;
|
|
|
|
None
|
|
|
|
}
|
|
|
|
x => x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow RandomAccessIterators to be fused without affecting random-access behavior
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental = "trait is experimental"]
|
2013-08-03 15:51:49 -05:00
|
|
|
impl<A, T: RandomAccessIterator<A>> RandomAccessIterator<A> for Fuse<T> {
|
|
|
|
#[inline]
|
|
|
|
fn indexable(&self) -> uint {
|
|
|
|
self.iter.indexable()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-04-22 00:15:42 -05:00
|
|
|
fn idx(&mut self, index: uint) -> Option<A> {
|
2013-08-03 15:51:49 -05:00
|
|
|
self.iter.idx(index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental = "seems marginal"]
|
2013-08-03 15:51:49 -05:00
|
|
|
impl<T> Fuse<T> {
|
|
|
|
/// Resets the fuse such that the next call to .next() or .next_back() will
|
2013-12-14 23:26:09 -06:00
|
|
|
/// call the underlying iterator again even if it previously returned None.
|
2013-08-03 15:51:49 -05:00
|
|
|
#[inline]
|
2013-12-08 01:55:28 -06:00
|
|
|
pub fn reset_fuse(&mut self) {
|
2013-08-03 15:51:49 -05:00
|
|
|
self.done = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-05 12:00:11 -05:00
|
|
|
/// An iterator that calls a function with a reference to each
|
|
|
|
/// element before yielding it.
|
2014-07-09 07:13:14 -05:00
|
|
|
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "waiting for unboxed closures"]
|
2014-12-03 03:04:27 -06:00
|
|
|
pub struct Inspect<A, I, F> where I: Iterator<A>, F: FnMut(&A) {
|
|
|
|
iter: I,
|
|
|
|
f: F,
|
2013-07-05 12:00:11 -05:00
|
|
|
}
|
|
|
|
|
2014-12-13 21:04:23 -06:00
|
|
|
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
|
2014-12-20 02:35:06 -06:00
|
|
|
#[stable]
|
2014-12-13 21:04:23 -06:00
|
|
|
impl<A, I, F> Clone for Inspect<A, I, F> where
|
|
|
|
I: Clone + Iterator<A>,
|
|
|
|
F: Clone + FnMut(&A),
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Inspect<A, I, F> {
|
|
|
|
Inspect {
|
|
|
|
iter: self.iter.clone(),
|
|
|
|
f: self.f.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-03 03:04:27 -06:00
|
|
|
impl<A, I, F> Inspect<A, I, F> where I: Iterator<A>, F: FnMut(&A) {
|
2013-07-05 12:00:11 -05:00
|
|
|
#[inline]
|
2014-04-22 01:25:18 -05:00
|
|
|
fn do_inspect(&mut self, elt: Option<A>) -> Option<A> {
|
2013-07-29 10:44:45 -05:00
|
|
|
match elt {
|
2013-07-05 12:00:11 -05:00
|
|
|
Some(ref a) => (self.f)(a),
|
|
|
|
None => ()
|
|
|
|
}
|
|
|
|
|
2013-07-29 10:44:45 -05:00
|
|
|
elt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-12-03 03:04:27 -06:00
|
|
|
impl<A, I, F> Iterator<A> for Inspect<A, I, F> where I: Iterator<A>, F: FnMut(&A) {
|
2013-07-29 10:44:45 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> {
|
|
|
|
let next = self.iter.next();
|
2013-08-10 18:55:34 -05:00
|
|
|
self.do_inspect(next)
|
2013-07-05 12:00:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
|
|
|
self.iter.size_hint()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-12-03 03:04:27 -06:00
|
|
|
impl<A, I, F> DoubleEndedIterator<A> for Inspect<A, I, F> where
|
|
|
|
I: DoubleEndedIterator<A>,
|
|
|
|
F: FnMut(&A),
|
|
|
|
{
|
2013-07-18 18:50:51 -05:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<A> {
|
|
|
|
let next = self.iter.next_back();
|
2013-08-10 18:55:34 -05:00
|
|
|
self.do_inspect(next)
|
2013-07-29 10:44:45 -05:00
|
|
|
}
|
|
|
|
}
|
2013-07-18 18:50:51 -05:00
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental = "trait is experimental"]
|
2014-12-03 03:04:27 -06:00
|
|
|
impl<A, I, F> RandomAccessIterator<A> for Inspect<A, I, F> where
|
|
|
|
I: RandomAccessIterator<A>,
|
|
|
|
F: FnMut(&A),
|
|
|
|
{
|
2013-07-29 10:44:45 -05:00
|
|
|
#[inline]
|
|
|
|
fn indexable(&self) -> uint {
|
|
|
|
self.iter.indexable()
|
|
|
|
}
|
2013-07-18 18:50:51 -05:00
|
|
|
|
2013-07-29 10:44:45 -05:00
|
|
|
#[inline]
|
2014-04-22 00:15:42 -05:00
|
|
|
fn idx(&mut self, index: uint) -> Option<A> {
|
|
|
|
let element = self.iter.idx(index);
|
|
|
|
self.do_inspect(element)
|
2013-07-18 18:50:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-15 03:22:49 -06:00
|
|
|
/// An iterator that passes mutable state to a closure and yields the result.
|
2014-12-03 18:31:21 -06:00
|
|
|
///
|
|
|
|
/// # Example: The Fibonacci Sequence
|
|
|
|
///
|
|
|
|
/// An iterator that yields sequential Fibonacci numbers, and stops on overflow.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::iter::Unfold;
|
|
|
|
/// use std::num::Int; // For `.checked_add()`
|
|
|
|
///
|
|
|
|
/// // This iterator will yield up to the last Fibonacci number before the max value of `u32`.
|
|
|
|
/// // You can simply change `u32` to `u64` in this line if you want higher values than that.
|
|
|
|
/// let mut fibonacci = Unfold::new((Some(0u32), Some(1u32)), |&(ref mut x2, ref mut x1)| {
|
|
|
|
/// // Attempt to get the next Fibonacci number
|
|
|
|
/// // `x1` will be `None` if previously overflowed.
|
|
|
|
/// let next = match (*x2, *x1) {
|
|
|
|
/// (Some(x2), Some(x1)) => x2.checked_add(x1),
|
|
|
|
/// _ => None,
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// // Shift left: ret <- x2 <- x1 <- next
|
|
|
|
/// let ret = *x2;
|
|
|
|
/// *x2 = *x1;
|
|
|
|
/// *x1 = next;
|
|
|
|
///
|
|
|
|
/// ret
|
|
|
|
/// });
|
|
|
|
///
|
|
|
|
/// for i in fibonacci {
|
|
|
|
/// println!("{}", i);
|
|
|
|
/// }
|
|
|
|
/// ```
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental]
|
2014-12-03 14:51:47 -06:00
|
|
|
pub struct Unfold<A, St, F> where F: FnMut(&mut St) -> Option<A> {
|
|
|
|
f: F,
|
2014-12-03 18:31:21 -06:00
|
|
|
/// Internal state that will be passed to the closure on the next iteration
|
2014-03-27 17:09:47 -05:00
|
|
|
pub state: St,
|
2013-04-19 09:07:07 -05:00
|
|
|
}
|
|
|
|
|
2014-12-13 21:04:23 -06:00
|
|
|
// FIXME(#19839) Remove in favor of `#[deriving(Clone)]`
|
2014-12-20 02:35:06 -06:00
|
|
|
#[stable]
|
2014-12-13 21:04:23 -06:00
|
|
|
impl<A, St, F> Clone for Unfold<A, St, F> where
|
|
|
|
F: Clone + FnMut(&mut St) -> Option<A>,
|
|
|
|
St: Clone,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Unfold<A, St, F> {
|
|
|
|
Unfold {
|
|
|
|
f: self.f.clone(),
|
|
|
|
state: self.state.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental]
|
2014-12-03 14:51:47 -06:00
|
|
|
impl<A, St, F> Unfold<A, St, F> where F: FnMut(&mut St) -> Option<A> {
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Creates a new iterator with the specified closure as the "iterator
|
2014-12-03 18:31:21 -06:00
|
|
|
/// function" and an initial state to eventually pass to the closure
|
2013-04-19 09:07:07 -05:00
|
|
|
#[inline]
|
2014-12-03 14:51:47 -06:00
|
|
|
pub fn new(initial_state: St, f: F) -> Unfold<A, St, F> {
|
2013-09-07 19:52:19 -05:00
|
|
|
Unfold {
|
2013-04-19 09:07:07 -05:00
|
|
|
f: f,
|
|
|
|
state: initial_state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental]
|
2014-12-03 14:51:47 -06:00
|
|
|
impl<A, St, F> Iterator<A> for Unfold<A, St, F> where F: FnMut(&mut St) -> Option<A> {
|
2013-04-19 09:07:07 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> {
|
|
|
|
(self.f)(&mut self.state)
|
|
|
|
}
|
2013-08-18 23:47:43 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
|
|
|
// no possible known bounds at this point
|
|
|
|
(0, None)
|
|
|
|
}
|
2013-04-19 09:07:07 -05:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// An infinite iterator starting at `start` and advancing by `step` with each
|
|
|
|
/// iteration
|
2014-12-14 21:35:22 -06:00
|
|
|
#[deriving(Clone, Copy)]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "may be renamed"]
|
2013-04-24 18:54:13 -05:00
|
|
|
pub struct Counter<A> {
|
2013-05-28 16:35:52 -05:00
|
|
|
/// The current state the counter is at (next value to be yielded)
|
2014-03-27 17:09:47 -05:00
|
|
|
state: A,
|
2013-05-28 16:35:52 -05:00
|
|
|
/// The amount that this iterator is stepping by
|
2014-03-27 17:09:47 -05:00
|
|
|
step: A,
|
2013-04-19 22:32:27 -05:00
|
|
|
}
|
|
|
|
|
2013-08-05 19:02:26 -05:00
|
|
|
/// Creates a new counter with the specified start/step
|
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "may be renamed"]
|
2013-08-05 19:02:26 -05:00
|
|
|
pub fn count<A>(start: A, step: A) -> Counter<A> {
|
|
|
|
Counter{state: start, step: step}
|
2013-04-24 18:54:13 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2013-09-14 15:37:52 -05:00
|
|
|
impl<A: Add<A, A> + Clone> Iterator<A> for Counter<A> {
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> {
|
|
|
|
let result = self.state.clone();
|
2014-12-01 14:52:17 -06:00
|
|
|
self.state = self.state.clone() + self.step.clone();
|
2013-09-14 15:37:52 -05:00
|
|
|
Some(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
2014-01-25 01:37:51 -06:00
|
|
|
(uint::MAX, None) // Too bad we can't specify an infinite lower bound
|
2013-09-14 15:37:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-14 15:33:19 -05:00
|
|
|
/// An iterator over the range [start, stop)
|
2014-12-14 21:35:22 -06:00
|
|
|
#[deriving(Clone, Copy)]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "may be refactored due to numerics reform or ops reform"]
|
2013-08-01 17:35:46 -05:00
|
|
|
pub struct Range<A> {
|
2014-03-27 17:09:47 -05:00
|
|
|
state: A,
|
|
|
|
stop: A,
|
2014-11-09 16:35:53 -06:00
|
|
|
one: A,
|
2013-08-01 17:35:46 -05:00
|
|
|
}
|
|
|
|
|
2014-06-27 11:29:41 -05:00
|
|
|
/// Returns an iterator over the given range [start, stop) (that is, starting
|
|
|
|
/// at start (inclusive), and ending at stop (exclusive)).
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// let array = [0, 1, 2, 3, 4];
|
|
|
|
///
|
|
|
|
/// for i in range(0, 5u) {
|
|
|
|
/// println!("{}", i);
|
|
|
|
/// assert_eq!(i, array[i]);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2013-08-01 17:35:46 -05:00
|
|
|
#[inline]
|
2014-11-09 16:35:53 -06:00
|
|
|
pub fn range<A: Int>(start: A, stop: A) -> Range<A> {
|
|
|
|
Range {
|
|
|
|
state: start,
|
|
|
|
stop: stop,
|
|
|
|
one: Int::one(),
|
|
|
|
}
|
2013-08-01 17:35:46 -05:00
|
|
|
}
|
|
|
|
|
2013-11-11 04:06:26 -06:00
|
|
|
// FIXME: #10414: Unfortunate type bound
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-11-09 16:35:53 -06:00
|
|
|
impl<A: Int + ToPrimitive> Iterator<A> for Range<A> {
|
2013-08-01 17:35:46 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> {
|
|
|
|
if self.state < self.stop {
|
|
|
|
let result = self.state.clone();
|
|
|
|
self.state = self.state + self.one;
|
|
|
|
Some(result)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2013-08-18 23:47:43 -05:00
|
|
|
|
2013-11-11 04:06:26 -06:00
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
|
|
|
// This first checks if the elements are representable as i64. If they aren't, try u64 (to
|
|
|
|
// handle cases like range(huge, huger)). We don't use uint/int because the difference of
|
|
|
|
// the i64/u64 might lie within their range.
|
|
|
|
let bound = match self.state.to_i64() {
|
|
|
|
Some(a) => {
|
2014-11-09 07:11:28 -06:00
|
|
|
let sz = self.stop.to_i64().map(|b| b.checked_sub(a));
|
2013-11-11 04:06:26 -06:00
|
|
|
match sz {
|
|
|
|
Some(Some(bound)) => bound.to_uint(),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => match self.state.to_u64() {
|
|
|
|
Some(a) => {
|
2014-11-09 07:11:28 -06:00
|
|
|
let sz = self.stop.to_u64().map(|b| b.checked_sub(a));
|
2013-11-11 04:06:26 -06:00
|
|
|
match sz {
|
|
|
|
Some(Some(bound)) => bound.to_uint(),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match bound {
|
|
|
|
Some(b) => (b, Some(b)),
|
|
|
|
// Standard fallback for unbounded/unrepresentable bounds
|
|
|
|
None => (0, None)
|
|
|
|
}
|
|
|
|
}
|
2013-08-01 17:35:46 -05:00
|
|
|
}
|
|
|
|
|
2014-02-16 14:20:01 -06:00
|
|
|
/// `Int` is required to ensure the range will be the same regardless of
|
2013-09-14 15:34:32 -05:00
|
|
|
/// the direction it is consumed.
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-11-09 16:35:53 -06:00
|
|
|
impl<A: Int + ToPrimitive> DoubleEndedIterator<A> for Range<A> {
|
2013-08-07 00:34:22 -05:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<A> {
|
|
|
|
if self.stop > self.state {
|
|
|
|
self.stop = self.stop - self.one;
|
|
|
|
Some(self.stop.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-14 15:33:19 -05:00
|
|
|
/// An iterator over the range [start, stop]
|
2014-03-05 00:19:14 -06:00
|
|
|
#[deriving(Clone)]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "may be refactored due to numerics reform or ops reform"]
|
2013-08-17 17:41:53 -05:00
|
|
|
pub struct RangeInclusive<A> {
|
2014-03-27 17:09:47 -05:00
|
|
|
range: Range<A>,
|
|
|
|
done: bool,
|
2013-08-17 17:41:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return an iterator over the range [start, stop]
|
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "may be refactored due to numerics reform or ops reform"]
|
2014-11-09 16:35:53 -06:00
|
|
|
pub fn range_inclusive<A: Int>(start: A, stop: A) -> RangeInclusive<A> {
|
|
|
|
RangeInclusive {
|
|
|
|
range: range(start, stop),
|
|
|
|
done: false,
|
|
|
|
}
|
2013-08-17 17:41:53 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-11-09 16:35:53 -06:00
|
|
|
impl<A: Int + ToPrimitive> Iterator<A> for RangeInclusive<A> {
|
2013-08-17 17:41:53 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> {
|
|
|
|
match self.range.next() {
|
|
|
|
Some(x) => Some(x),
|
|
|
|
None => {
|
2013-09-14 23:27:52 -05:00
|
|
|
if !self.done && self.range.state == self.range.stop {
|
2013-08-17 17:41:53 -05:00
|
|
|
self.done = true;
|
|
|
|
Some(self.range.stop.clone())
|
2013-09-14 23:27:52 -05:00
|
|
|
} else {
|
|
|
|
None
|
2013-08-17 17:41:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-21 15:58:00 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
|
|
|
let (lo, hi) = self.range.size_hint();
|
|
|
|
if self.done {
|
|
|
|
(lo, hi)
|
|
|
|
} else {
|
|
|
|
let lo = lo.saturating_add(1);
|
|
|
|
let hi = match hi {
|
2014-11-09 07:11:28 -06:00
|
|
|
Some(x) => x.checked_add(1),
|
2013-08-21 15:58:00 -05:00
|
|
|
None => None
|
|
|
|
};
|
|
|
|
(lo, hi)
|
|
|
|
}
|
|
|
|
}
|
2013-08-17 17:41:53 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-11-09 16:35:53 -06:00
|
|
|
impl<A: Int + ToPrimitive> DoubleEndedIterator<A> for RangeInclusive<A> {
|
2013-08-17 17:41:53 -05:00
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<A> {
|
|
|
|
if self.range.stop > self.range.state {
|
|
|
|
let result = self.range.stop.clone();
|
|
|
|
self.range.stop = self.range.stop - self.range.one;
|
|
|
|
Some(result)
|
2013-09-14 23:39:34 -05:00
|
|
|
} else if !self.done && self.range.state == self.range.stop {
|
2013-08-17 17:41:53 -05:00
|
|
|
self.done = true;
|
|
|
|
Some(self.range.stop.clone())
|
2013-09-14 23:39:34 -05:00
|
|
|
} else {
|
|
|
|
None
|
2013-08-17 17:41:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-14 15:47:21 -05:00
|
|
|
/// An iterator over the range [start, stop) by `step`. It handles overflow by stopping.
|
2014-03-05 00:19:14 -06:00
|
|
|
#[deriving(Clone)]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "may be refactored due to numerics reform or ops reform"]
|
2013-09-14 15:47:21 -05:00
|
|
|
pub struct RangeStep<A> {
|
2014-03-27 17:09:47 -05:00
|
|
|
state: A,
|
|
|
|
stop: A,
|
|
|
|
step: A,
|
|
|
|
rev: bool,
|
2013-09-14 15:47:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return an iterator over the range [start, stop) by `step`. It handles overflow by stopping.
|
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "may be refactored due to numerics reform or ops reform"]
|
2014-11-09 07:11:28 -06:00
|
|
|
pub fn range_step<A: Int>(start: A, stop: A, step: A) -> RangeStep<A> {
|
2014-11-09 16:35:53 -06:00
|
|
|
let rev = step < Int::zero();
|
2013-09-14 15:47:21 -05:00
|
|
|
RangeStep{state: start, stop: stop, step: step, rev: rev}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-11-09 07:11:28 -06:00
|
|
|
impl<A: Int> Iterator<A> for RangeStep<A> {
|
2013-09-14 15:47:21 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> {
|
2013-09-14 23:11:50 -05:00
|
|
|
if (self.rev && self.state > self.stop) || (!self.rev && self.state < self.stop) {
|
2014-11-09 07:11:28 -06:00
|
|
|
let result = self.state;
|
|
|
|
match self.state.checked_add(self.step) {
|
2013-09-14 15:47:21 -05:00
|
|
|
Some(x) => self.state = x,
|
|
|
|
None => self.state = self.stop.clone()
|
|
|
|
}
|
|
|
|
Some(result)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An iterator over the range [start, stop] by `step`. It handles overflow by stopping.
|
2014-03-05 00:19:14 -06:00
|
|
|
#[deriving(Clone)]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "may be refactored due to numerics reform or ops reform"]
|
2013-09-14 15:47:21 -05:00
|
|
|
pub struct RangeStepInclusive<A> {
|
2014-03-27 17:09:47 -05:00
|
|
|
state: A,
|
|
|
|
stop: A,
|
|
|
|
step: A,
|
|
|
|
rev: bool,
|
|
|
|
done: bool,
|
2013-09-14 15:47:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return an iterator over the range [start, stop] by `step`. It handles overflow by stopping.
|
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "may be refactored due to numerics reform or ops reform"]
|
2014-11-09 07:11:28 -06:00
|
|
|
pub fn range_step_inclusive<A: Int>(start: A, stop: A, step: A) -> RangeStepInclusive<A> {
|
2014-11-09 16:35:53 -06:00
|
|
|
let rev = step < Int::zero();
|
|
|
|
RangeStepInclusive {
|
|
|
|
state: start,
|
|
|
|
stop: stop,
|
|
|
|
step: step,
|
|
|
|
rev: rev,
|
|
|
|
done: false,
|
|
|
|
}
|
2013-09-14 15:47:21 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2014-11-09 07:11:28 -06:00
|
|
|
impl<A: Int> Iterator<A> for RangeStepInclusive<A> {
|
2013-09-14 15:47:21 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> {
|
2013-09-14 23:11:50 -05:00
|
|
|
if !self.done && ((self.rev && self.state >= self.stop) ||
|
|
|
|
(!self.rev && self.state <= self.stop)) {
|
2014-11-09 07:11:28 -06:00
|
|
|
let result = self.state;
|
|
|
|
match self.state.checked_add(self.step) {
|
2013-09-14 23:11:50 -05:00
|
|
|
Some(x) => self.state = x,
|
|
|
|
None => self.done = true
|
2013-09-14 15:47:21 -05:00
|
|
|
}
|
2013-09-14 23:11:50 -05:00
|
|
|
Some(result)
|
2013-09-14 15:47:21 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-15 21:25:33 -06:00
|
|
|
|
|
|
|
/// The `Step` trait identifies objects which can be stepped over in both
|
|
|
|
/// directions. The `steps_between` function provides a way to
|
|
|
|
/// compare two Step objects (it could be provided using `step()` and `Ord`,
|
|
|
|
/// but the implementation would be so inefficient as to be useless).
|
|
|
|
#[unstable = "Trait is unstable."]
|
|
|
|
pub trait Step: Ord {
|
|
|
|
/// Change self to the next object.
|
|
|
|
fn step(&mut self);
|
|
|
|
/// Change self to the previous object.
|
|
|
|
fn step_back(&mut self);
|
|
|
|
/// The steps_between two step objects.
|
|
|
|
/// a should always be less than b, so the result should never be negative.
|
|
|
|
/// Return None if it is not possible to calculate steps_between without
|
|
|
|
/// overflow.
|
|
|
|
fn steps_between(a: &Self, b: &Self) -> Option<uint>;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! step_impl {
|
|
|
|
($($t:ty)*) => ($(
|
|
|
|
#[unstable = "Trait is unstable."]
|
|
|
|
impl Step for $t {
|
|
|
|
#[inline]
|
|
|
|
fn step(&mut self) { *self += 1; }
|
|
|
|
#[inline]
|
|
|
|
fn step_back(&mut self) { *self -= 1; }
|
|
|
|
#[inline]
|
|
|
|
fn steps_between(a: &$t, b: &$t) -> Option<uint> {
|
|
|
|
debug_assert!(a < b);
|
|
|
|
Some((*a - *b) as uint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! step_impl_no_between {
|
|
|
|
($($t:ty)*) => ($(
|
|
|
|
#[unstable = "Trait is unstable."]
|
|
|
|
impl Step for $t {
|
|
|
|
#[inline]
|
|
|
|
fn step(&mut self) { *self += 1; }
|
|
|
|
#[inline]
|
|
|
|
fn step_back(&mut self) { *self -= 1; }
|
|
|
|
#[inline]
|
|
|
|
fn steps_between(_a: &$t, _b: &$t) -> Option<uint> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*)
|
|
|
|
}
|
|
|
|
|
|
|
|
step_impl!(uint u8 u16 u32 int i8 i16 i32);
|
|
|
|
#[cfg(target_word_size = "64")]
|
|
|
|
step_impl!(u64 i64);
|
|
|
|
#[cfg(target_word_size = "32")]
|
|
|
|
step_impl_no_between!(u64 i64);
|
|
|
|
|
|
|
|
|
2013-08-03 14:34:00 -05:00
|
|
|
/// An iterator that repeats an element endlessly
|
2014-03-05 00:19:14 -06:00
|
|
|
#[deriving(Clone)]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[stable]
|
2013-08-03 14:34:00 -05:00
|
|
|
pub struct Repeat<A> {
|
2014-03-27 17:09:47 -05:00
|
|
|
element: A
|
2013-08-03 14:34:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<A: Clone> Repeat<A> {
|
2013-08-17 17:28:04 -05:00
|
|
|
/// Create a new `Repeat` that endlessly repeats the element `elt`.
|
2013-08-03 14:34:00 -05:00
|
|
|
#[inline]
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[deprecated = "use iter::repeat instead"]
|
2013-08-03 14:34:00 -05:00
|
|
|
pub fn new(elt: A) -> Repeat<A> {
|
|
|
|
Repeat{element: elt}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2013-08-03 14:34:00 -05:00
|
|
|
impl<A: Clone> Iterator<A> for Repeat<A> {
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> { self.idx(0) }
|
|
|
|
#[inline]
|
2014-01-25 01:37:51 -06:00
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) { (uint::MAX, None) }
|
2013-08-03 14:34:00 -05:00
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[unstable = "trait is unstable"]
|
2013-08-03 14:34:00 -05:00
|
|
|
impl<A: Clone> DoubleEndedIterator<A> for Repeat<A> {
|
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<A> { self.idx(0) }
|
|
|
|
}
|
|
|
|
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental = "trait is experimental"]
|
2013-08-03 14:34:00 -05:00
|
|
|
impl<A: Clone> RandomAccessIterator<A> for Repeat<A> {
|
|
|
|
#[inline]
|
2014-01-25 01:37:51 -06:00
|
|
|
fn indexable(&self) -> uint { uint::MAX }
|
2013-08-03 14:34:00 -05:00
|
|
|
#[inline]
|
2014-04-22 00:15:42 -05:00
|
|
|
fn idx(&mut self, _: uint) -> Option<A> { Some(self.element.clone()) }
|
2013-08-03 14:34:00 -05:00
|
|
|
}
|
|
|
|
|
2014-12-03 14:51:47 -06:00
|
|
|
type IterateState<T, F> = (F, Option<T>, bool);
|
2014-07-06 17:50:53 -05:00
|
|
|
|
|
|
|
/// An iterator that repeatedly applies a given function, starting
|
|
|
|
/// from a given seed value.
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental]
|
2014-12-03 14:51:47 -06:00
|
|
|
pub type Iterate<T, F> = Unfold<T, IterateState<T, F>, fn(&mut IterateState<T, F>) -> Option<T>>;
|
2014-07-06 17:50:53 -05:00
|
|
|
|
2014-10-30 17:44:42 -05:00
|
|
|
/// Create a new iterator that produces an infinite sequence of
|
2014-07-06 17:50:53 -05:00
|
|
|
/// repeated applications of the given function `f`.
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental]
|
2014-12-03 14:51:47 -06:00
|
|
|
pub fn iterate<T, F>(seed: T, f: F) -> Iterate<T, F> where
|
|
|
|
T: Clone,
|
|
|
|
F: FnMut(T) -> T,
|
|
|
|
{
|
|
|
|
fn next<T, F>(st: &mut IterateState<T, F>) -> Option<T> where
|
|
|
|
T: Clone,
|
|
|
|
F: FnMut(T) -> T,
|
|
|
|
{
|
2014-07-06 17:50:53 -05:00
|
|
|
let &(ref mut f, ref mut val, ref mut first) = st;
|
|
|
|
if *first {
|
|
|
|
*first = false;
|
|
|
|
} else {
|
2014-08-18 19:52:38 -05:00
|
|
|
match val.take() {
|
|
|
|
Some(x) => {
|
|
|
|
*val = Some((*f)(x))
|
|
|
|
}
|
|
|
|
None => {}
|
|
|
|
}
|
2014-07-06 17:50:53 -05:00
|
|
|
}
|
|
|
|
val.clone()
|
2014-12-03 14:51:47 -06:00
|
|
|
}
|
|
|
|
|
2014-12-15 14:41:30 -06:00
|
|
|
// coerce to a fn pointer
|
|
|
|
let next: fn(&mut IterateState<T,F>) -> Option<T> = next;
|
|
|
|
|
2014-12-03 14:51:47 -06:00
|
|
|
Unfold::new((f, Some(seed), true), next)
|
2014-07-06 17:50:53 -05:00
|
|
|
}
|
|
|
|
|
2014-10-30 17:44:42 -05:00
|
|
|
/// Create a new iterator that endlessly repeats the element `elt`.
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[inline]
|
|
|
|
#[stable]
|
2014-10-30 17:44:42 -05:00
|
|
|
pub fn repeat<T: Clone>(elt: T) -> Repeat<T> {
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
Repeat{element: elt}
|
2014-10-30 17:44:42 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:07:21 -05:00
|
|
|
/// Functions for lexicographical ordering of sequences.
|
|
|
|
///
|
|
|
|
/// Lexicographical ordering through `<`, `<=`, `>=`, `>` requires
|
2014-05-29 19:45:07 -05:00
|
|
|
/// that the elements implement both `PartialEq` and `PartialOrd`.
|
2013-08-08 15:07:21 -05:00
|
|
|
///
|
|
|
|
/// If two sequences are equal up until the point where one ends,
|
|
|
|
/// the shorter sequence compares less.
|
libs: stabilize iter module
This is an initial pass at stabilizing the `iter` module. The module is
fairly large, but is also pretty polished, so most of the stabilization
leaves things as they are.
Some changes:
* Due to the new object safety rules, various traits needs to be split
into object-safe traits and extension traits. This includes `Iterator`
itself. While splitting up the traits adds some complexity, it will
also increase flexbility: once we have automatic impls of `Trait` for
trait objects over `Trait`, then things like the iterator adapters
will all work with trait objects.
* Iterator adapters that use up the entire iterator now take it by
value, which makes the semantics more clear and helps catch bugs. Due
to the splitting of Iterator, this does not affect trait objects. If
the underlying iterator is still desired for some reason, `by_ref` can
be used. (Note: this change had no fallout in the Rust distro except
for the useless mut lint.)
* In general, extension traits new and old are following an [in-progress
convention](https://github.com/rust-lang/rfcs/pull/445). As such, they
are marked `unstable`.
* As usual, anything involving closures is `unstable` pending unboxed
closures.
* A few of the more esoteric/underdeveloped iterator forms (like
`RandomAccessIterator` and `MutableDoubleEndedIterator`, along with
various unfolds) are left experimental for now.
* The `order` submodule is left `experimental` because it will hopefully
be replaced by generalized comparison traits.
* "Leaf" iterators (like `Repeat` and `Counter`) are uniformly
constructed by free fns at the module level. That's because the types
are not otherwise of any significance (if we had `impl Trait`, you
wouldn't want to define a type at all).
Closes #17701
Due to renamings and splitting of traits, this is a:
[breaking-change]
2014-11-06 11:32:32 -06:00
|
|
|
#[experimental = "likely to be removed after cmp reform"]
|
2013-08-08 15:07:21 -05:00
|
|
|
pub mod order {
|
|
|
|
use cmp;
|
2014-05-31 12:43:52 -05:00
|
|
|
use cmp::{Eq, Ord, PartialOrd, PartialEq};
|
2014-11-28 10:57:41 -06:00
|
|
|
use cmp::Ordering::{Equal, Less, Greater};
|
|
|
|
use option::Option;
|
|
|
|
use option::Option::{Some, None};
|
2013-08-08 15:07:21 -05:00
|
|
|
use super::Iterator;
|
|
|
|
|
2014-05-31 12:43:52 -05:00
|
|
|
/// Compare `a` and `b` for equality using `Eq`
|
|
|
|
pub fn equals<A: Eq, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> bool {
|
2013-08-08 15:07:21 -05:00
|
|
|
loop {
|
|
|
|
match (a.next(), b.next()) {
|
|
|
|
(None, None) => return true,
|
|
|
|
(None, _) | (_, None) => return false,
|
2014-03-23 06:54:42 -05:00
|
|
|
(Some(x), Some(y)) => if x != y { return false },
|
2013-08-08 15:07:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-31 12:43:52 -05:00
|
|
|
/// Order `a` and `b` lexicographically using `Ord`
|
|
|
|
pub fn cmp<A: Ord, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> cmp::Ordering {
|
2013-08-08 15:07:21 -05:00
|
|
|
loop {
|
|
|
|
match (a.next(), b.next()) {
|
2014-11-28 10:57:41 -06:00
|
|
|
(None, None) => return Equal,
|
|
|
|
(None, _ ) => return Less,
|
|
|
|
(_ , None) => return Greater,
|
2013-08-08 15:07:21 -05:00
|
|
|
(Some(x), Some(y)) => match x.cmp(&y) {
|
2014-11-28 10:57:41 -06:00
|
|
|
Equal => (),
|
2013-08-08 15:07:21 -05:00
|
|
|
non_eq => return non_eq,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-18 01:25:51 -05:00
|
|
|
/// Order `a` and `b` lexicographically using `PartialOrd`
|
|
|
|
pub fn partial_cmp<A: PartialOrd, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S)
|
|
|
|
-> Option<cmp::Ordering> {
|
|
|
|
loop {
|
|
|
|
match (a.next(), b.next()) {
|
2014-11-28 10:57:41 -06:00
|
|
|
(None, None) => return Some(Equal),
|
|
|
|
(None, _ ) => return Some(Less),
|
|
|
|
(_ , None) => return Some(Greater),
|
2014-06-18 01:25:51 -05:00
|
|
|
(Some(x), Some(y)) => match x.partial_cmp(&y) {
|
2014-11-28 10:57:41 -06:00
|
|
|
Some(Equal) => (),
|
2014-06-18 01:25:51 -05:00
|
|
|
non_eq => return non_eq,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-29 19:45:07 -05:00
|
|
|
/// Compare `a` and `b` for equality (Using partial equality, `PartialEq`)
|
2014-11-20 23:14:05 -06:00
|
|
|
pub fn eq<A, B, L, R>(mut a: L, mut b: R) -> bool where
|
|
|
|
A: PartialEq<B>,
|
|
|
|
L: Iterator<A>,
|
|
|
|
R: Iterator<B>,
|
|
|
|
{
|
2013-08-08 15:07:21 -05:00
|
|
|
loop {
|
|
|
|
match (a.next(), b.next()) {
|
|
|
|
(None, None) => return true,
|
|
|
|
(None, _) | (_, None) => return false,
|
|
|
|
(Some(x), Some(y)) => if !x.eq(&y) { return false },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-29 19:45:07 -05:00
|
|
|
/// Compare `a` and `b` for nonequality (Using partial equality, `PartialEq`)
|
2014-11-20 23:14:05 -06:00
|
|
|
pub fn ne<A, B, L, R>(mut a: L, mut b: R) -> bool where
|
|
|
|
A: PartialEq<B>,
|
|
|
|
L: Iterator<A>,
|
|
|
|
R: Iterator<B>,
|
|
|
|
{
|
2013-08-08 15:07:21 -05:00
|
|
|
loop {
|
|
|
|
match (a.next(), b.next()) {
|
|
|
|
(None, None) => return false,
|
|
|
|
(None, _) | (_, None) => return true,
|
|
|
|
(Some(x), Some(y)) => if x.ne(&y) { return true },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-29 19:45:07 -05:00
|
|
|
/// Return `a` < `b` lexicographically (Using partial order, `PartialOrd`)
|
|
|
|
pub fn lt<A: PartialOrd, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> bool {
|
2013-08-08 15:07:21 -05:00
|
|
|
loop {
|
|
|
|
match (a.next(), b.next()) {
|
|
|
|
(None, None) => return false,
|
|
|
|
(None, _ ) => return true,
|
|
|
|
(_ , None) => return false,
|
|
|
|
(Some(x), Some(y)) => if x.ne(&y) { return x.lt(&y) },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-29 19:45:07 -05:00
|
|
|
/// Return `a` <= `b` lexicographically (Using partial order, `PartialOrd`)
|
|
|
|
pub fn le<A: PartialOrd, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> bool {
|
2013-08-08 15:07:21 -05:00
|
|
|
loop {
|
|
|
|
match (a.next(), b.next()) {
|
|
|
|
(None, None) => return true,
|
|
|
|
(None, _ ) => return true,
|
|
|
|
(_ , None) => return false,
|
|
|
|
(Some(x), Some(y)) => if x.ne(&y) { return x.le(&y) },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-29 19:45:07 -05:00
|
|
|
/// Return `a` > `b` lexicographically (Using partial order, `PartialOrd`)
|
|
|
|
pub fn gt<A: PartialOrd, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> bool {
|
2013-08-08 15:07:21 -05:00
|
|
|
loop {
|
|
|
|
match (a.next(), b.next()) {
|
|
|
|
(None, None) => return false,
|
|
|
|
(None, _ ) => return false,
|
|
|
|
(_ , None) => return true,
|
|
|
|
(Some(x), Some(y)) => if x.ne(&y) { return x.gt(&y) },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-29 19:45:07 -05:00
|
|
|
/// Return `a` >= `b` lexicographically (Using partial order, `PartialOrd`)
|
|
|
|
pub fn ge<A: PartialOrd, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> bool {
|
2013-08-08 15:07:21 -05:00
|
|
|
loop {
|
|
|
|
match (a.next(), b.next()) {
|
|
|
|
(None, None) => return true,
|
|
|
|
(None, _ ) => return false,
|
|
|
|
(_ , None) => return true,
|
|
|
|
(Some(x), Some(y)) => if x.ne(&y) { return x.ge(&y) },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-19 06:30:22 -05:00
|
|
|
}
|