From a7a065bd98e9959d09362ca4982c2f7226ddc94b Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Fri, 5 Dec 2014 16:59:13 -0500 Subject: [PATCH] libcollections: use unboxed closures in `[Clone]SliceAllocPrelude` methods --- src/libcollections/slice.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index 6bdfa490748f..463e28b420d1 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -94,6 +94,7 @@ use core::cmp; use core::kinds::{Copy, Sized}; use core::mem::size_of; use core::mem; +use core::ops::FnMut; use core::prelude::{Clone, Greater, Iterator, IteratorExt, Less, None, Option}; use core::prelude::{Ord, Ordering, RawPtr, Some, range}; use core::ptr; @@ -296,7 +297,7 @@ pub trait CloneSliceAllocPrelude for Sized? { /// Partitions the vector into two vectors `(a, b)`, where all /// elements of `a` satisfy `f` and all elements of `b` do not. - fn partitioned(&self, f: |&T| -> bool) -> (Vec, Vec); + fn partitioned(&self, f: F) -> (Vec, Vec) where F: FnMut(&T) -> bool; /// Creates an iterator that yields every possible permutation of the /// vector in succession. @@ -336,7 +337,7 @@ impl CloneSliceAllocPrelude for [T] { #[inline] - fn partitioned(&self, f: |&T| -> bool) -> (Vec, Vec) { + fn partitioned(&self, mut f: F) -> (Vec, Vec) where F: FnMut(&T) -> bool { let mut lefts = Vec::new(); let mut rights = Vec::new(); @@ -361,7 +362,7 @@ impl CloneSliceAllocPrelude for [T] { } -fn insertion_sort(v: &mut [T], compare: |&T, &T| -> Ordering) { +fn insertion_sort(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> Ordering { let len = v.len() as int; let buf_v = v.as_mut_ptr(); @@ -403,7 +404,7 @@ fn insertion_sort(v: &mut [T], compare: |&T, &T| -> Ordering) { } } -fn merge_sort(v: &mut [T], compare: |&T, &T| -> Ordering) { +fn merge_sort(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> Ordering { // warning: this wildly uses unsafe. static BASE_INSERTION: uint = 32; static LARGE_INSERTION: uint = 16; @@ -611,7 +612,7 @@ pub trait SliceAllocPrelude for Sized? { /// v.sort_by(|a, b| b.cmp(a)); /// assert!(v == [5, 4, 3, 2, 1]); /// ``` - fn sort_by(&mut self, compare: |&T, &T| -> Ordering); + fn sort_by(&mut self, compare: F) where F: FnMut(&T, &T) -> Ordering; /// Consumes `src` and moves as many elements as it can into `self` /// from the range [start,end). @@ -639,7 +640,7 @@ pub trait SliceAllocPrelude for Sized? { impl SliceAllocPrelude for [T] { #[inline] - fn sort_by(&mut self, compare: |&T, &T| -> Ordering) { + fn sort_by(&mut self, compare: F) where F: FnMut(&T, &T) -> Ordering { merge_sort(self, compare) }