From 02bda7a0617fd0d0d3ac11dffb17ffba8c0f0601 Mon Sep 17 00:00:00 2001 From: Clar Fon Date: Mon, 17 Dec 2018 21:39:07 -0500 Subject: [PATCH] Move trivial constructors to inherent methods --- src/libcore/iter/adapters/mod.rs | 82 +++++++++++++++++++++++------ src/libcore/iter/traits/iterator.rs | 20 +++---- 2 files changed, 76 insertions(+), 26 deletions(-) diff --git a/src/libcore/iter/adapters/mod.rs b/src/libcore/iter/adapters/mod.rs index 8bd064021e6c..f8d6bedeace2 100644 --- a/src/libcore/iter/adapters/mod.rs +++ b/src/libcore/iter/adapters/mod.rs @@ -26,7 +26,12 @@ pub(crate) use self::zip::TrustedRandomAccess; #[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Rev { - pub(super) iter: T + iter: T +} +impl Rev { + pub(super) fn new(iter: T) -> Rev { + Rev { iter } + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -127,7 +132,12 @@ unsafe impl TrustedLen for Rev #[must_use = "iterators are lazy and do nothing unless consumed"] #[derive(Clone, Debug)] pub struct Copied { - pub(super) it: I, + it: I, +} +impl Copied { + pub(super) fn new(it: I) -> Copied { + Copied { it } + } } #[unstable(feature = "iter_copied", issue = "57127")] @@ -227,7 +237,12 @@ unsafe impl<'a, I, T: 'a> TrustedLen for Copied #[must_use = "iterators are lazy and do nothing unless consumed"] #[derive(Clone, Debug)] pub struct Cloned { - pub(super) it: I, + it: I, +} +impl Cloned { + pub(super) fn new(it: I) -> Cloned { + Cloned { it } + } } #[stable(feature = "iter_cloned", since = "1.1.0")] @@ -525,8 +540,13 @@ impl ExactSizeIterator for StepBy where I: ExactSizeIterator {} #[stable(feature = "rust1", since = "1.0.0")] #[derive(Clone)] pub struct Map { - pub(super) iter: I, - pub(super) f: F, + iter: I, + f: F, +} +impl Map { + pub(super) fn new(iter: I, f: F) -> Map { + Map { iter, f } + } } #[stable(feature = "core_impl_debug", since = "1.9.0")] @@ -636,8 +656,13 @@ unsafe impl TrustedRandomAccess for Map #[stable(feature = "rust1", since = "1.0.0")] #[derive(Clone)] pub struct Filter { - pub(super) iter: I, - pub(super) predicate: P, + iter: I, + predicate: P, +} +impl Filter { + pub(super) fn new(iter: I, predicate: P) -> Filter { + Filter { iter, predicate } + } } #[stable(feature = "core_impl_debug", since = "1.9.0")] @@ -768,8 +793,13 @@ impl FusedIterator for Filter #[stable(feature = "rust1", since = "1.0.0")] #[derive(Clone)] pub struct FilterMap { - pub(super) iter: I, - pub(super) f: F, + iter: I, + f: F, +} +impl FilterMap { + pub(super) fn new(iter: I, f: F) -> FilterMap { + FilterMap { iter, f } + } } #[stable(feature = "core_impl_debug", since = "1.9.0")] @@ -1377,8 +1407,13 @@ impl FusedIterator for TakeWhile #[must_use = "iterators are lazy and do nothing unless consumed"] #[stable(feature = "rust1", since = "1.0.0")] pub struct Skip { - pub(super) iter: I, - pub(super) n: usize + iter: I, + n: usize +} +impl Skip { + pub(super) fn new(iter: I, n: usize) -> Skip { + Skip { iter, n } + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -1518,6 +1553,11 @@ pub struct Take { pub(super) iter: I, pub(super) n: usize } +impl Take { + pub(super) fn new(iter: I, n: usize) -> Take { + Take { iter, n } + } +} #[stable(feature = "rust1", since = "1.0.0")] impl Iterator for Take where I: Iterator{ @@ -1603,9 +1643,14 @@ unsafe impl TrustedLen for Take {} #[stable(feature = "rust1", since = "1.0.0")] #[derive(Clone)] pub struct Scan { - pub(super) iter: I, - pub(super) f: F, - pub(super) state: St, + iter: I, + f: F, + state: St, +} +impl Scan { + pub(super) fn new(iter: I, state: St, f: F) -> Scan { + Scan { iter, state, f } + } } #[stable(feature = "core_impl_debug", since = "1.9.0")] @@ -1893,8 +1938,13 @@ impl ExactSizeIterator for Fuse where I: ExactSizeIterator { #[stable(feature = "rust1", since = "1.0.0")] #[derive(Clone)] pub struct Inspect { - pub(super) iter: I, - pub(super) f: F, + iter: I, + f: F, +} +impl Inspect { + pub(super) fn new(iter: I, f: F) -> Inspect { + Inspect { iter, f } + } } #[stable(feature = "core_impl_debug", since = "1.9.0")] diff --git a/src/libcore/iter/traits/iterator.rs b/src/libcore/iter/traits/iterator.rs index 44e29fd813a9..9dfa83f473ba 100644 --- a/src/libcore/iter/traits/iterator.rs +++ b/src/libcore/iter/traits/iterator.rs @@ -558,7 +558,7 @@ pub trait Iterator { fn map(self, f: F) -> Map where Self: Sized, F: FnMut(Self::Item) -> B, { - Map { iter: self, f } + Map::new(self, f) } /// Calls a closure on each element of an iterator. @@ -669,7 +669,7 @@ pub trait Iterator { fn filter

