Skip to content

Commit 02bda7a

Browse files
author
Clar Fon
committed
Move trivial constructors to inherent methods
1 parent 4c28b2c commit 02bda7a

File tree

2 files changed

+76
-26
lines changed

2 files changed

+76
-26
lines changed

src/libcore/iter/adapters/mod.rs

+66-16
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ pub(crate) use self::zip::TrustedRandomAccess;
2626
#[must_use = "iterators are lazy and do nothing unless consumed"]
2727
#[stable(feature = "rust1", since = "1.0.0")]
2828
pub struct Rev<T> {
29-
pub(super) iter: T
29+
iter: T
30+
}
31+
impl<T> Rev<T> {
32+
pub(super) fn new(iter: T) -> Rev<T> {
33+
Rev { iter }
34+
}
3035
}
3136

3237
#[stable(feature = "rust1", since = "1.0.0")]
@@ -127,7 +132,12 @@ unsafe impl<I> TrustedLen for Rev<I>
127132
#[must_use = "iterators are lazy and do nothing unless consumed"]
128133
#[derive(Clone, Debug)]
129134
pub struct Copied<I> {
130-
pub(super) it: I,
135+
it: I,
136+
}
137+
impl<I> Copied<I> {
138+
pub(super) fn new(it: I) -> Copied<I> {
139+
Copied { it }
140+
}
131141
}
132142

133143
#[unstable(feature = "iter_copied", issue = "57127")]
@@ -227,7 +237,12 @@ unsafe impl<'a, I, T: 'a> TrustedLen for Copied<I>
227237
#[must_use = "iterators are lazy and do nothing unless consumed"]
228238
#[derive(Clone, Debug)]
229239
pub struct Cloned<I> {
230-
pub(super) it: I,
240+
it: I,
241+
}
242+
impl<I> Cloned<I> {
243+
pub(super) fn new(it: I) -> Cloned<I> {
244+
Cloned { it }
245+
}
231246
}
232247

233248
#[stable(feature = "iter_cloned", since = "1.1.0")]
@@ -525,8 +540,13 @@ impl<I> ExactSizeIterator for StepBy<I> where I: ExactSizeIterator {}
525540
#[stable(feature = "rust1", since = "1.0.0")]
526541
#[derive(Clone)]
527542
pub struct Map<I, F> {
528-
pub(super) iter: I,
529-
pub(super) f: F,
543+
iter: I,
544+
f: F,
545+
}
546+
impl<I, F> Map<I, F> {
547+
pub(super) fn new(iter: I, f: F) -> Map<I, F> {
548+
Map { iter, f }
549+
}
530550
}
531551

532552
#[stable(feature = "core_impl_debug", since = "1.9.0")]
@@ -636,8 +656,13 @@ unsafe impl<B, I, F> TrustedRandomAccess for Map<I, F>
636656
#[stable(feature = "rust1", since = "1.0.0")]
637657
#[derive(Clone)]
638658
pub struct Filter<I, P> {
639-
pub(super) iter: I,
640-
pub(super) predicate: P,
659+
iter: I,
660+
predicate: P,
661+
}
662+
impl<I, P> Filter<I, P> {
663+
pub(super) fn new(iter: I, predicate: P) -> Filter<I, P> {
664+
Filter { iter, predicate }
665+
}
641666
}
642667

643668
#[stable(feature = "core_impl_debug", since = "1.9.0")]
@@ -768,8 +793,13 @@ impl<I: FusedIterator, P> FusedIterator for Filter<I, P>
768793
#[stable(feature = "rust1", since = "1.0.0")]
769794
#[derive(Clone)]
770795
pub struct FilterMap<I, F> {
771-
pub(super) iter: I,
772-
pub(super) f: F,
796+
iter: I,
797+
f: F,
798+
}
799+
impl<I, F> FilterMap<I, F> {
800+
pub(super) fn new(iter: I, f: F) -> FilterMap<I, F> {
801+
FilterMap { iter, f }
802+
}
773803
}
774804

775805
#[stable(feature = "core_impl_debug", since = "1.9.0")]
@@ -1377,8 +1407,13 @@ impl<I, P> FusedIterator for TakeWhile<I, P>
13771407
#[must_use = "iterators are lazy and do nothing unless consumed"]
13781408
#[stable(feature = "rust1", since = "1.0.0")]
13791409
pub struct Skip<I> {
1380-
pub(super) iter: I,
1381-
pub(super) n: usize
1410+
iter: I,
1411+
n: usize
1412+
}
1413+
impl<I> Skip<I> {
1414+
pub(super) fn new(iter: I, n: usize) -> Skip<I> {
1415+
Skip { iter, n }
1416+
}
13821417
}
13831418

13841419
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1518,6 +1553,11 @@ pub struct Take<I> {
15181553
pub(super) iter: I,
15191554
pub(super) n: usize
15201555
}
1556+
impl<I> Take<I> {
1557+
pub(super) fn new(iter: I, n: usize) -> Take<I> {
1558+
Take { iter, n }
1559+
}
1560+
}
15211561

