]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #41065 - jorendorff:slice-rsplit-41020, r=alexcrichton
authorAriel Ben-Yehuda <arielb1@mail.tau.ac.il>
Wed, 5 Apr 2017 23:01:13 +0000 (23:01 +0000)
committerGitHub <noreply@github.com>
Wed, 5 Apr 2017 23:01:13 +0000 (23:01 +0000)
[T]::rsplit() and rsplit_mut(), #41020

1  2 
src/doc/unstable-book/src/SUMMARY.md
src/libcollections/lib.rs
src/libcore/slice/mod.rs

index b424dd686afe72baae64df8ab3af4fde0070e012,52f33244695eaf858017b94ac4c28fa59cfdf860..9ce097e78a4e530a6afd1979b2b4eb30ba43e09c
  - [no_debug](no-debug.md)
  - [non_ascii_idents](non-ascii-idents.md)
  - [nonzero](nonzero.md)
 +- [offset_to](offset-to.md)
  - [omit_gdb_pretty_printer_section](omit-gdb-pretty-printer-section.md)
  - [on_unimplemented](on-unimplemented.md)
  - [once_poison](once-poison.md)
  - [slice_concat_ext](slice-concat-ext.md)
  - [slice_get_slice](slice-get-slice.md)
  - [slice_patterns](slice-patterns.md)
+ - [slice_rsplit](slice-rsplit.md)
  - [sort_internals](sort-internals.md)
  - [sort_unstable](sort-unstable.md)
  - [specialization](specialization.md)
  - [windows_handle](windows-handle.md)
  - [windows_net](windows-net.md)
  - [windows_stdio](windows-stdio.md)
 -- [windows_subsystem](windows-subsystem.md)
  - [zero_one](zero-one.md)
index 534d7cc7c7ec2d5053e74bd7e919aaafbe1afc21,ff020f53e0e77f37aa41c2d9a173108e39fc566f..248c15e96f8f65f3b432fbe7db9dc7645823c1d5
@@@ -52,6 -52,7 +52,7 @@@
  #![feature(shared)]
  #![feature(slice_get_slice)]
  #![feature(slice_patterns)]
+ #![feature(slice_rsplit)]
  #![cfg_attr(not(test), feature(sort_unstable))]
  #![feature(specialization)]
  #![feature(staged_api)]
@@@ -62,7 -63,6 +63,7 @@@
  #![feature(untagged_unions)]
  #![cfg_attr(not(test), feature(str_checked_slicing))]
  #![cfg_attr(test, feature(rand, test))]
 +#![feature(offset_to)]
  
  #![no_std]
  
diff --combined src/libcore/slice/mod.rs
index a425e418e42f15426a849705c75b3827c4b5395a,6e3f11ec7fbaec1bb5ab96bee7649d54d4283882..6d598677c9ba4f0cad90dc5bf1458d1d94433f58
@@@ -81,6 -81,10 +81,10 @@@ pub trait SliceExt 
      fn split<P>(&self, pred: P) -> Split<Self::Item, P>
          where P: FnMut(&Self::Item) -> bool;
  
+     #[unstable(feature = "slice_rsplit", issue = "41020")]
+     fn rsplit<P>(&self, pred: P) -> RSplit<Self::Item, P>
+         where P: FnMut(&Self::Item) -> bool;
      #[stable(feature = "core", since = "1.6.0")]
      fn splitn<P>(&self, n: usize, pred: P) -> SplitN<Self::Item, P>
          where P: FnMut(&Self::Item) -> bool;
      fn split_mut<P>(&mut self, pred: P) -> SplitMut<Self::Item, P>
          where P: FnMut(&Self::Item) -> bool;
  
+     #[unstable(feature = "slice_rsplit", issue = "41020")]
+     fn rsplit_mut<P>(&mut self, pred: P) -> RSplitMut<Self::Item, P>
+         where P: FnMut(&Self::Item) -> bool;
      #[stable(feature = "core", since = "1.6.0")]
      fn splitn_mut<P>(&mut self, n: usize, pred: P) -> SplitNMut<Self::Item, P>
          where P: FnMut(&Self::Item) -> bool;
@@@ -293,6 -301,13 +301,13 @@@ impl<T> SliceExt for [T] 
          }
      }
  
