From 56ecb51ba62b973e3a415cc0d6d6979dd7aeee7d Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Wed, 19 Nov 2014 18:06:41 -0500 Subject: libcore: use unboxed closures in `Option` methods --- src/libcore/option.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 5e2d6266f0e..7be47f73e9e 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -156,7 +156,7 @@ use result::Result::{Ok, Err}; use slice; use slice::AsSlice; use clone::Clone; -use ops::Deref; +use ops::{Deref, FnOnce}; // Note that this is not a lang item per se, but it has a hidden dependency on // `Iterator`, which is one. The compiler assumes that the `next` method of @@ -389,7 +389,7 @@ impl Option { /// ``` #[inline] #[unstable = "waiting for conventions"] - pub fn unwrap_or_else(self, f: || -> T) -> T { + pub fn unwrap_or_else T>(self, f: F) -> T { match self { Some(x) => x, None => f() @@ -413,7 +413,7 @@ impl Option { /// ``` #[inline] #[unstable = "waiting for unboxed closures"] - pub fn map(self, f: |T| -> U) -> Option { + pub fn map U>(self, f: F) -> Option { match self { Some(x) => Some(f(x)), None => None @@ -433,7 +433,7 @@ impl Option { /// ``` #[inline] #[unstable = "waiting for unboxed closures"] - pub fn map_or(self, def: U, f: |T| -> U) -> U { + pub fn map_or U>(self, def: U, f: F) -> U { match self { Some(t) => f(t), None => def @@ -455,7 +455,7 @@ impl Option { /// ``` #[inline] #[unstable = "waiting for unboxed closures"] - pub fn map_or_else(self, def: || -> U, f: |T| -> U) -> U { + pub fn map_or_else U, F: FnOnce(T) -> U>(self, def: D, f: F) -> U { match self { Some(t) => f(t), None => def() @@ -497,7 +497,7 @@ impl Option { /// ``` #[inline] #[experimental] - pub fn ok_or_else(self, err: || -> E) -> Result { + pub fn ok_or_else E>(self, err: F) -> Result { match self { Some(v) => Ok(v), None => Err(err()), @@ -615,7 +615,7 @@ impl Option { /// ``` #[inline] #[unstable = "waiting for unboxed closures"] - pub fn and_then(self, f: |T| -> Option) -> Option { + pub fn and_then Option>(self, f: F) -> Option { match self { Some(x) => f(x), None => None, @@ -667,7 +667,7 @@ impl Option { /// ``` #[inline] #[unstable = "waiting for unboxed closures"] - pub fn or_else(self, f: || -> Option) -> Option { + pub fn or_else Option>(self, f: F) -> Option { match self { Some(_) => self, None => f() -- cgit 1.4.1-3-g733a5 From 19524f1ed14d8da8b3d953812a3c3b461085d3dc Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Tue, 2 Dec 2014 12:47:07 -0500 Subject: libcore: use unboxed closures in `Result` methods --- src/libcore/result.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 9d1ffa78911..88d33a59b38 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -239,6 +239,7 @@ use slice::AsSlice; use iter::{Iterator, IteratorExt, DoubleEndedIterator, FromIterator, ExactSizeIterator}; use option::Option; use option::Option::{None, Some}; +use ops::{FnMut, FnOnce}; /// `Result` is a type that represents either success (`Ok`) or failure (`Err`). /// @@ -466,7 +467,7 @@ impl Result { /// ``` #[inline] #[unstable = "waiting for unboxed closures"] - pub fn map(self, op: |T| -> U) -> Result { + pub fn map U>(self, op: F) -> Result { match self { Ok(t) => Ok(op(t)), Err(e) => Err(e) @@ -492,7 +493,7 @@ impl Result { /// ``` #[inline] #[unstable = "waiting for unboxed closures"] - pub fn map_err(self, op: |E| -> F) -> Result { + pub fn map_err F>(self, op: O) -> Result { match self { Ok(t) => Ok(t), Err(e) => Err(op(e)) @@ -612,7 +613,7 @@ impl Result { /// ``` #[inline] #[unstable = "waiting for unboxed closures"] - pub fn and_then(self, op: |T| -> Result) -> Result { + pub fn and_then Result>(self, op: F) -> Result { match self { Ok(t) => op(t), Err(e) => Err(e), @@ -666,7 +667,7 @@ impl Result { /// ``` #[inline] #[unstable = "waiting for unboxed closures"] - pub fn or_else(self, op: |E| -> Result) -> Result { + pub fn or_else Result>(self, op: O) -> Result { match self { Ok(t) => Ok(t), Err(e) => op(e), @@ -708,7 +709,7 @@ impl Result { /// ``` #[inline] #[unstable = "waiting for conventions"] - pub fn unwrap_or_else(self, op: |E| -> T) -> T { + pub fn unwrap_or_else T>(self, op: F) -> T { match self { Ok(t) => t, Err(e) => op(e) @@ -904,10 +905,11 @@ impl> FromIterator> for Result { pub fn fold V, Iter: Iterator>>( mut iterator: Iter, mut init: V, - f: |V, T| -> V) + mut f: F) -> Result { for t in iterator { match t { -- cgit 1.4.1-3-g733a5 From 1646d10edc57ec82536d6253f866084beb69a73e Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Tue, 2 Dec 2014 13:59:08 -0500 Subject: libcore: use unboxed closures in the fields of `Map` --- src/libcore/iter.rs | 29 +++++++++++++++++++---------- src/libcore/str.rs | 16 +++++++++------- 2 files changed, 28 insertions(+), 17 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index ddca9d36bed..0c0562a8b68 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -62,7 +62,7 @@ use cmp::Ord; use kinds::Copy; use mem; use num::{ToPrimitive, Int}; -use ops::{Add, Deref}; +use ops::{Add, Deref, FnMut}; use option::Option; use option::Option::{Some, None}; use uint; @@ -165,7 +165,7 @@ pub trait IteratorExt: Iterator { /// ``` #[inline] #[unstable = "waiting for unboxed closures"] - fn map<'r, B>(self, f: |A|: 'r -> B) -> Map<'r, A, B, Self> { + fn map B>(self, f: F) -> Map { Map{iter: self, f: f} } @@ -778,7 +778,10 @@ impl<'a, A, T: ExactSizeIterator> ExactSizeIterator for Inspect<'a, A, T> #[unstable = "trait is unstable"] impl> ExactSizeIterator for Rev {} #[unstable = "trait is unstable"] -impl<'a, A, B, T: ExactSizeIterator> ExactSizeIterator for Map<'a, A, B, T> {} +impl ExactSizeIterator for Map where + I: ExactSizeIterator, + F: FnMut(A) -> B, +{} #[unstable = "trait is unstable"] impl ExactSizeIterator<(A, B)> for Zip where T: ExactSizeIterator, U: ExactSizeIterator {} @@ -1374,12 +1377,12 @@ RandomAccessIterator<(A, B)> for Zip { /// An iterator which maps the values of `iter` with `f` #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable] -pub struct Map<'a, A, B, T> { - iter: T, - f: |A|: 'a -> B +pub struct Map, F: FnMut(A) -> B> { + iter: I, + f: F, } -impl<'a, A, B, T> Map<'a, A, B, T> { +impl Map where I: Iterator, F: FnMut(A) -> B { #[inline] fn do_map(&mut self, elt: Option) -> Option { match elt { @@ -1390,7 +1393,7 @@ impl<'a, A, B, T> Map<'a, A, B, T> { } #[unstable = "trait is unstable"] -impl<'a, A, B, T: Iterator> Iterator for Map<'a, A, B, T> { +impl Iterator for Map where I: Iterator, F: FnMut(A) -> B { #[inline] fn next(&mut self) -> Option { let next = self.iter.next(); @@ -1404,7 +1407,10 @@ impl<'a, A, B, T: Iterator> Iterator for Map<'a, A, B, T> { } #[unstable = "trait is unstable"] -impl<'a, A, B, T: DoubleEndedIterator> DoubleEndedIterator for Map<'a, A, B, T> { +impl DoubleEndedIterator for Map where + I: DoubleEndedIterator, + F: FnMut(A) -> B, +{ #[inline] fn next_back(&mut self) -> Option { let next = self.iter.next_back(); @@ -1413,7 +1419,10 @@ impl<'a, A, B, T: DoubleEndedIterator> DoubleEndedIterator for Map<'a, A, } #[experimental = "trait is experimental"] -impl<'a, A, B, T: RandomAccessIterator> RandomAccessIterator for Map<'a, A, B, T> { +impl RandomAccessIterator for Map where + I: RandomAccessIterator, + F: FnMut(A) -> B, +{ #[inline] fn indexable(&self) -> uint { self.iter.indexable() diff --git a/src/libcore/str.rs b/src/libcore/str.rs index d0c8558b55d..051c36a2dc0 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -323,8 +323,7 @@ impl<'a> DoubleEndedIterator<(uint, char)> for CharOffsets<'a> { /// External iterator for a string's bytes. /// Use with the `std::iter` module. -pub type Bytes<'a> = - Map<'a, &'a u8, u8, slice::Items<'a, u8>>; +pub type Bytes<'a> = Map<&'a u8, u8, slice::Items<'a, u8>, fn(&u8) -> u8>; /// An iterator over the substrings of a string, separated by `sep`. #[deriving(Clone)] @@ -349,8 +348,7 @@ pub struct CharSplitsN<'a, Sep> { } /// An iterator over the lines of a string, separated by either `\n` or (`\r\n`). -pub type AnyLines<'a> = - Map<'a, &'a str, &'a str, CharSplits<'a, char>>; +pub type AnyLines<'a> = Map<&'a str, &'a str, CharSplits<'a, char>, fn(&str) -> &str>; impl<'a, Sep> CharSplits<'a, Sep> { #[inline] @@ -1980,7 +1978,9 @@ impl StrPrelude for str { #[inline] fn bytes(&self) -> Bytes { - self.as_bytes().iter().map(|&b| b) + fn deref(&x: &u8) -> u8 { x } + + self.as_bytes().iter().map(deref) } #[inline] @@ -2053,11 +2053,13 @@ impl StrPrelude for str { } fn lines_any(&self) -> AnyLines { - self.lines().map(|line| { + fn f(line: &str) -> &str { let l = line.len(); if l > 0 && line.as_bytes()[l - 1] == b'\r' { line.slice(0, l - 1) } else { line } - }) + } + + self.lines().map(f) } #[inline] -- cgit 1.4.1-3-g733a5 From 801ae1333c05ab641ff08c14fee776c08f42cff8 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Tue, 2 Dec 2014 17:49:42 -0500 Subject: libcore: use unboxed closures in the fields of `Filter` --- src/libcore/iter.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 0c0562a8b68..490f4e68bcc 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -183,7 +183,7 @@ pub trait IteratorExt: Iterator { /// ``` #[inline] #[unstable = "waiting for unboxed closures"] - fn filter<'r>(self, predicate: |&A|: 'r -> bool) -> Filter<'r, A, Self> { + fn filter

