@@ -22,7 +22,7 @@ use self::zip::try_get_unchecked;
22
22
pub use self :: zip:: TrustedRandomAccess ;
23
23
pub use self :: zip:: Zip ;
24
24
25
- /// This trait provides transitive access to source-stages in an interator-adapter pipeline
25
+ /// This trait provides transitive access to source-stage in an interator-adapter pipeline
26
26
/// under the conditions that
27
27
/// * the iterator source `S` itself implements `SourceIter<Source = S>`
28
28
/// * there is a delegating implementation of this trait for each adapter in the pipeline between
@@ -49,40 +49,44 @@ pub use self::zip::Zip;
49
49
///
50
50
/// let mut iter = vec![9, 9, 9].into_iter().map(|i| i * i);
51
51
/// let _ = iter.next();
52
- /// let mut remainder = std::mem::replace(iter.as_inner(), Vec::new().into_iter());
52
+ /// let mut remainder = std::mem::replace(unsafe { iter.as_inner() } , Vec::new().into_iter());
53
53
/// println!("n = {} elements remaining", remainder.len());
54
54
/// ```
55
55
///
56
- /// [`FromIterator`]: trait. FromIterator.html
57
- /// [`as_inner`]: #method. as_inner
56
+ /// [`FromIterator`]: crate::iter:: FromIterator
57
+ /// [`as_inner`]: SourceIter:: as_inner
58
58
#[ unstable( issue = "0" , feature = "inplace_iteration" ) ]
59
59
pub unsafe trait SourceIter {
60
60
/// A source stage in an iterator pipeline.
61
61
type Source : Iterator ;
62
62
63
- /// Extract the source of an iterator pipeline.
63
+ /// Retrieve the source of an iterator pipeline.
64
64
///
65
- /// Callers may assume that calls to [`next()`] or any method taking `&self`
66
- /// does no replace the referenced value.
67
- /// But callers may replace the referenced values as long they in turn do not
68
- /// expose it through a delegating implementation of this trait.
69
- /// Which means that while adapters may not modify the reference they cannot
70
- /// rely on it not being modified.
65
+ /// # Safety
71
66
///
72
- /// Adapters must not rely on exclusive ownership or immutability of the source.
73
- /// The lack of exclusive ownership also requires that adapters must uphold the source's
74
- /// public API even when they have crate- or module-internal access.
67
+ /// Implementations of must return the same mutable reference for their lifetime, unless
68
+ /// replaced by a caller.
69
+ /// Callers may only replace the reference when they stopped iteration and drop the
70
+ /// iterator pipeline after extracting the source.
71
+ ///
72
+ /// This means iterator adapters can rely on the source not changing during
73
+ /// iteration but they cannot rely on it in their Drop implementations.
74
+ ///
75
+ /// Implementing this method means adapters relinquish private-only access to their
76
+ /// source and can only rely on guarantees made based on method receiver types.
77
+ /// The lack of restricted access also requires that adapters must uphold the source's
78
+ /// public API even when they have access to its internals.
75
79
///
76
80
/// Callers in turn must expect the source to be in any state that is consistent with
77
81
/// its public API since adapters sitting between it and the source have the same
78
82
/// access. In particular an adapter may have consumed more elements than strictly necessary.
79
83
///
80
- /// The overall goal of these requirements is to grant the consumer of a pipeline
81
- /// access to the underlying storage of an iterator while restricting any statefulness
82
- /// and side-effects of the pipeline stages from affecting or relying on that storage.
84
+ /// The overall goal of these requirements is to let the consumer of a pipeline use
85
+ /// * whatever remains in the source after iteration has stopped
86
+ /// * the memory that has become unused by advancing a consuming iterator
83
87
///
84
88
/// [`next()`]: trait.Iterator.html#method.next
85
- fn as_inner ( & mut self ) -> & mut Self :: Source ;
89
+ unsafe fn as_inner ( & mut self ) -> & mut Self :: Source ;
86
90
}
87
91
88
92
/// A double-ended iterator with the direction inverted.
@@ -1015,8 +1019,9 @@ where
1015
1019
type Source = S ;
1016
1020
1017
1021
#[ inline]
1018
- fn as_inner ( & mut self ) -> & mut S {
1019
- SourceIter :: as_inner ( & mut self . iter )
1022
+ unsafe fn as_inner ( & mut self ) -> & mut S {
1023
+ // Safety: unsafe function forwarding to unsafe function with the same requirements
1024
+ unsafe { SourceIter :: as_inner ( & mut self . iter ) }
1020
1025
}
1021
1026
}
1022
1027
@@ -1162,8 +1167,9 @@ unsafe impl<S: Iterator, P, I: Iterator> SourceIter for Filter<I, P> where
1162
1167
type Source = S ;
1163
1168
1164
1169
#[ inline]
1165
- fn as_inner ( & mut self ) -> & mut S {
1166
- SourceIter :: as_inner ( & mut self . iter )
1170
+ unsafe fn as_inner ( & mut self ) -> & mut S {
1171
+ // Safety: unsafe function forwarding to unsafe function with the same requirements
1172
+ unsafe { SourceIter :: as_inner ( & mut self . iter ) }
1167
1173
}
1168
1174
}
1169
1175
@@ -1305,8 +1311,9 @@ unsafe impl<S: Iterator, B, I: Iterator, F> SourceIter for FilterMap<I, F> where
1305
1311
type Source = S ;
1306
1312
1307
1313
#[ inline]
1308
- fn as_inner ( & mut self ) -> & mut S {
1309
- SourceIter :: as_inner ( & mut self . iter )
1314
+ unsafe fn as_inner ( & mut self ) -> & mut S {
1315
+ // Safety: unsafe function forwarding to unsafe function with the same requirements
1316
+ unsafe { SourceIter :: as_inner ( & mut self . iter ) }
1310
1317
}
1311
1318
}
1312
1319
@@ -1541,8 +1548,9 @@ where
1541
1548
type Source = S ;
1542
1549
1543
1550
#[ inline]
1544
- fn as_inner ( & mut self ) -> & mut S {
1545
- SourceIter :: as_inner ( & mut self . iter )
1551
+ unsafe fn as_inner ( & mut self ) -> & mut S {
1552
+ // Safety: unsafe function forwarding to unsafe function with the same requirements
1553
+ unsafe { SourceIter :: as_inner ( & mut self . iter ) }
1546
1554
}
1547
1555
}
1548
1556
@@ -1838,8 +1846,9 @@ where
1838
1846
type Source = S ;
1839
1847
1840
1848
#[ inline]
1841
- fn as_inner ( & mut self ) -> & mut S {
1842
- SourceIter :: as_inner ( & mut self . iter )
1849
+ unsafe fn as_inner ( & mut self ) -> & mut S {
1850
+ // Safety: unsafe function forwarding to unsafe function with the same requirements
1851
+ unsafe { SourceIter :: as_inner ( & mut self . iter ) }
1843
1852
}
1844
1853
}
1845
1854
@@ -1955,8 +1964,9 @@ unsafe impl<S: Iterator, P, I: Iterator> SourceIter for SkipWhile<I, P> where
1955
1964
type Source = S ;
1956
1965
1957
1966
#[ inline]
1958
- fn as_inner ( & mut self ) -> & mut S {
1959
- SourceIter :: as_inner ( & mut self . iter )
1967
+ unsafe fn as_inner ( & mut self ) -> & mut S {
1968
+ // Safety: unsafe function forwarding to unsafe function with the same requirements
1969
+ unsafe { SourceIter :: as_inner ( & mut self . iter ) }
1960
1970
}
1961
1971
}
1962
1972
@@ -2163,8 +2173,9 @@ unsafe impl<S: Iterator, P, I: Iterator> SourceIter for TakeWhile<I, P> where
2163
2173
type Source = S ;
2164
2174
2165
2175
#[ inline]
2166
- fn as_inner ( & mut self ) -> & mut S {
2167
- SourceIter :: as_inner ( & mut self . iter )
2176
+ unsafe fn as_inner ( & mut self ) -> & mut S {
2177
+ // Safety: unsafe function forwarding to unsafe function with the same requirements
2178
+ unsafe { SourceIter :: as_inner ( & mut self . iter ) }
2168
2179
}
2169
2180
}
2170
2181
@@ -2364,8 +2375,9 @@ where
2364
2375
type Source = S ;
2365
2376
2366
2377
#[ inline]
2367
- fn as_inner ( & mut self ) -> & mut S {
2368
- SourceIter :: as_inner ( & mut self . iter )
2378
+ unsafe fn as_inner ( & mut self ) -> & mut S {
2379
+ // Safety: unsafe function forwarding to unsafe function with the same requirements
2380
+ unsafe { SourceIter :: as_inner ( & mut self . iter ) }
2369
2381
}
2370
2382
}
2371
2383
@@ -2487,8 +2499,9 @@ unsafe impl<S: Iterator, I: Iterator> SourceIter for Take<I> where I: SourceIter
2487
2499
type Source = S ;
2488
2500
2489
2501
#[ inline]
2490
- fn as_inner ( & mut self ) -> & mut S {
2491
- SourceIter :: as_inner ( & mut self . iter )
2502
+ unsafe fn as_inner ( & mut self ) -> & mut S {
2503
+ // Safety: unsafe function forwarding to unsafe function with the same requirements
2504
+ unsafe { SourceIter :: as_inner ( & mut self . iter ) }
2492
2505
}
2493
2506
}
2494
2507
@@ -2667,8 +2680,9 @@ unsafe impl<St, F, B, S: Iterator, I: Iterator> SourceIter for Scan<I, St, F>
2667
2680
type Source = S ;
2668
2681
2669
2682
#[ inline]
2670
- fn as_inner ( & mut self ) -> & mut S {
2671
- SourceIter :: as_inner ( & mut self . iter )
2683
+ unsafe fn as_inner ( & mut self ) -> & mut S {
2684
+ // Safety: unsafe function forwarding to unsafe function with the same requirements
2685
+ unsafe { SourceIter :: as_inner ( & mut self . iter ) }
2672
2686
}
2673
2687
}
2674
2688
@@ -2831,8 +2845,9 @@ unsafe impl<S: Iterator, I: Iterator, F> SourceIter for Inspect<I, F> where
2831
2845
type Source = S ;
2832
2846
2833
2847
#[ inline]
2834
- fn as_inner ( & mut self ) -> & mut S {
2835
- SourceIter :: as_inner ( & mut self . iter )
2848
+ unsafe fn as_inner ( & mut self ) -> & mut S {
2849
+ // Safety: unsafe function forwarding to unsafe function with the same requirements
2850
+ unsafe { SourceIter :: as_inner ( & mut self . iter ) }
2836
2851
}
2837
2852
}
2838
2853
0 commit comments