(self, predicate: P) -> Filter where Self: Sized, P: FnMut(&Self::Item) -> bool, { - Filter {iter: self, predicate } + Filter::new(self, predicate) } /// Creates an iterator that both filters and maps. @@ -726,7 +726,7 @@ pub trait Iterator { fn filter_map(self, f: F) -> FilterMap where Self: Sized, F: FnMut(Self::Item) -> Option, { - FilterMap { iter: self, f } + FilterMap::new(self, f) } /// Creates an iterator which gives the current iteration count as well as @@ -981,7 +981,7 @@ pub trait Iterator { #[inline] #[stable(feature = "rust1", since = "1.0.0")] fn skip(self, n: usize) -> Skip where Self: Sized { - Skip { iter: self, n } + Skip::new(self, n) } /// Creates an iterator that yields its first `n` elements. @@ -1013,7 +1013,7 @@ pub trait Iterator { #[inline] #[stable(feature = "rust1", since = "1.0.0")] fn take(self, n: usize) -> Take where Self: Sized, { - Take { iter: self, n } + Take::new(self, n) } /// An iterator adaptor similar to [`fold`] that holds internal state and @@ -1058,7 +1058,7 @@ pub trait Iterator { fn scan(self, initial_state: St, f: F) -> Scan where Self: Sized, F: FnMut(&mut St, Self::Item) -> Option, { - Scan { iter: self, f, state: initial_state } + Scan::new(self, initial_state, f) } /// Creates an iterator that works like map, but flattens nested structure. @@ -1307,7 +1307,7 @@ pub trait Iterator { fn inspect(self, f: F) -> Inspect where Self: Sized, F: FnMut(&Self::Item), { - Inspect { iter: self, f } + Inspect::new(self, f) } /// Borrows an iterator, rather than consuming it. @@ -2181,7 +2181,7 @@ pub trait Iterator { #[inline] #[stable(feature = "rust1", since = "1.0.0")] fn rev(self) -> Rev where Self: Sized + DoubleEndedIterator { - Rev{iter: self} + Rev::new(self) } /// Converts an iterator of pairs into a pair of containers. @@ -2249,7 +2249,7 @@ pub trait Iterator { fn copied<'a, T: 'a>(self) -> Copied where Self: Sized + Iterator, T: Copy { - Copied { it: self } + Copied::new(self) } /// Creates an iterator which [`clone`]s all of its elements. @@ -2278,7 +2278,7 @@ pub trait Iterator { fn cloned<'a, T: 'a>(self) -> Cloned where Self: Sized + Iterator, T: Clone { - Cloned { it: self } + Cloned::new(self) } /// Repeats an iterator endlessly.