(self, predicate: P) -> Filter where P: FnMut(&A) -> bool { Filter{iter: self, predicate: predicate} } @@ -1438,13 +1438,13 @@ impl RandomAccessIterator for Map where /// An iterator which filters the elements of `iter` with `predicate` #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable] -pub struct Filter<'a, A, T> { - iter: T, - predicate: |&A|: 'a -> bool +pub struct Filter where I: Iterator, P: FnMut(&A) -> bool { + iter: I, + predicate: P, } #[unstable = "trait is unstable"] -impl<'a, A, T: Iterator> Iterator for Filter<'a, A, T> { +impl Iterator for Filter where I: Iterator, P: FnMut(&A) -> bool { #[inline] fn next(&mut self) -> Option { for x in self.iter { @@ -1465,7 +1465,10 @@ impl<'a, A, T: Iterator> Iterator for Filter<'a, A, T> { } #[unstable = "trait is unstable"] -impl<'a, A, T: DoubleEndedIterator> DoubleEndedIterator for Filter<'a, A, T> { +impl DoubleEndedIterator for Filter where + I: DoubleEndedIterator, + P: FnMut(&A) -> bool, +{ #[inline] fn next_back(&mut self) -> Option { for x in self.iter.by_ref().rev() { -- cgit 1.4.1-3-g733a5 From eede5d2bce06cd0546864b279389ec48e8ad7917 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Tue, 2 Dec 2014 21:00:07 -0500 Subject: libcore: use unboxed closures in the fields of `FilterMap` --- src/libcore/iter.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 490f4e68bcc..b356e42907b 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -201,7 +201,7 @@ pub trait IteratorExt: Iterator { /// ``` #[inline] #[unstable = "waiting for unboxed closures"] - fn filter_map<'r, B>(self, f: |A|: 'r -> Option) -> FilterMap<'r, A, B, Self> { + fn filter_map(self, f: F) -> FilterMap where F: FnMut(A) -> Option { FilterMap { iter: self, f: f } } @@ -1483,13 +1483,16 @@ impl DoubleEndedIterator for Filter where /// An iterator which uses `f` to both filter and map elements from `iter` #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable] -pub struct FilterMap<'a, A, B, T> { - iter: T, - f: |A|: 'a -> Option +pub struct FilterMap where I: Iterator, F: FnMut(A) -> Option { + iter: I, + f: F, } #[unstable = "trait is unstable"] -impl<'a, A, B, T: Iterator> Iterator for FilterMap<'a, A, B, T> { +impl Iterator for FilterMap where + I: Iterator, + F: FnMut(A) -> Option, +{ #[inline] fn next(&mut self) -> Option { for x in self.iter { @@ -1509,8 +1512,10 @@ impl<'a, A, B, T: Iterator> Iterator for FilterMap<'a, A, B, T> { } #[unstable = "trait is unstable"] -impl<'a, A, B, T: DoubleEndedIterator> DoubleEndedIterator -for FilterMap<'a, A, B, T> { +impl DoubleEndedIterator for FilterMap where + I: DoubleEndedIterator, + F: FnMut(A) -> Option, +{ #[inline] fn next_back(&mut self) -> Option { for x in self.iter.by_ref().rev() { -- cgit 1.4.1-3-g733a5 From 0cfdc99c71f132f967cf968e5b2c9d20adf14b46 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Tue, 2 Dec 2014 23:28:32 -0500 Subject: libcore: use unboxed closures in the fields of `SkipWhile` --- src/libcore/iter.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index b356e42907b..e1d944dcb2d 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -264,7 +264,7 @@ pub trait IteratorExt: Iterator { /// ``` #[inline] #[unstable = "waiting for unboxed closures"] - fn skip_while<'r>(self, predicate: |&A|: 'r -> bool) -> SkipWhile<'r, A, Self> { + fn skip_while

