From 9c7046573b75b988e8291c6d7bf9c126ba2b7b5a Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 4 Dec 2014 23:47:40 -0500 Subject: [PATCH] libcore: use unboxed closures in the fields of `Splits` --- src/libcore/slice.rs | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index 4e3007b55fe..3dad5458b36 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -43,7 +43,7 @@ use default::Default; use iter::*; use kinds::Copy; use num::Int; -use ops; +use ops::{FnMut, mod}; use option::Option; use option::Option::{None, Some}; use ptr; @@ -105,20 +105,23 @@ pub trait SlicePrelude for Sized? { /// Returns an iterator over subslices separated by elements that match /// `pred`. The matched element is not contained in the subslices. #[unstable = "iterator type may change, waiting on unboxed closures"] - fn split<'a>(&'a self, pred: |&T|: 'a -> bool) -> Splits<'a, T>; + fn split<'a, P>(&'a self, pred: P) -> Splits<'a, T, P> where + P: FnMut(&T) -> bool; /// Returns an iterator over subslices separated by elements that match /// `pred`, limited to splitting at most `n` times. The matched element is /// not contained in the subslices. #[unstable = "iterator type may change"] - fn splitn<'a>(&'a self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN>; + fn splitn<'a, P>(&'a self, n: uint, pred: P) -> SplitsN> where + P: FnMut(&T) -> bool; /// Returns an iterator over subslices separated by elements that match /// `pred` limited to splitting at most `n` times. This starts at the end of /// the slice and works backwards. The matched element is not contained in /// the subslices. #[unstable = "iterator type may change"] - fn rsplitn<'a>(&'a self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN>; + fn rsplitn<'a, P>(&'a self, n: uint, pred: P) -> SplitsN> where + P: FnMut(&T) -> bool; /// Returns an iterator over all contiguous windows of length /// `size`. The windows overlap. If the slice is shorter than @@ -470,7 +473,7 @@ impl SlicePrelude for [T] { } #[inline] - fn split<'a>(&'a self, pred: |&T|: 'a -> bool) -> Splits<'a, T> { + fn split<'a, P>(&'a self, pred: P) -> Splits<'a, T, P> where P: FnMut(&T) -> bool { Splits { v: self, pred: pred, @@ -479,7 +482,9 @@ impl SlicePrelude for [T] { } #[inline] - fn splitn<'a>(&'a self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN> { + fn splitn<'a, P>(&'a self, n: uint, pred: P) -> SplitsN> where + P: FnMut(&T) -> bool, + { SplitsN { iter: self.split(pred), count: n, @@ -488,7 +493,9 @@ impl SlicePrelude for [T] { } #[inline] - fn rsplitn<'a>(&'a self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN> { + fn rsplitn<'a, P>(&'a self, n: uint, pred: P) -> SplitsN> where + P: FnMut(&T) -> bool, + { SplitsN { iter: self.split(pred), count: n, @@ -1271,14 +1278,14 @@ trait SplitsIter: DoubleEndedIterator { /// An iterator over subslices separated by elements that match a predicate /// function. #[experimental = "needs review"] -pub struct Splits<'a, T:'a> { +pub struct Splits<'a, T:'a, P> where P: FnMut(&T) -> bool { v: &'a [T], - pred: |t: &T|: 'a -> bool, + pred: P, finished: bool } #[experimental = "needs review"] -impl<'a, T> Iterator<&'a [T]> for Splits<'a, T> { +impl<'a, T, P> Iterator<&'a [T]> for Splits<'a, T, P> where P: FnMut(&T) -> bool { #[inline] fn next(&mut self) -> Option<&'a [T]> { if self.finished { return None; } @@ -1304,7 +1311,7 @@ impl<'a, T> Iterator<&'a [T]> for Splits<'a, T> { } #[experimental = "needs review"] -impl<'a, T> DoubleEndedIterator<&'a [T]> for Splits<'a, T> { +impl<'a, T, P> DoubleEndedIterator<&'a [T]> for Splits<'a, T, P> where P: FnMut(&T) -> bool { #[inline] fn next_back(&mut self) -> Option<&'a [T]> { if self.finished { return None; } @@ -1320,7 +1327,7 @@ impl<'a, T> DoubleEndedIterator<&'a [T]> for Splits<'a, T> { } } -impl<'a, T> SplitsIter<&'a [T]> for Splits<'a, T> { +impl<'a, T, P> SplitsIter<&'a [T]> for Splits<'a, T, P> where P: FnMut(&T) -> bool { #[inline] fn finish(&mut self) -> Option<&'a [T]> { if self.finished { None } else { self.finished = true; Some(self.v) }