15221562
#[stable(feature = "rust1", since = "1.0.0")]
15231563
impl<I> Iterator for Take<I> where I: Iterator{
@@ -1603,9 +1643,14 @@ unsafe impl<I: TrustedLen> TrustedLen for Take<I> {}
16031643
#[stable(feature = "rust1", since = "1.0.0")]
16041644
#[derive(Clone)]
16051645
pub struct Scan<I, St, F> {
1606-
pub(super) iter: I,
1607-
pub(super) f: F,
1608-
pub(super) state: St,
1646+
iter: I,
1647+
f: F,
1648+
state: St,
1649+
}
1650+
impl<I, St, F> Scan<I, St, F> {
1651+
pub(super) fn new(iter: I, state: St, f: F) -> Scan<I, St, F> {
1652+
Scan { iter, state, f }
1653+
}
16091654
}
16101655

16111656
#[stable(feature = "core_impl_debug", since = "1.9.0")]
@@ -1893,8 +1938,13 @@ impl<I> ExactSizeIterator for Fuse<I> where I: ExactSizeIterator {
18931938
#[stable(feature = "rust1", since = "1.0.0")]
18941939
#[derive(Clone)]
18951940
pub struct Inspect<I, F> {
1896-
pub(super) iter: I,
1897-
pub(super) f: F,
1941+
iter: I,
1942+
f: F,
1943+
}
1944+
impl<I, F> Inspect<I, F> {
1945+
pub(super) fn new(iter: I, f: F) -> Inspect<I, F> {
1946+
Inspect { iter, f }
1947+
}
18981948
}
18991949

19001950
#[stable(feature = "core_impl_debug", since = "1.9.0")]

src/libcore/iter/traits/iterator.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ pub trait Iterator {
558558
fn map<B, F>(self, f: F) -> Map<Self, F> where
559559
Self: Sized, F: FnMut(Self::Item) -> B,
560560
{
561-
Map { iter: self, f }
561+
Map::new(self, f)
562562
}
563563

564564
/// Calls a closure on each element of an iterator.
@@ -669,7 +669,7 @@ pub trait Iterator {
669669
fn filter<P>(self, predicate: P) -> Filter<Self, P> where
670670
Self: Sized, P: FnMut(&Self::Item) -> bool,
671671
{
672-
Filter {iter: self, predicate }
672+
Filter::new(self, predicate)
673673
}
674674

675675
/// Creates an iterator that both filters and maps.
@@ -726,7 +726,7 @@ pub trait Iterator {
726726
fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F> where
727727
Self: Sized, F: FnMut(Self::Item) -> Option<B>,
728728
{
729-
FilterMap { iter: self, f }
729+
FilterMap::new(self, f)
730730
}
731731

732732
/// Creates an iterator which gives the current iteration count as well as
@@ -981,7 +981,7 @@ pub trait Iterator {
981981
#[inline]
982982
#[stable(feature = "rust1", since = "1.0.0")]
983983
fn skip(self, n: usize) -> Skip<Self> where Self: Sized {
984-
Skip { iter: self, n }
984+
Skip::new(self, n)
985985
}
986986

987987
/// Creates an iterator that yields its first `n` elements.
@@ -1013,7 +1013,7 @@ pub trait Iterator {
10131013
#[inline]
10141014
#[stable(feature = "rust1", since = "1.0.0")]
10151015
fn take(self, n: usize) -> Take<Self> where Self: Sized, {
1016-
Take { iter: self, n }
1016+
Take::new(self, n)
10171017
}
10181018

10191019
/// An iterator adaptor similar to [`fold`] that holds internal state and
@@ -1058,7 +1058,7 @@ pub trait Iterator {
10581058
fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F>
10591059
where Self: Sized, F: FnMut(&mut St, Self::Item) -> Option<B>,
10601060
{
1061-
Scan { iter: self, f, state: initial_state }
1061+
Scan::new(self, initial_state, f)
10621062
}
10631063

10641064
/// Creates an iterator that works like map, but flattens nested structure.
@@ -1307,7 +1307,7 @@ pub trait Iterator {
13071307
fn inspect<F>(self, f: F) -> Inspect<Self, F> where
13081308
Self: Sized, F: FnMut(&Self::Item),
13091309
{
1310-
Inspect { iter: self, f }
1310+
Inspect::new(self, f)
13111311
}
13121312

13131313
/// Borrows an iterator, rather than consuming it.
@@ -2181,7 +2181,7 @@ pub trait Iterator {
21812181
#[inline]
21822182
#[stable(feature = "rust1", since = "1.0.0")]
21832183
fn rev(self) -> Rev<Self> where Self: Sized + DoubleEndedIterator {
2184-
Rev{iter: self}
2184+
Rev::new(self)
21852185
}
21862186

21872187
/// Converts an iterator of pairs into a pair of containers.
@@ -2249,7 +2249,7 @@ pub trait Iterator {
22492249
fn copied<'a, T: 'a>(self) -> Copied<Self>
22502250
where Self: Sized + Iterator<Item=&'a T>, T: Copy
22512251
{
2252-
Copied { it: self }
2252+
Copied::new(self)
22532253
}
22542254

22552255
/// Creates an iterator which [`clone`]s all of its elements.
@@ -2278,7 +2278,7 @@ pub trait Iterator {
22782278
fn cloned<'a, T: 'a>(self) -> Cloned<Self>
22792279
where Self: Sized + Iterator<Item=&'a T>, T: Clone
22802280
{
2281-
Cloned { it: self }
2281+
Cloned::new(self)
22822282
}
22832283

22842284
/// Repeats an iterator endlessly.

0 commit comments

Comments
 (0)