(self, predicate: P) -> SkipWhile where P: FnMut(&A) -> bool { SkipWhile{iter: self, flag: false, predicate: predicate} } @@ -1645,14 +1645,14 @@ impl<'a, A, T: Iterator> Peekable { /// An iterator which rejects elements while `predicate` is true #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable] -pub struct SkipWhile<'a, A, T> { - iter: T, +pub struct SkipWhile where I: Iterator, P: FnMut(&A) -> bool { + iter: I, flag: bool, - predicate: |&A|: 'a -> bool + predicate: P, } #[unstable = "trait is unstable"] -impl<'a, A, T: Iterator> Iterator for SkipWhile<'a, A, T> { +impl Iterator for SkipWhile where I: Iterator, P: FnMut(&A) -> bool { #[inline] fn next(&mut self) -> Option { for x in self.iter { -- cgit 1.4.1-3-g733a5 From e2724cb1d53b368e6a18571f345f08a0dd2644c9 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Wed, 3 Dec 2014 00:42:28 -0500 Subject: libcore: use unboxed closures in the fields of `TakeWhile` --- src/libcore/iter.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index e1d944dcb2d..c3ed1b7a9ad 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -283,7 +283,7 @@ pub trait IteratorExt: Iterator { /// ``` #[inline] #[unstable = "waiting for unboxed closures, may want to require peek"] - fn take_while<'r>(self, predicate: |&A|: 'r -> bool) -> TakeWhile<'r, A, Self> { + fn take_while

(self, predicate: P) -> TakeWhile where P: FnMut(&A) -> bool { TakeWhile{iter: self, flag: false, predicate: predicate} } @@ -1674,14 +1674,14 @@ impl Iterator for SkipWhile where I: Iterator, P: FnMut( /// An iterator which only accepts elements while `predicate` is true #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[stable] -pub struct TakeWhile<'a, A, T> { - iter: T, +pub struct TakeWhile where I: Iterator, P: FnMut(&A) -> bool { + iter: I, flag: bool, - predicate: |&A|: 'a -> bool + predicate: P, } #[unstable = "trait is unstable"] -impl<'a, A, T: Iterator> Iterator for TakeWhile<'a, A, T> { +impl Iterator for TakeWhile where I: Iterator, P: FnMut(&A) -> bool { #[inline] fn next(&mut self) -> Option { if self.flag { -- cgit 1.4.1-3-g733a5 From ba480cbf75387236a364961354b466cbee69146d Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Wed, 3 Dec 2014 01:59:31 -0500 Subject: libcore: use unboxed closures in the fields of `Scan` --- src/libcore/iter.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index c3ed1b7a9ad..8a28988c2cb 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -346,8 +346,9 @@ pub trait IteratorExt: Iterator { /// ``` #[inline] #[unstable = "waiting for unboxed closures"] - fn scan<'r, St, B>(self, initial_state: St, f: |&mut St, A|: 'r -> Option) - -> Scan<'r, A, B, Self, St> { + fn scan(self, initial_state: St, f: F) -> Scan where + F: FnMut(&mut St, A) -> Option, + { Scan{iter: self, f: f, state: initial_state} } @@ -1833,16 +1834,19 @@ impl> RandomAccessIterator for Take { /// An iterator to maintain state while iterating another iterator #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[unstable = "waiting for unboxed closures"] -pub struct Scan<'a, A, B, T, St> { - iter: T, - f: |&mut St, A|: 'a -> Option, +pub struct Scan where I: Iterator, F: FnMut(&mut St, A) -> Option { + iter: I, + f: F, /// The current internal state to be passed to the closure next. pub state: St, } #[unstable = "trait is unstable"] -impl<'a, A, B, T: Iterator, St> Iterator for Scan<'a, A, B, T, St> { +impl Iterator for Scan where + I: Iterator, + F: FnMut(&mut St, A) -> Option, +{ #[inline] fn next(&mut self) -> Option { self.iter.next().and_then(|a| (self.f)(&mut self.state, a)) -- cgit 1.4.1-3-g733a5 From a051ba1dffa2648a9cd25d39f70fbb5089505762 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Wed, 3 Dec 2014 03:07:16 -0500 Subject: libcore: use unboxed closures in the fields of `FlatMap` --- src/libcore/iter.rs | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 8a28988c2cb..c0eae929498 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -372,8 +372,10 @@ pub trait IteratorExt: Iterator { /// ``` #[inline] #[unstable = "waiting for unboxed closures"] - fn flat_map<'r, B, U: Iterator>(self, f: |A|: 'r -> U) - -> FlatMap<'r, A, Self, U> { + fn flat_map(self, f: F) -> FlatMap where + U: Iterator, + F: FnMut(A) -> U, + { FlatMap{iter: self, f: f, frontiter: None, backiter: None } } @@ -1864,15 +1866,19 @@ impl Iterator for Scan where /// #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[unstable = "waiting for unboxed closures"] -pub struct FlatMap<'a, A, T, U> { - iter: T, - f: |A|: 'a -> U, +pub struct FlatMap where I: Iterator, U: Iterator, F: FnMut(A) -> U { + iter: I, + f: F, frontiter: Option, backiter: Option, } #[unstable = "trait is unstable"] -impl<'a, A, T: Iterator, B, U: Iterator> Iterator for FlatMap<'a, A, T, U> { +impl Iterator for FlatMap where + I: Iterator, + U: Iterator, + F: FnMut(A) -> U, +{ #[inline] fn next(&mut self) -> Option { loop { @@ -1901,10 +1907,11 @@ impl<'a, A, T: Iterator, B, U: Iterator> Iterator for FlatMap<'a, A, T, } #[unstable = "trait is unstable"] -impl<'a, - A, T: DoubleEndedIterator, - B, U: DoubleEndedIterator> DoubleEndedIterator - for FlatMap<'a, A, T, U> { +impl DoubleEndedIterator for FlatMap where + I: DoubleEndedIterator, + U: DoubleEndedIterator, + F: FnMut(A) -> U, +{ #[inline] fn next_back(&mut self) -> Option { loop { -- cgit 1.4.1-3-g733a5 From 7e3493e5e3a0cc7b4c99f36865305b241ff993d0 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Wed, 3 Dec 2014 04:04:27 -0500 Subject: libcore: use unboxed closures in the fields of `Inspect` --- src/libcore/iter.rs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index c0eae929498..36b902dbee1 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -432,7 +432,7 @@ pub trait IteratorExt: Iterator { /// ``` #[inline] #[unstable = "waiting for unboxed closures"] - fn inspect<'r>(self, f: |&A|: 'r) -> Inspect<'r, A, Self> { + fn inspect(self, f: F) -> Inspect where F: FnMut(&A) { Inspect{iter: self, f: f} } @@ -777,7 +777,10 @@ pub trait ExactSizeIterator : DoubleEndedIterator { #[unstable = "trait is unstable"] impl> ExactSizeIterator<(uint, A)> for Enumerate {} #[unstable = "trait is unstable"] -impl<'a, A, T: ExactSizeIterator> ExactSizeIterator for Inspect<'a, A, T> {} +impl ExactSizeIterator for Inspect where + I: ExactSizeIterator, + F: FnMut(&A), +{} #[unstable = "trait is unstable"] impl> ExactSizeIterator for Rev {} #[unstable = "trait is unstable"] @@ -2012,12 +2015,12 @@ impl Fuse { /// element before yielding it. #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[unstable = "waiting for unboxed closures"] -pub struct Inspect<'a, A, T> { - iter: T, - f: |&A|: 'a +pub struct Inspect where I: Iterator, F: FnMut(&A) { + iter: I, + f: F, } -impl<'a, A, T> Inspect<'a, A, T> { +impl Inspect where I: Iterator, F: FnMut(&A) { #[inline] fn do_inspect(&mut self, elt: Option) -> Option { match elt { @@ -2030,7 +2033,7 @@ impl<'a, A, T> Inspect<'a, A, T> { } #[unstable = "trait is unstable"] -impl<'a, A, T: Iterator> Iterator for Inspect<'a, A, T> { +impl Iterator for Inspect where I: Iterator, F: FnMut(&A) { #[inline] fn next(&mut self) -> Option { let next = self.iter.next(); @@ -2044,8 +2047,10 @@ impl<'a, A, T: Iterator> Iterator for Inspect<'a, A, T> { } #[unstable = "trait is unstable"] -impl<'a, A, T: DoubleEndedIterator> DoubleEndedIterator -for Inspect<'a, A, T> { +impl DoubleEndedIterator for Inspect where + I: DoubleEndedIterator, + F: FnMut(&A), +{ #[inline] fn next_back(&mut self) -> Option { let next = self.iter.next_back(); @@ -2054,8 +2059,10 @@ for Inspect<'a, A, T> { } #[experimental = "trait is experimental"] -impl<'a, A, T: RandomAccessIterator> RandomAccessIterator -for Inspect<'a, A, T> { +impl RandomAccessIterator for Inspect where + I: RandomAccessIterator, + F: FnMut(&A), +{ #[inline] fn indexable(&self) -> uint { self.iter.indexable() -- cgit 1.4.1-3-g733a5 From 216bcfd66b63a9f60e41e4b736145f62edda4150 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Wed, 3 Dec 2014 15:51:47 -0500 Subject: libcore: use unboxed closures in the fields of `Unfold` --- src/libcore/iter.rs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 36b902dbee1..51ae0f47029 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -2108,19 +2108,18 @@ impl RandomAccessIterator for Inspect where /// } /// ``` #[experimental] -pub struct Unfold<'a, A, St> { - f: |&mut St|: 'a -> Option, +pub struct Unfold where F: FnMut(&mut St) -> Option { + f: F, /// Internal state that will be passed to the closure on the next iteration pub state: St, } #[experimental] -impl<'a, A, St> Unfold<'a, A, St> { +impl Unfold where F: FnMut(&mut St) -> Option { /// Creates a new iterator with the specified closure as the "iterator /// function" and an initial state to eventually pass to the closure #[inline] - pub fn new<'a>(initial_state: St, f: |&mut St|: 'a -> Option) - -> Unfold<'a, A, St> { + pub fn new(initial_state: St, f: F) -> Unfold { Unfold { f: f, state: initial_state @@ -2129,7 +2128,7 @@ impl<'a, A, St> Unfold<'a, A, St> { } #[experimental] -impl<'a, A, St> Iterator for Unfold<'a, A, St> { +impl Iterator for Unfold where F: FnMut(&mut St) -> Option { #[inline] fn next(&mut self) -> Option { (self.f)(&mut self.state) @@ -2456,18 +2455,24 @@ impl RandomAccessIterator for Repeat { fn idx(&mut self, _: uint) -> Option { Some(self.element.clone()) } } -type IterateState<'a, T> = (|T|: 'a -> T, Option, bool); +type IterateState = (F, Option, bool); /// An iterator that repeatedly applies a given function, starting /// from a given seed value. #[experimental] -pub type Iterate<'a, T> = Unfold<'a, T, IterateState<'a, T>>; +pub type Iterate = Unfold, fn(&mut IterateState) -> Option>; /// Create a new iterator that produces an infinite sequence of /// repeated applications of the given function `f`. #[experimental] -pub fn iterate<'a, T: Clone>(seed: T, f: |T|: 'a -> T) -> Iterate<'a, T> { - Unfold::new((f, Some(seed), true), |st| { +pub fn iterate(seed: T, f: F) -> Iterate where + T: Clone, + F: FnMut(T) -> T, +{ + fn next(st: &mut IterateState) -> Option where + T: Clone, + F: FnMut(T) -> T, + { let &(ref mut f, ref mut val, ref mut first) = st; if *first { *first = false; @@ -2480,7 +2485,9 @@ pub fn iterate<'a, T: Clone>(seed: T, f: |T|: 'a -> T) -> Iterate<'a, T> { } } val.clone() - }) + } + + Unfold::new((f, Some(seed), true), next) } /// Create a new iterator that endlessly repeats the element `elt`. -- cgit 1.4.1-3-g733a5 From 5e9ca5b25530ab58f9d0b09662884928b054f271 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Wed, 3 Dec 2014 19:43:25 -0500 Subject: libcore: use unboxed closures in `IteratorExt` methods --- src/libcore/iter.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 51ae0f47029..e886dfadb47 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -521,7 +521,7 @@ pub trait IteratorExt: Iterator { /// ``` #[inline] #[unstable = "waiting for unboxed closures, just changed to take self by value"] - fn fold(mut self, init: B, f: |B, A| -> B) -> B { + fn fold(mut self, init: B, mut f: F) -> B where F: FnMut(B, A) -> B { let mut accum = init; for x in self { accum = f(accum, x); @@ -555,7 +555,7 @@ pub trait IteratorExt: Iterator { /// ``` #[inline] #[unstable = "waiting for unboxed closures, just changed to take self by value"] - fn all(mut self, f: |A| -> bool) -> bool { + fn all(mut self, mut f: F) -> bool where F: FnMut(A) -> bool { for x in self { if !f(x) { return false; } } true } @@ -573,7 +573,7 @@ pub trait IteratorExt: Iterator { /// ``` #[inline] #[unstable = "waiting for unboxed closures"] - fn any(&mut self, f: |A| -> bool) -> bool { + fn any(&mut self, mut f: F) -> bool where F: FnMut(A) -> bool { for x in *self { if f(x) { return true; } } false } @@ -583,7 +583,7 @@ pub trait IteratorExt: Iterator { /// Does not consume the iterator past the first found element. #[inline] #[unstable = "waiting for unboxed closures"] - fn find(&mut self, predicate: |&A| -> bool) -> Option { + fn find

(&mut self, mut predicate: P) -> Option where P: FnMut(&A) -> bool { for x in *self { if predicate(&x) { return Some(x) } } @@ -593,7 +593,7 @@ pub trait IteratorExt: Iterator { /// Return the index of the first element satisfying the specified predicate #[inline] #[unstable = "waiting for unboxed closures"] - fn position(&mut self, predicate: |A| -> bool) -> Option { + fn position

(&mut self, mut predicate: P) -> Option where P: FnMut(A) -> bool { let mut i = 0; for x in *self { if predicate(x) { @@ -617,7 +617,7 @@ pub trait IteratorExt: Iterator { /// ``` #[inline] #[unstable = "waiting for unboxed closures, just changed to take self by value"] - fn max_by(self, f: |&A| -> B) -> Option { + fn max_by(self, mut f: F) -> Option where F: FnMut(&A) -> B { self.fold(None, |max: Option<(A, B)>, x| { let x_val = f(&x); match max { @@ -644,7 +644,7 @@ pub trait IteratorExt: Iterator { /// ``` #[inline] #[unstable = "waiting for unboxed closures, just changed to take self by value"] - fn min_by(self, f: |&A| -> B) -> Option { + fn min_by(self, mut f: F) -> Option where F: FnMut(&A) -> B { self.fold(None, |min: Option<(A, B)>, x| { let x_val = f(&x); match min { -- cgit 1.4.1-3-g733a5 From aa921b61622a6dc08d5c70cb6a217b1f6d304aac Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 4 Dec 2014 16:13:12 -0500 Subject: libcore: use unboxed closures in `ExactSizeIterator` methods --- src/libcore/iter.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libcore') diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index e886dfadb47..8ee2a8874bb 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -749,7 +749,7 @@ pub trait ExactSizeIterator : DoubleEndedIterator { /// /// If no element matches, None is returned. #[inline] - fn rposition(&mut self, predicate: |A| -> bool) -> Option { + fn rposition

(&mut self, mut predicate: P) -> Option where P: FnMut(A) -> bool { let len = self.len(); for i in range(0, len).rev() { if predicate(self.next_back().expect("rposition: incorrect ExactSizeIterator")) { -- cgit 1.4.1-3-g733a5 From d3f5c1397c5012ce9bb022c0320c958c9fdc1208 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 4 Dec 2014 16:17:07 -0500 Subject: libcore: impl CharEq for FnMut(char) -> bool implementors --- src/libcore/str.rs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 051c36a2dc0..328a64acaf8 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -31,6 +31,7 @@ use mem; use num::Int; use option::Option; use option::Option::{None, Some}; +use ops::FnMut; use ptr::RawPtr; use raw::{Repr, Slice}; use slice::{mod, SlicePrelude}; @@ -136,15 +137,7 @@ impl CharEq for char { fn only_ascii(&self) -> bool { (*self as uint) < 128 } } -impl<'a> CharEq for |char|: 'a -> bool { - #[inline] - fn matches(&mut self, c: char) -> bool { (*self)(c) } - - #[inline] - fn only_ascii(&self) -> bool { false } -} - -impl CharEq for extern "Rust" fn(char) -> bool { +impl CharEq for F where F: FnMut(char) -> bool { #[inline] fn matches(&mut self, c: char) -> bool { (*self)(c) } @@ -2142,11 +2135,11 @@ impl StrPrelude for str { #[inline] fn trim_chars(&self, mut to_trim: C) -> &str { - let cur = match self.find(|c: char| !to_trim.matches(c)) { + let cur = match self.find(|&mut: c: char| !to_trim.matches(c)) { None => "", Some(i) => unsafe { self.slice_unchecked(i, self.len()) } }; - match cur.rfind(|c: char| !to_trim.matches(c)) { + match cur.rfind(|&mut: c: char| !to_trim.matches(c)) { None => "", Some(i) => { let right = cur.char_range_at(i).next; @@ -2157,7 +2150,7 @@ impl StrPrelude for str { #[inline] fn trim_left_chars(&self, mut to_trim: C) -> &str { - match self.find(|c: char| !to_trim.matches(c)) { + match self.find(|&mut: c: char| !to_trim.matches(c)) { None => "", Some(first) => unsafe { self.slice_unchecked(first, self.len()) } } @@ -2165,7 +2158,7 @@ impl StrPrelude for str { #[inline] fn trim_right_chars(&self, mut to_trim: C) -> &str { - match self.rfind(|c: char| !to_trim.matches(c)) { + match self.rfind(|&mut: c: char| !to_trim.matches(c)) { None => "", Some(last) => { let next = self.char_range_at(last).next; -- cgit 1.4.1-3-g733a5 From 30ea64ea77dd33d0af9271b032644120b4f5166a Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 4 Dec 2014 21:31:49 -0500 Subject: libcore: fix fallout in doctests --- src/libcore/str.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 9 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 328a64acaf8..92f82bd9771 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -1352,10 +1352,13 @@ pub trait StrPrelude for Sized? { /// # Example /// /// ```rust + /// # #![feature(unboxed_closures)] + /// + /// # fn main() { /// let v: Vec<&str> = "Mary had a little lamb".split(' ').collect(); /// assert_eq!(v, vec!["Mary", "had", "a", "little", "lamb"]); /// - /// let v: Vec<&str> = "abc1def2ghi".split(|c: char| c.is_numeric()).collect(); + /// let v: Vec<&str> = "abc1def2ghi".split(|&: c: char| c.is_numeric()).collect(); /// assert_eq!(v, vec!["abc", "def", "ghi"]); /// /// let v: Vec<&str> = "lionXXtigerXleopard".split('X').collect(); @@ -1363,6 +1366,7 @@ pub trait StrPrelude for Sized? { /// /// let v: Vec<&str> = "".split('X').collect(); /// assert_eq!(v, vec![""]); + /// # } /// ``` fn split<'a, Sep: CharEq>(&'a self, sep: Sep) -> CharSplits<'a, Sep>; @@ -1373,10 +1377,13 @@ pub trait StrPrelude for Sized? { /// # Example /// /// ```rust + /// # #![feature(unboxed_closures)] + /// + /// # fn main() { /// let v: Vec<&str> = "Mary had a little lambda".splitn(2, ' ').collect(); /// assert_eq!(v, vec!["Mary", "had", "a little lambda"]); /// - /// let v: Vec<&str> = "abc1def2ghi".splitn(1, |c: char| c.is_numeric()).collect(); + /// let v: Vec<&str> = "abc1def2ghi".splitn(1, |&: c: char| c.is_numeric()).collect(); /// assert_eq!(v, vec!["abc", "def2ghi"]); /// /// let v: Vec<&str> = "lionXXtigerXleopard".splitn(2, 'X').collect(); @@ -1387,6 +1394,7 @@ pub trait StrPrelude for Sized? { /// /// let v: Vec<&str> = "".splitn(1, 'X').collect(); /// assert_eq!(v, vec![""]); + /// # } /// ``` fn splitn<'a, Sep: CharEq>(&'a self, count: uint, sep: Sep) -> CharSplitsN<'a, Sep>; @@ -1399,6 +1407,9 @@ pub trait StrPrelude for Sized? { /// # Example /// /// ```rust + /// # #![feature(unboxed_closures)] + /// + /// # fn main() { /// let v: Vec<&str> = "A.B.".split_terminator('.').collect(); /// assert_eq!(v, vec!["A", "B"]); /// @@ -1408,11 +1419,12 @@ pub trait StrPrelude for Sized? { /// let v: Vec<&str> = "Mary had a little lamb".split(' ').rev().collect(); /// assert_eq!(v, vec!["lamb", "little", "a", "had", "Mary"]); /// - /// let v: Vec<&str> = "abc1def2ghi".split(|c: char| c.is_numeric()).rev().collect(); + /// let v: Vec<&str> = "abc1def2ghi".split(|&: c: char| c.is_numeric()).rev().collect(); /// assert_eq!(v, vec!["ghi", "def", "abc"]); /// /// let v: Vec<&str> = "lionXXtigerXleopard".split('X').rev().collect(); /// assert_eq!(v, vec!["leopard", "tiger", "", "lion"]); + /// # } /// ``` fn split_terminator<'a, Sep: CharEq>(&'a self, sep: Sep) -> CharSplits<'a, Sep>; @@ -1423,14 +1435,18 @@ pub trait StrPrelude for Sized? { /// # Example /// /// ```rust + /// # #![feature(unboxed_closures)] + /// + /// # fn main() { /// let v: Vec<&str> = "Mary had a little lamb".rsplitn(2, ' ').collect(); /// assert_eq!(v, vec!["lamb", "little", "Mary had a"]); /// - /// let v: Vec<&str> = "abc1def2ghi".rsplitn(1, |c: char| c.is_numeric()).collect(); + /// let v: Vec<&str> = "abc1def2ghi".rsplitn(1, |&: c: char| c.is_numeric()).collect(); /// assert_eq!(v, vec!["ghi", "abc1def"]); /// /// let v: Vec<&str> = "lionXXtigerXleopard".rsplitn(2, 'X').collect(); /// assert_eq!(v, vec!["leopard", "tiger", "lionX"]); + /// # } /// ``` fn rsplitn<'a, Sep: CharEq>(&'a self, count: uint, sep: Sep) -> CharSplitsN<'a, Sep>; @@ -1641,10 +1657,14 @@ pub trait StrPrelude for Sized? { /// # Example /// /// ```rust + /// # #![feature(unboxed_closures)] + /// + /// # fn main() { /// assert_eq!("11foo1bar11".trim_chars('1'), "foo1bar") /// let x: &[_] = &['1', '2']; /// assert_eq!("12foo1bar12".trim_chars(x), "foo1bar") - /// assert_eq!("123foo1bar123".trim_chars(|c: char| c.is_numeric()), "foo1bar") + /// assert_eq!("123foo1bar123".trim_chars(|&: c: char| c.is_numeric()), "foo1bar") + /// # } /// ``` fn trim_chars<'a, C: CharEq>(&'a self, to_trim: C) -> &'a str; @@ -1657,10 +1677,14 @@ pub trait StrPrelude for Sized? { /// # Example /// /// ```rust + /// # #![feature(unboxed_closures)] + /// + /// # fn main() { /// assert_eq!("11foo1bar11".trim_left_chars('1'), "foo1bar11") /// let x: &[_] = &['1', '2']; /// assert_eq!("12foo1bar12".trim_left_chars(x), "foo1bar12") - /// assert_eq!("123foo1bar123".trim_left_chars(|c: char| c.is_numeric()), "foo1bar123") + /// assert_eq!("123foo1bar123".trim_left_chars(|&: c: char| c.is_numeric()), "foo1bar123") + /// # } /// ``` fn trim_left_chars<'a, C: CharEq>(&'a self, to_trim: C) -> &'a str; @@ -1673,10 +1697,14 @@ pub trait StrPrelude for Sized? { /// # Example /// /// ```rust + /// # #![feature(unboxed_closures)] + /// + /// # fn main() { /// assert_eq!("11foo1bar11".trim_right_chars('1'), "11foo1bar") /// let x: &[_] = &['1', '2']; /// assert_eq!("12foo1bar12".trim_right_chars(x), "12foo1bar") - /// assert_eq!("123foo1bar123".trim_right_chars(|c: char| c.is_numeric()), "123foo1bar") + /// assert_eq!("123foo1bar123".trim_right_chars(|&: c: char| c.is_numeric()), "123foo1bar") + /// # } /// ``` fn trim_right_chars<'a, C: CharEq>(&'a self, to_trim: C) -> &'a str; @@ -1817,17 +1845,21 @@ pub trait StrPrelude for Sized? { /// # Example /// /// ```rust + /// # #![feature(unboxed_closures)] + /// + /// # fn main() { /// let s = "Löwe 老虎 Léopard"; /// /// assert_eq!(s.find('L'), Some(0)); /// assert_eq!(s.find('é'), Some(14)); /// /// // the first space - /// assert_eq!(s.find(|c: char| c.is_whitespace()), Some(5)); + /// assert_eq!(s.find(|&: c: char| c.is_whitespace()), Some(5)); /// /// // neither are found /// let x: &[_] = &['1', '2']; /// assert_eq!(s.find(x), None); + /// # } /// ``` fn find(&self, search: C) -> Option; @@ -1842,17 +1874,21 @@ pub trait StrPrelude for Sized? { /// # Example /// /// ```rust + /// # #![feature(unboxed_closures)] + /// + /// # fn main() { /// let s = "Löwe 老虎 Léopard"; /// /// assert_eq!(s.rfind('L'), Some(13)); /// assert_eq!(s.rfind('é'), Some(14)); /// /// // the second space - /// assert_eq!(s.rfind(|c: char| c.is_whitespace()), Some(12)); + /// assert_eq!(s.rfind(|&: c: char| c.is_whitespace()), Some(12)); /// /// // searches for an occurrence of either `1` or `2`, but neither are found /// let x: &[_] = &['1', '2']; /// assert_eq!(s.rfind(x), None); + /// # } /// ``` fn rfind(&self, search: C) -> Option; -- cgit 1.4.1-3-g733a5 From 9c7046573b75b988e8291c6d7bf9c126ba2b7b5a Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 4 Dec 2014 23:47:40 -0500 Subject: libcore: use unboxed closures in the fields of `Splits` --- src/libcore/slice.rs | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'src/libcore') 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) } -- cgit 1.4.1-3-g733a5 From 6ae9b9e54a9eb9711b32d663bb1a044f7540b4b0 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Fri, 5 Dec 2014 01:00:50 -0500 Subject: libcore: use unboxed closures in the fields of `MutSplits` --- src/libcore/slice.rs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index 3dad5458b36..cfdb406f711 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -319,20 +319,23 @@ pub trait SlicePrelude for Sized? { /// Returns an iterator over mutable subslices separated by elements that /// match `pred`. The matched element is not contained in the subslices. #[unstable = "waiting on unboxed closures, iterator type name conventions"] - fn split_mut<'a>(&'a mut self, pred: |&T|: 'a -> bool) -> MutSplits<'a, T>; + fn split_mut<'a, P>(&'a mut self, pred: P) -> MutSplits<'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 = "waiting on unboxed closures, iterator type name conventions"] - fn splitn_mut<'a>(&'a mut self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN>; + fn splitn_mut<'a, P>(&'a mut 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 = "waiting on unboxed closures, iterator type name conventions"] - fn rsplitn_mut<'a>(&'a mut self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN>; + fn rsplitn_mut<'a, P>(&'a mut self, n: uint, pred: P) -> SplitsN> where + P: FnMut(&T) -> bool; /// Returns an iterator over `chunk_size` elements of the slice at a time. /// The chunks are mutable and do not overlap. If `chunk_size` does @@ -644,12 +647,14 @@ impl SlicePrelude for [T] { } #[inline] - fn split_mut<'a>(&'a mut self, pred: |&T|: 'a -> bool) -> MutSplits<'a, T> { + fn split_mut<'a, P>(&'a mut self, pred: P) -> MutSplits<'a, T, P> where P: FnMut(&T) -> bool { MutSplits { v: self, pred: pred, finished: false } } #[inline] - fn splitn_mut<'a>(&'a mut self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN> { + fn splitn_mut<'a, P>(&'a mut self, n: uint, pred: P) -> SplitsN> where + P: FnMut(&T) -> bool + { SplitsN { iter: self.split_mut(pred), count: n, @@ -658,7 +663,9 @@ impl SlicePrelude for [T] { } #[inline] - fn rsplitn_mut<'a>(&'a mut self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN> { + fn rsplitn_mut<'a, P>(&'a mut self, n: uint, pred: P) -> SplitsN> where + P: FnMut(&T) -> bool, + { SplitsN { iter: self.split_mut(pred), count: n, @@ -1337,13 +1344,13 @@ impl<'a, T, P> SplitsIter<&'a [T]> for Splits<'a, T, P> where P: FnMut(&T) -> bo /// An iterator over the subslices of the vector which are separated /// by elements that match `pred`. #[experimental = "needs review"] -pub struct MutSplits<'a, T:'a> { +pub struct MutSplits<'a, T:'a, P> where P: FnMut(&T) -> bool { v: &'a mut [T], - pred: |t: &T|: 'a -> bool, + pred: P, finished: bool } -impl<'a, T> SplitsIter<&'a mut [T]> for MutSplits<'a, T> { +impl<'a, T, P> SplitsIter<&'a mut [T]> for MutSplits<'a, T, P> where P: FnMut(&T) -> bool { #[inline] fn finish(&mut self) -> Option<&'a mut [T]> { if self.finished { @@ -1356,7 +1363,7 @@ impl<'a, T> SplitsIter<&'a mut [T]> for MutSplits<'a, T> { } #[experimental = "needs review"] -impl<'a, T> Iterator<&'a mut [T]> for MutSplits<'a, T> { +impl<'a, T, P> Iterator<&'a mut [T]> for MutSplits<'a, T, P> where P: FnMut(&T) -> bool { #[inline] fn next(&mut self) -> Option<&'a mut [T]> { if self.finished { return None; } @@ -1389,7 +1396,9 @@ impl<'a, T> Iterator<&'a mut [T]> for MutSplits<'a, T> { } #[experimental = "needs review"] -impl<'a, T> DoubleEndedIterator<&'a mut [T]> for MutSplits<'a, T> { +impl<'a, T, P> DoubleEndedIterator<&'a mut [T]> for MutSplits<'a, T, P> where + P: FnMut(&T) -> bool, +{ #[inline] fn next_back(&mut self) -> Option<&'a mut [T]> { if self.finished { return None; } -- cgit 1.4.1-3-g733a5 From e2a362f9bbf94eedca42eceea2929e4d96f4eeee Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Fri, 5 Dec 2014 02:04:33 -0500 Subject: libcore: use unboxed closures in `SlicePrelude` methods --- src/libcore/slice.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index cfdb406f711..af150994765 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -238,7 +238,7 @@ pub trait SlicePrelude for Sized? { /// assert!(match r { Found(1...4) => true, _ => false, }); /// ``` #[unstable = "waiting on unboxed closures"] - fn binary_search(&self, f: |&T| -> Ordering) -> BinarySearchResult; + fn binary_search(&self, f: F) -> BinarySearchResult where F: FnMut(&T) -> Ordering; /// Return the number of elements in the slice /// @@ -552,7 +552,7 @@ impl SlicePrelude for [T] { } #[unstable] - fn binary_search(&self, f: |&T| -> Ordering) -> BinarySearchResult { + fn binary_search(&self, mut f: F) -> BinarySearchResult where F: FnMut(&T) -> Ordering { let mut base : uint = 0; let mut lim : uint = self.len(); -- cgit 1.4.1-3-g733a5 From f18b255bced2dfedc437ea3d54465e4f42b1938a Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Fri, 5 Dec 2014 08:24:40 -0500 Subject: libcore: use unboxed closures in the `finally` module --- src/libcore/finally.rs | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/finally.rs b/src/libcore/finally.rs index d2b7591b3ef..db76ce35cad 100644 --- a/src/libcore/finally.rs +++ b/src/libcore/finally.rs @@ -30,29 +30,19 @@ #![experimental] -use ops::Drop; +use ops::{Drop, FnMut, FnOnce}; /// A trait for executing a destructor unconditionally after a block of code, /// regardless of whether the blocked fails. pub trait Finally { /// Executes this object, unconditionally running `dtor` after this block of /// code has run. - fn finally(&mut self, dtor: ||) -> T; + fn finally(&mut self, dtor: F) -> T where F: FnMut(); } -impl<'a,T> Finally for ||: 'a -> T { - fn finally(&mut self, dtor: ||) -> T { - try_finally(&mut (), self, - |_, f| (*f)(), - |_| dtor()) - } -} - -impl Finally for fn() -> T { - fn finally(&mut self, dtor: ||) -> T { - try_finally(&mut (), (), - |_, _| (*self)(), - |_| dtor()) +impl Finally for F where F: FnMut() -> T { + fn finally(&mut self, mut dtor: G) -> T where G: FnMut() { + try_finally(&mut (), self, |_, f| (*f)(), |_| dtor()) } } @@ -86,11 +76,10 @@ impl Finally for fn() -> T { /// // use state.buffer, state.len to cleanup /// }) /// ``` -pub fn try_finally(mutate: &mut T, - drop: U, - try_fn: |&mut T, U| -> R, - finally_fn: |&mut T|) - -> R { +pub fn try_finally(mutate: &mut T, drop: U, try_fn: F, finally_fn: G) -> R where + F: FnOnce(&mut T, U) -> R, + G: FnMut(&mut T), +{ let f = Finallyalizer { mutate: mutate, dtor: finally_fn, @@ -98,13 +87,13 @@ pub fn try_finally(mutate: &mut T, try_fn(&mut *f.mutate, drop) } -struct Finallyalizer<'a,A:'a> { +struct Finallyalizer<'a, A:'a, F> where F: FnMut(&mut A) { mutate: &'a mut A, - dtor: |&mut A|: 'a + dtor: F, } #[unsafe_destructor] -impl<'a,A> Drop for Finallyalizer<'a,A> { +impl<'a, A, F> Drop for Finallyalizer<'a, A, F> where F: FnMut(&mut A) { #[inline] fn drop(&mut self) { (self.dtor)(self.mutate); -- cgit 1.4.1-3-g733a5 From 0b0c3e1d9623f4cb55a717cd3ffe8d985c84247f Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Fri, 5 Dec 2014 11:55:36 -0500 Subject: libcore: fix fallout in doc tests --- src/libcore/finally.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/libcore') diff --git a/src/libcore/finally.rs b/src/libcore/finally.rs index db76ce35cad..2b48b2bf81a 100644 --- a/src/libcore/finally.rs +++ b/src/libcore/finally.rs @@ -19,13 +19,17 @@ //! # Example //! //! ``` +//! # #![feature(unboxed_closures)] +//! //! use std::finally::Finally; //! -//! (|| { +//! # fn main() { +//! (|&mut:| { //! // ... //! }).finally(|| { //! // this code is always run //! }) +//! # } //! ``` #![experimental] -- cgit 1.4.1-3-g733a5 From 1a87fc7c9f6ce91293fd6553a49536f2ceccc165 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Fri, 5 Dec 2014 13:06:24 -0500 Subject: libcore: use unboxed closures in `Formatter` methods --- src/libcore/fmt/mod.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 88ea811cfd6..37a1d4d564d 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -19,7 +19,7 @@ use kinds::{Copy, Sized}; use mem; use option::Option; use option::Option::{Some, None}; -use ops::Deref; +use ops::{Deref, FnOnce}; use result::Result::{Ok, Err}; use result; use slice::SlicePrelude; @@ -491,10 +491,9 @@ impl<'a> Formatter<'a> { /// Runs a callback, emitting the correct padding either before or /// afterwards depending on whether right or left alignment is requested. - fn with_padding(&mut self, - padding: uint, - default: rt::Alignment, - f: |&mut Formatter| -> Result) -> Result { + fn with_padding(&mut self, padding: uint, default: rt::Alignment, f: F) -> Result where + F: FnOnce(&mut Formatter) -> Result, + { use char::Char; let align = match self.align { rt::AlignUnknown => default, -- cgit 1.4.1-3-g733a5 From 02e7389c5d3e7fd9dfec13f691a04cfff003205d Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Fri, 5 Dec 2014 13:53:25 -0500 Subject: libcore: use unboxed closures in the `char` module --- src/libcore/char.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/char.rs b/src/libcore/char.rs index 671b4ccb9e4..75f7991df02 100644 --- a/src/libcore/char.rs +++ b/src/libcore/char.rs @@ -16,6 +16,7 @@ #![doc(primitive = "char")] use mem::transmute; +use ops::FnMut; use option::Option; use option::Option::{None, Some}; use iter::{range_step, Iterator, RangeStep}; @@ -165,7 +166,7 @@ pub fn from_digit(num: uint, radix: uint) -> Option { /// - chars above 0x10000 get 8-digit escapes: `\\u{{NNN}NNNNN}` /// #[deprecated = "use the Char::escape_unicode method"] -pub fn escape_unicode(c: char, f: |char|) { +pub fn escape_unicode(c: char, mut f: F) where F: FnMut(char) { for char in c.escape_unicode() { f(char); } @@ -184,7 +185,7 @@ pub fn escape_unicode(c: char, f: |char|) { /// - Any other chars are given hex Unicode escapes; see `escape_unicode`. /// #[deprecated = "use the Char::escape_default method"] -pub fn escape_default(c: char, f: |char|) { +pub fn escape_default(c: char, mut f: F) where F: FnMut(char) { for c in c.escape_default() { f(c); } -- cgit 1.4.1-3-g733a5 From c7b6eb38ff14b7225b7ccb86f9a2080fdba4a3e0 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 6 Dec 2014 12:05:38 -0500 Subject: libcore: use unboxed closures in `float_to_str_bytes_common` --- src/libcore/fmt/float.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/fmt/float.rs b/src/libcore/fmt/float.rs index 400ce76baa0..fb4d91e912a 100644 --- a/src/libcore/fmt/float.rs +++ b/src/libcore/fmt/float.rs @@ -20,6 +20,7 @@ use fmt; use iter::{range, DoubleEndedIteratorExt}; use num::{Float, FPNaN, FPInfinite, ToPrimitive}; use num::cast; +use ops::FnOnce; use result::Result::Ok; use slice::{mod, SlicePrelude}; use str::StrPrelude; @@ -84,7 +85,7 @@ static DIGIT_E_RADIX: uint = ('e' as uint) - ('a' as uint) + 11u; /// between digit and exponent sign `'e'`. /// - Panics if `radix` > 25 and `exp_format` is `ExpBin` due to conflict /// between digit and exponent sign `'p'`. -pub fn float_to_str_bytes_common( +pub fn float_to_str_bytes_common( num: T, radix: uint, negative_zero: bool, @@ -92,8 +93,10 @@ pub fn float_to_str_bytes_common( digits: SignificantDigits, exp_format: ExponentFormat, exp_upper: bool, - f: |&[u8]| -> U -) -> U { + f: F +) -> U where + F: FnOnce(&[u8]) -> U, +{ assert!(2 <= radix && radix <= 36); match exp_format { ExpDec if radix >= DIGIT_E_RADIX // decimal exponent 'e' -- cgit 1.4.1-3-g733a5 From f56f9728e626886a89be852e642b84be7be36ab8 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 6 Dec 2014 12:11:15 -0500 Subject: libcore: use unboxed closures in `slice::raw` free functions --- src/libcore/slice.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index af150994765..27a4328ba80 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -1725,6 +1725,7 @@ pub mod raw { use mem::transmute; use ptr::RawPtr; use raw::Slice; + use ops::FnOnce; use option::Option; use option::Option::{None, Some}; @@ -1732,8 +1733,9 @@ pub mod raw { /// not bytes). #[inline] #[deprecated = "renamed to slice::from_raw_buf"] - pub unsafe fn buf_as_slice(p: *const T, len: uint, f: |v: &[T]| -> U) - -> U { + pub unsafe fn buf_as_slice(p: *const T, len: uint, f: F) -> U where + F: FnOnce(&[T]) -> U, + { f(transmute(Slice { data: p, len: len @@ -1744,12 +1746,9 @@ pub mod raw { /// not bytes). #[inline] #[deprecated = "renamed to slice::from_raw_mut_buf"] - pub unsafe fn mut_buf_as_slice( - p: *mut T, - len: uint, - f: |v: &mut [T]| -> U) - -> U { + pub unsafe fn mut_buf_as_slice(p: *mut T, len: uint, f: F) -> U where + F: FnOnce(&mut [T]) -> U, + { f(transmute(Slice { data: p as *const T, len: len -- cgit 1.4.1-3-g733a5