+     #[inline]
+     fn rsplit<P>(&self, pred: P) -> RSplit<T, P>
+         where P: FnMut(&T) -> bool
+     {
+         RSplit { inner: self.split(pred) }
+     }
      #[inline]
      fn splitn<P>(&self, n: usize, pred: P) -> SplitN<T, P>
          where P: FnMut(&T) -> bool
          SplitN {
              inner: GenericSplitN {
                  iter: self.split(pred),
-                 count: n,
-                 invert: false
+                 count: n
              }
          }
      }
      {
          RSplitN {
              inner: GenericSplitN {
-                 iter: self.split(pred),
-                 count: n,
-                 invert: true
+                 iter: self.rsplit(pred),
+                 count: n
              }
          }
      }
          SplitMut { v: self, pred: pred, finished: false }
      }
  
+     #[inline]
+     fn rsplit_mut<P>(&mut self, pred: P) -> RSplitMut<T, P>
+         where P: FnMut(&T) -> bool
+     {
+         RSplitMut { inner: self.split_mut(pred) }
+     }
      #[inline]
      fn splitn_mut<P>(&mut self, n: usize, pred: P) -> SplitNMut<T, P>
          where P: FnMut(&T) -> bool
          SplitNMut {
              inner: GenericSplitN {
                  iter: self.split_mut(pred),
-                 count: n,
-                 invert: false
+                 count: n
              }
          }
      }
      {
          RSplitNMut {
              inner: GenericSplitN {
-                 iter: self.split_mut(pred),
-                 count: n,
-                 invert: true
+                 iter: self.rsplit_mut(pred),
+                 count: n
              }
          }
      }
@@@ -1498,10 -1516,9 +1516,10 @@@ unsafe impl<'a, T> TrustedLen for IterM
  // Return the arithmetic difference if `T` is zero size.
  #[inline(always)]
  fn ptrdistance<T>(start: *const T, end: *const T) -> usize {
 -    let diff = (end as usize).wrapping_sub(start as usize);
 -    let size = mem::size_of::<T>();
 -    diff / (if size == 0 { 1 } else { size })
 +    match start.offset_to(end) {
 +        Some(x) => x as usize,
 +        None => (end as usize).wrapping_sub(start as usize),
 +    }
  }
  
  // Extension methods for raw pointers, used by the iterators
@@@ -1736,6 -1753,123 +1754,123 @@@ impl<'a, T, P> DoubleEndedIterator for 
  #[unstable(feature = "fused", issue = "35602")]
  impl<'a, T, P> FusedIterator for SplitMut<'a, T, P> where P: FnMut(&T) -> bool {}
  
