@@ -81,6 +81,10 @@ pub trait SliceExt {
81
81
fn split < P > ( & self , pred : P ) -> Split < Self :: Item , P >
82
82
where P : FnMut ( & Self :: Item ) -> bool ;
83
83
84
+ #[ unstable( feature = "slice_rsplit" , issue = "41020" ) ]
85
+ fn rsplit < P > ( & self , pred : P ) -> RSplit < Self :: Item , P >
86
+ where P : FnMut ( & Self :: Item ) -> bool ;
87
+
84
88
#[ stable( feature = "core" , since = "1.6.0" ) ]
85
89
fn splitn < P > ( & self , n : usize , pred : P ) -> SplitN < Self :: Item , P >
86
90
where P : FnMut ( & Self :: Item ) -> bool ;
@@ -159,6 +163,10 @@ pub trait SliceExt {
159
163
fn split_mut < P > ( & mut self , pred : P ) -> SplitMut < Self :: Item , P >
160
164
where P : FnMut ( & Self :: Item ) -> bool ;
161
165
166
+ #[ unstable( feature = "slice_rsplit" , issue = "41020" ) ]
167
+ fn rsplit_mut < P > ( & mut self , pred : P ) -> RSplitMut < Self :: Item , P >
168
+ where P : FnMut ( & Self :: Item ) -> bool ;
169
+
162
170
#[ stable( feature = "core" , since = "1.6.0" ) ]
163
171
fn splitn_mut < P > ( & mut self , n : usize , pred : P ) -> SplitNMut < Self :: Item , P >
164
172
where P : FnMut ( & Self :: Item ) -> bool ;
@@ -293,15 +301,21 @@ impl<T> SliceExt for [T] {
293
301
}
294
302
}
295
303
304
+ #[ inline]
305
+ fn rsplit < P > ( & self , pred : P ) -> RSplit < T , P >
306
+ where P : FnMut ( & T ) -> bool
307
+ {
308
+ RSplit { inner : self . split ( pred) }
309
+ }
310
+
296
311
#[ inline]
297
312
fn splitn < P > ( & self , n : usize , pred : P ) -> SplitN < T , P >
298
313
where P : FnMut ( & T ) -> bool
299
314
{
300
315
SplitN {
301
316
inner : GenericSplitN {
302
317
iter : self . split ( pred) ,
303
- count : n,
304
- invert : false
318
+ count : n
305
319
}
306
320
}
307
321
}
@@ -312,9 +326,8 @@ impl<T> SliceExt for [T] {
312
326
{
313
327
RSplitN {
314
328
inner : GenericSplitN {
315
- iter : self . split ( pred) ,
316
- count : n,
317
- invert : true
329
+ iter : self . rsplit ( pred) ,
330
+ count : n
318
331
}
319
332
}
320
333
}
@@ -475,15 +488,21 @@ impl<T> SliceExt for [T] {
475
488
SplitMut { v : self , pred : pred, finished : false }
476
489
}
477
490
491
+ #[ inline]
492
+ fn rsplit_mut < P > ( & mut self , pred : P ) -> RSplitMut < T , P >
493
+ where P : FnMut ( & T ) -> bool
494
+ {
495
+ RSplitMut { inner : self . split_mut ( pred) }
496
+ }
497
+
478
498
#[ inline]
479
499
fn splitn_mut < P > ( & mut self , n : usize , pred : P ) -> SplitNMut < T , P >
480
500
where P : FnMut ( & T ) -> bool
481
501
{
482
502
SplitNMut {
483
503
inner : GenericSplitN {
484
504
iter : self . split_mut ( pred) ,
485
- count : n,
486
- invert : false
505
+ count : n
487
506
}
488
507
}
489
508
}
@@ -494,9 +513,8 @@ impl<T> SliceExt for [T] {
494
513
{
495
514
RSplitNMut {
496
515
inner : GenericSplitN {
497
- iter : self . split_mut ( pred) ,
498
- count : n,
499
- invert : true
516
+ iter : self . rsplit_mut ( pred) ,
517
+ count : n
500
518
}
501
519
}
502
520
}
@@ -1736,14 +1754,130 @@ impl<'a, T, P> DoubleEndedIterator for SplitMut<'a, T, P> where
1736
1754
#[ unstable( feature = "fused" , issue = "35602" ) ]
1737
1755
impl < ' a , T , P > FusedIterator for SplitMut < ' a , T , P > where P : FnMut ( & T ) -> bool { }
1738
1756
1757
+ /// An iterator over subslices separated by elements that match a predicate
1758
+ /// function, starting from the end of the slice.
1759
+ ///
1760
+ /// This struct is created by the [`rsplit`] method on [slices].
1761
+ ///
1762
+ /// [`rsplit`]: ../../std/primitive.slice.html#method.rsplit
1763
+ /// [slices]: ../../std/primitive.slice.html
1764
+ #[ unstable( feature = "slice_rsplit" , issue = "41020" ) ]
1765
+ #[ derive( Clone ) ] // Is this correct, or does it incorrectly require `T: Clone`?
1766
+ pub struct RSplit < ' a , T : ' a , P > where P : FnMut ( & T ) -> bool {
1767
+ inner : Split < ' a , T , P >
1768
+ }
1769
+
1770
+ #[ unstable( feature = "slice_rsplit" , issue = "41020" ) ]
1771
+ impl < ' a , T : ' a + fmt:: Debug , P > fmt:: Debug for RSplit < ' a , T , P > where P : FnMut ( & T ) -> bool {
1772
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
1773
+ f. debug_struct ( "RSplit" )
1774
+ . field ( "v" , & self . inner . v )
1775
+ . field ( "finished" , & self . inner . finished )
1776
+ . finish ( )
1777
+ }
1778
+ }
1779
+
1780
+ #[ unstable( feature = "slice_rsplit" , issue = "41020" ) ]
1781
+ impl < ' a , T , P > Iterator for RSplit < ' a , T , P > where P : FnMut ( & T ) -> bool {
1782
+ type Item = & ' a [ T ] ;
1783
+
1784
+ #[ inline]
1785
+ fn next ( & mut self ) -> Option < & ' a [ T ] > {
1786
+ self . inner . next_back ( )
1787
+ }
1788
+
1789
+ #[ inline]
1790
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
1791
+ self . inner . size_hint ( )
1792
+ }
1793
+ }
1794
+
1795
+ #[ unstable( feature = "slice_rsplit" , issue = "41020" ) ]
1796
+ impl < ' a , T , P > DoubleEndedIterator for RSplit < ' a , T , P > where P : FnMut ( & T ) -> bool {
1797
+ #[ inline]
1798
+ fn next_back ( & mut self ) -> Option < & ' a [ T ] > {
1799
+ self . inner . next ( )
1800
+ }
1801
+ }
1802
+
1803
+ #[ unstable( feature = "slice_rsplit" , issue = "41020" ) ]
1804
+ impl < ' a , T , P > SplitIter for RSplit < ' a , T , P > where P : FnMut ( & T ) -> bool {
1805
+ #[ inline]
1806
+ fn finish ( & mut self ) -> Option < & ' a [ T ] > {
1807
+ self . inner . finish ( )
1808
+ }
1809
+ }
1810
+
1811
+ //#[unstable(feature = "fused", issue = "35602")]
1812
+ #[ unstable( feature = "slice_rsplit" , issue = "41020" ) ]
1813
+ impl < ' a , T , P > FusedIterator for RSplit < ' a , T , P > where P : FnMut ( & T ) -> bool { }
1814
+
1815
+ /// An iterator over the subslices of the vector which are separated
1816
+ /// by elements that match `pred`, starting from the end of the slice.
1817
+ ///
1818
+ /// This struct is created by the [`rsplit_mut`] method on [slices].
1819
+ ///
1820
+ /// [`rsplit_mut`]: ../../std/primitive.slice.html#method.rsplit_mut
1821
+ /// [slices]: ../../std/primitive.slice.html
1822
+ #[ unstable( feature = "slice_rsplit" , issue = "41020" ) ]
1823
+ pub struct RSplitMut < ' a , T : ' a , P > where P : FnMut ( & T ) -> bool {
1824
+ inner : SplitMut < ' a , T , P >
1825
+ }
1826
+
1827
+ #[ unstable( feature = "slice_rsplit" , issue = "41020" ) ]
1828
+ impl < ' a , T : ' a + fmt:: Debug , P > fmt:: Debug for RSplitMut < ' a , T , P > where P : FnMut ( & T ) -> bool {
1829
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
1830
+ f. debug_struct ( "RSplitMut" )
1831
+ . field ( "v" , & self . inner . v )
1832
+ . field ( "finished" , & self . inner . finished )
1833
+ . finish ( )
1834
+ }
1835
+ }
1836
+
1837
+ #[ unstable( feature = "slice_rsplit" , issue = "41020" ) ]
1838
+ impl < ' a , T , P > SplitIter for RSplitMut < ' a , T , P > where P : FnMut ( & T ) -> bool {
1839
+ #[ inline]
1840
+ fn finish ( & mut self ) -> Option < & ' a mut [ T ] > {
1841
+ self . inner . finish ( )
1842
+ }
1843
+ }
1844
+
1845
+ #[ unstable( feature = "slice_rsplit" , issue = "41020" ) ]
1846
+ impl < ' a , T , P > Iterator for RSplitMut < ' a , T , P > where P : FnMut ( & T ) -> bool {
1847
+ type Item = & ' a mut [ T ] ;
1848
+
1849
+ #[ inline]
1850
+ fn next ( & mut self ) -> Option < & ' a mut [ T ] > {
1851
+ self . inner . next_back ( )
1852
+ }
1853
+
1854
+ #[ inline]
1855
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
1856
+ self . inner . size_hint ( )
1857
+ }
1858
+ }
1859
+
1860
+ #[ unstable( feature = "slice_rsplit" , issue = "41020" ) ]
1861
+ impl < ' a , T , P > DoubleEndedIterator for RSplitMut < ' a , T , P > where
1862
+ P : FnMut ( & T ) -> bool ,
1863
+ {
1864
+ #[ inline]
1865
+ fn next_back ( & mut self ) -> Option < & ' a mut [ T ] > {
1866
+ self . inner . next ( )
1867
+ }
1868
+ }
1869
+
1870
+ //#[unstable(feature = "fused", issue = "35602")]
1871
+ #[ unstable( feature = "slice_rsplit" , issue = "41020" ) ]
1872
+ impl < ' a , T , P > FusedIterator for RSplitMut < ' a , T , P > where P : FnMut ( & T ) -> bool { }
1873
+
1739
1874
/// An private iterator over subslices separated by elements that
1740
1875
/// match a predicate function, splitting at most a fixed number of
1741
1876
/// times.
1742
1877
#[ derive( Debug ) ]
1743
1878
struct GenericSplitN < I > {
1744
1879
iter : I ,
1745
1880
count : usize ,
1746
- invert : bool
1747
1881
}
1748
1882
1749
1883
impl < T , I : SplitIter < Item =T > > Iterator for GenericSplitN < I > {
@@ -1754,10 +1888,7 @@ impl<T, I: SplitIter<Item=T>> Iterator for GenericSplitN<I> {
1754
1888
match self . count {
1755
1889
0 => None ,
1756
1890
1 => { self . count -= 1 ; self . iter . finish ( ) }
1757
- _ => {
1758
- self . count -= 1 ;
1759
- if self . invert { self . iter . next_back ( ) } else { self . iter . next ( ) }
1760
- }
1891
+ _ => { self . count -= 1 ; self . iter . next ( ) }
1761
1892
}
1762
1893
}
1763
1894
@@ -1799,7 +1930,7 @@ impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for SplitN<'a, T, P> where P: FnMut(&
1799
1930
/// [slices]: ../../std/primitive.slice.html
1800
1931
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1801
1932
pub struct RSplitN < ' a , T : ' a , P > where P : FnMut ( & T ) -> bool {
1802
- inner : GenericSplitN < Split < ' a , T , P > >
1933
+ inner : GenericSplitN < RSplit < ' a , T , P > >
1803
1934
}
1804
1935
1805
1936
#[ stable( feature = "core_impl_debug" , since = "1.9.0" ) ]
@@ -1842,7 +1973,7 @@ impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for SplitNMut<'a, T, P> where P: FnMu
1842
1973
/// [slices]: ../../std/primitive.slice.html
1843
1974
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1844
1975
pub struct RSplitNMut < ' a , T : ' a , P > where P : FnMut ( & T ) -> bool {
1845
- inner : GenericSplitN < SplitMut < ' a , T , P > >
1976
+ inner : GenericSplitN < RSplitMut < ' a , T , P > >
1846
1977
}
1847
1978
1848
1979
#[ stable( feature = "core_impl_debug" , since = "1.9.0" ) ]
0 commit comments