@@ -20,7 +20,7 @@ pub use self::zip::Zip;
20
20
#[ unstable( issue = "0" , feature = "std_internals" ) ]
21
21
pub use self :: zip:: TrustedRandomAccess ;
22
22
23
- /// This trait provides transitive access to source-stages in an interator-adapter pipeline
23
+ /// This trait provides transitive access to source-stage in an interator-adapter pipeline
24
24
/// under the conditions that
25
25
/// * the iterator source `S` itself implements `SourceIter<Source = S>`
26
26
/// * there is a delegating implementation of this trait for each adapter in the pipeline between
@@ -47,7 +47,7 @@ pub use self::zip::TrustedRandomAccess;
47
47
///
48
48
/// let mut iter = vec![9, 9, 9].into_iter().map(|i| i * i);
49
49
/// let _ = iter.next();
50
- /// let mut remainder = std::mem::replace(iter.as_inner(), Vec::new().into_iter());
50
+ /// let mut remainder = std::mem::replace(unsafe { iter.as_inner() } , Vec::new().into_iter());
51
51
/// println!("n = {} elements remaining", remainder.len());
52
52
/// ```
53
53
///
@@ -58,29 +58,33 @@ pub unsafe trait SourceIter {
58
58
/// A source stage in an iterator pipeline.
59
59
type Source : Iterator ;
60
60
61
- /// Extract the source of an iterator pipeline.
61
+ /// Retrieve the source of an iterator pipeline.
62
62
///
63
- /// Callers may assume that calls to [`next()`] or any method taking `&self`
64
- /// does no replace the referenced value.
65
- /// But callers may replace the referenced values as long they in turn do not
66
- /// expose it through a delegating implementation of this trait.
67
- /// Which means that while adapters may not modify the reference they cannot
68
- /// rely on it not being modified.
63
+ /// # Safety
69
64
///
70
- /// Adapters must not rely on exclusive ownership or immutability of the source.
71
- /// The lack of exclusive ownership also requires that adapters must uphold the source's
72
- /// public API even when they have crate- or module-internal access.
65
+ /// Implementations of must return the same mutable reference for their lifetime, unless
66
+ /// replaced by a caller.
67
+ /// Callers may only replace the reference when they stopped iteration and drop the
68
+ /// iterator pipeline after extracting the source.
69
+ ///
70
+ /// This means iterator adapters can rely on the source not changing during
71
+ /// iteration but they cannot rely on it in their Drop implementations.
72
+ ///
73
+ /// Implementing this method means adapters relinquish private-only access to their
74
+ /// source and can only rely on guarantees made based on method receiver types.
75
+ /// The lack of restricted access also requires that adapters must uphold the source's
76
+ /// public API even when they have access to its internals.
73
77
///
74
78
/// Callers in turn must expect the source to be in any state that is consistent with
75
79
/// its public API since adapters sitting between it and the source have the same
76
80
/// access. In particular an adapter may have consumed more elements than strictly necessary.
77
81
///
78
- /// The overall goal of these requirements is to grant the consumer of a pipeline
79
- /// access to the underlying storage of an iterator while restricting any statefulness
80
- /// and side-effects of the pipeline stages from affecting or relying on that storage.
82
+ /// The overall goal of these requirements is to let the consumer of a pipeline use
83
+ /// * whatever remains in the source after iteration has stopped
84
+ /// * the memory that has become unused by advancing a consuming iterator
81
85
///
82
86
/// [`next()`]: trait.Iterator.html#method.next
83
- fn as_inner ( & mut self ) -> & mut Self :: Source ;
87
+ unsafe fn as_inner ( & mut self ) -> & mut Self :: Source ;
84
88
}
85
89
86
90
/// A double-ended iterator with the direction inverted.
@@ -959,7 +963,7 @@ where
959
963
type Source = S ;
960
964
961
965
#[ inline]
962
- fn as_inner ( & mut self ) -> & mut S {
966
+ unsafe fn as_inner ( & mut self ) -> & mut S {
963
967
SourceIter :: as_inner ( & mut self . iter )
964
968
}
965
969
}
@@ -1106,7 +1110,7 @@ unsafe impl<S: Iterator, P, I: Iterator> SourceIter for Filter<I, P> where
1106
1110
type Source = S ;
1107
1111
1108
1112
#[ inline]
1109
- fn as_inner ( & mut self ) -> & mut S {
1113
+ unsafe fn as_inner ( & mut self ) -> & mut S {
1110
1114
SourceIter :: as_inner ( & mut self . iter )
1111
1115
}
1112
1116
}
@@ -1249,7 +1253,7 @@ unsafe impl<S: Iterator, B, I: Iterator, F> SourceIter for FilterMap<I, F> where
1249
1253
type Source = S ;
1250
1254
1251
1255
#[ inline]
1252
- fn as_inner ( & mut self ) -> & mut S {
1256
+ unsafe fn as_inner ( & mut self ) -> & mut S {
1253
1257
SourceIter :: as_inner ( & mut self . iter )
1254
1258
}
1255
1259
}
@@ -1479,7 +1483,7 @@ where
1479
1483
type Source = S ;
1480
1484
1481
1485
#[ inline]
1482
- fn as_inner ( & mut self ) -> & mut S {
1486
+ unsafe fn as_inner ( & mut self ) -> & mut S {
1483
1487
SourceIter :: as_inner ( & mut self . iter )
1484
1488
}
1485
1489
}
@@ -1709,7 +1713,7 @@ where
1709
1713
type Source = S ;
1710
1714
1711
1715
#[ inline]
1712
- fn as_inner ( & mut self ) -> & mut S {
1716
+ unsafe fn as_inner ( & mut self ) -> & mut S {
1713
1717
SourceIter :: as_inner ( & mut self . iter )
1714
1718
}
1715
1719
}
@@ -1826,7 +1830,7 @@ unsafe impl<S: Iterator, P, I: Iterator> SourceIter for SkipWhile<I, P> where
1826
1830
type Source = S ;
1827
1831
1828
1832
#[ inline]
1829
- fn as_inner ( & mut self ) -> & mut S {
1833
+ unsafe fn as_inner ( & mut self ) -> & mut S {
1830
1834
SourceIter :: as_inner ( & mut self . iter )
1831
1835
}
1832
1836
}
@@ -2033,7 +2037,7 @@ unsafe impl<S: Iterator, P, I: Iterator> SourceIter for TakeWhile<I, P> where
2033
2037
type Source = S ;
2034
2038
2035
2039
#[ inline]
2036
- fn as_inner ( & mut self ) -> & mut S {
2040
+ unsafe fn as_inner ( & mut self ) -> & mut S {
2037
2041
SourceIter :: as_inner ( & mut self . iter )
2038
2042
}
2039
2043
}
@@ -2232,7 +2236,7 @@ where
2232
2236
type Source = S ;
2233
2237
2234
2238
#[ inline]
2235
- fn as_inner ( & mut self ) -> & mut S {
2239
+ unsafe fn as_inner ( & mut self ) -> & mut S {
2236
2240
SourceIter :: as_inner ( & mut self . iter )
2237
2241
}
2238
2242
}
@@ -2341,7 +2345,7 @@ unsafe impl<S: Iterator, I: Iterator> SourceIter for Take<I> where I: SourceIter
2341
2345
type Source = S ;
2342
2346
2343
2347
#[ inline]
2344
- fn as_inner ( & mut self ) -> & mut S {
2348
+ unsafe fn as_inner ( & mut self ) -> & mut S {
2345
2349
SourceIter :: as_inner ( & mut self . iter )
2346
2350
}
2347
2351
}
@@ -2489,7 +2493,7 @@ unsafe impl<St, F, B, S: Iterator, I: Iterator> SourceIter for Scan<I, St, F>
2489
2493
type Source = S ;
2490
2494
2491
2495
#[ inline]
2492
- fn as_inner ( & mut self ) -> & mut S {
2496
+ unsafe fn as_inner ( & mut self ) -> & mut S {
2493
2497
SourceIter :: as_inner ( & mut self . iter )
2494
2498
}
2495
2499
}
@@ -2763,7 +2767,7 @@ where
2763
2767
type Source = S ;
2764
2768
2765
2769
#[ inline]
2766
- fn as_inner ( & mut self ) -> & mut S {
2770
+ unsafe fn as_inner ( & mut self ) -> & mut S {
2767
2771
SourceIter :: as_inner ( & mut self . iter )
2768
2772
}
2769
2773
}
@@ -2925,7 +2929,7 @@ unsafe impl<S: Iterator, I: Iterator, F> SourceIter for Inspect<I, F> where
2925
2929
type Source = S ;
2926
2930
2927
2931
#[ inline]
2928
- fn as_inner ( & mut self ) -> & mut S {
2932
+ unsafe fn as_inner ( & mut self ) -> & mut S {
2929
2933
SourceIter :: as_inner ( & mut self . iter )
2930
2934
}
2931
2935
}
0 commit comments