+ /// An iterator over subslices separated by elements that match a predicate
+ /// function, starting from the end of the slice.
+ ///
+ /// This struct is created by the [`rsplit`] method on [slices].
+ ///
+ /// [`rsplit`]: ../../std/primitive.slice.html#method.rsplit
+ /// [slices]: ../../std/primitive.slice.html
+ #[unstable(feature = "slice_rsplit", issue = "41020")]
+ #[derive(Clone)] // Is this correct, or does it incorrectly require `T: Clone`?
+ pub struct RSplit<'a, T:'a, P> where P: FnMut(&T) -> bool {
+     inner: Split<'a, T, P>
+ }
+ #[unstable(feature = "slice_rsplit", issue = "41020")]
+ impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for RSplit<'a, T, P> where P: FnMut(&T) -> bool {
+     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+         f.debug_struct("RSplit")
+             .field("v", &self.inner.v)
+             .field("finished", &self.inner.finished)
+             .finish()
+     }
+ }
+ #[unstable(feature = "slice_rsplit", issue = "41020")]
+ impl<'a, T, P> Iterator for RSplit<'a, T, P> where P: FnMut(&T) -> bool {
+     type Item = &'a [T];
+     #[inline]
+     fn next(&mut self) -> Option<&'a [T]> {
+         self.inner.next_back()
+     }
+     #[inline]
+     fn size_hint(&self) -> (usize, Option<usize>) {
+         self.inner.size_hint()
+     }
+ }
+ #[unstable(feature = "slice_rsplit", issue = "41020")]
+ impl<'a, T, P> DoubleEndedIterator for RSplit<'a, T, P> where P: FnMut(&T) -> bool {
+     #[inline]
+     fn next_back(&mut self) -> Option<&'a [T]> {
+         self.inner.next()
+     }
+ }
+ #[unstable(feature = "slice_rsplit", issue = "41020")]
+ impl<'a, T, P> SplitIter for RSplit<'a, T, P> where P: FnMut(&T) -> bool {
+     #[inline]
+     fn finish(&mut self) -> Option<&'a [T]> {
+         self.inner.finish()
+     }
+ }
+ //#[unstable(feature = "fused", issue = "35602")]
+ #[unstable(feature = "slice_rsplit", issue = "41020")]
+ impl<'a, T, P> FusedIterator for RSplit<'a, T, P> where P: FnMut(&T) -> bool {}
+ /// An iterator over the subslices of the vector which are separated
+ /// by elements that match `pred`, starting from the end of the slice.
+ ///
+ /// This struct is created by the [`rsplit_mut`] method on [slices].
+ ///
+ /// [`rsplit_mut`]: ../../std/primitive.slice.html#method.rsplit_mut
+ /// [slices]: ../../std/primitive.slice.html
+ #[unstable(feature = "slice_rsplit", issue = "41020")]
+ pub struct RSplitMut<'a, T:'a, P> where P: FnMut(&T) -> bool {
+     inner: SplitMut<'a, T, P>
+ }
+ #[unstable(feature = "slice_rsplit", issue = "41020")]
+ impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for RSplitMut<'a, T, P> where P: FnMut(&T) -> bool {
+     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+         f.debug_struct("RSplitMut")
+             .field("v", &self.inner.v)
+             .field("finished", &self.inner.finished)
+             .finish()
+     }
+ }
+ #[unstable(feature = "slice_rsplit", issue = "41020")]
+ impl<'a, T, P> SplitIter for RSplitMut<'a, T, P> where P: FnMut(&T) -> bool {
+     #[inline]
+     fn finish(&mut self) -> Option<&'a mut [T]> {
+         self.inner.finish()
+     }
+ }
+ #[unstable(feature = "slice_rsplit", issue = "41020")]
+ impl<'a, T, P> Iterator for RSplitMut<'a, T, P> where P: FnMut(&T) -> bool {
+     type Item = &'a mut [T];
+     #[inline]
+     fn next(&mut self) -> Option<&'a mut [T]> {
+         self.inner.next_back()
+     }
+     #[inline]
+     fn size_hint(&self) -> (usize, Option<usize>) {
+         self.inner.size_hint()
+     }
+ }
+ #[unstable(feature = "slice_rsplit", issue = "41020")]
+ impl<'a, T, P> DoubleEndedIterator for RSplitMut<'a, T, P> where
+     P: FnMut(&T) -> bool,
+ {
+     #[inline]
+     fn next_back(&mut self) -> Option<&'a mut [T]> {
+         self.inner.next()
+     }
+ }
+ //#[unstable(feature = "fused", issue = "35602")]
+ #[unstable(feature = "slice_rsplit", issue = "41020")]
+ impl<'a, T, P> FusedIterator for RSplitMut<'a, T, P> where P: FnMut(&T) -> bool {}
  /// An private iterator over subslices separated by elements that
  /// match a predicate function, splitting at most a fixed number of
  /// times.
  struct GenericSplitN<I> {
      iter: I,
      count: usize,
-     invert: bool
  }
  
  impl<T, I: SplitIter<Item=T>> Iterator for GenericSplitN<I> {
          match self.count {
              0 => None,
              1 => { self.count -= 1; self.iter.finish() }
-             _ => {
-                 self.count -= 1;
-                 if self.invert {self.iter.next_back()} else {self.iter.next()}
-             }
+             _ => { self.count -= 1; self.iter.next() }
          }
      }
  
@@@ -1799,7 -1929,7 +1930,7 @@@ impl<'a, T: 'a + fmt::Debug, P> fmt::De
  /// [slices]: ../../std/primitive.slice.html
  #[stable(feature = "rust1", since = "1.0.0")]
  pub struct RSplitN<'a, T: 'a, P> where P: FnMut(&T) -> bool {
-     inner: GenericSplitN<Split<'a, T, P>>
+     inner: GenericSplitN<RSplit<'a, T, P>>
  }
  
  #[stable(feature = "core_impl_debug", since = "1.9.0")]
@@@ -1842,7 -1972,7 +1973,7 @@@ impl<'a, T: 'a + fmt::Debug, P> fmt::De
  /// [slices]: ../../std/primitive.slice.html
  #[stable(feature = "rust1", since = "1.0.0")]
  pub struct RSplitNMut<'a, T: 'a, P> where P: FnMut(&T) -> bool {
-     inner: GenericSplitN<SplitMut<'a, T, P>>
+     inner: GenericSplitN<RSplitMut<'a, T, P>>
  }
  
  #[stable(feature = "core_impl_debug", since = "1.9.0")]