From 0dfd875b6efa68ed67988a2f9856fc3bbdc91ce2 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Tue, 24 Nov 2015 11:23:48 +1300 Subject: rustfmt libcollections --- src/libcollections/slice.rs | 112 ++++++++++++++++++++++++++++++-------------- 1 file changed, 76 insertions(+), 36 deletions(-) (limited to 'src/libcollections/slice.rs') diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index ec888127983..0e615402b46 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -156,7 +156,9 @@ mod hack { } #[inline] - pub fn to_vec(s: &[T]) -> Vec where T: Clone { + pub fn to_vec(s: &[T]) -> Vec + where T: Clone + { let mut vector = Vec::with_capacity(s.len()); vector.push_all(s); vector @@ -535,7 +537,9 @@ impl [T] { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn split(&self, pred: F) -> Split where F: FnMut(&T) -> bool { + pub fn split(&self, pred: F) -> Split + where F: FnMut(&T) -> bool + { core_slice::SliceExt::split(self, pred) } @@ -543,7 +547,9 @@ impl [T] { /// match `pred`. The matched element is not contained in the subslices. #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn split_mut(&mut self, pred: F) -> SplitMut where F: FnMut(&T) -> bool { + pub fn split_mut(&mut self, pred: F) -> SplitMut + where F: FnMut(&T) -> bool + { core_slice::SliceExt::split_mut(self, pred) } @@ -567,7 +573,9 @@ impl [T] { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn splitn(&self, n: usize, pred: F) -> SplitN where F: FnMut(&T) -> bool { + pub fn splitn(&self, n: usize, pred: F) -> SplitN + where F: FnMut(&T) -> bool + { core_slice::SliceExt::splitn(self, n, pred) } @@ -580,7 +588,8 @@ impl [T] { #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn splitn_mut(&mut self, n: usize, pred: F) -> SplitNMut - where F: FnMut(&T) -> bool { + where F: FnMut(&T) -> bool + { core_slice::SliceExt::splitn_mut(self, n, pred) } @@ -605,7 +614,9 @@ impl [T] { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn rsplitn(&self, n: usize, pred: F) -> RSplitN where F: FnMut(&T) -> bool { + pub fn rsplitn(&self, n: usize, pred: F) -> RSplitN + where F: FnMut(&T) -> bool + { core_slice::SliceExt::rsplitn(self, n, pred) } @@ -618,8 +629,9 @@ impl [T] { /// slice. #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn rsplitn_mut(&mut self, n: usize, pred: F) -> RSplitNMut - where F: FnMut(&T) -> bool { + pub fn rsplitn_mut(&mut self, n: usize, pred: F) -> RSplitNMut + where F: FnMut(&T) -> bool + { core_slice::SliceExt::rsplitn_mut(self, n, pred) } @@ -633,7 +645,9 @@ impl [T] { /// assert!(!v.contains(&50)); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn contains(&self, x: &T) -> bool where T: PartialEq { + pub fn contains(&self, x: &T) -> bool + where T: PartialEq + { core_slice::SliceExt::contains(self, x) } @@ -649,7 +663,9 @@ impl [T] { /// assert!(!v.starts_with(&[10, 50])); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn starts_with(&self, needle: &[T]) -> bool where T: PartialEq { + pub fn starts_with(&self, needle: &[T]) -> bool + where T: PartialEq + { core_slice::SliceExt::starts_with(self, needle) } @@ -665,7 +681,9 @@ impl [T] { /// assert!(!v.ends_with(&[50, 30])); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn ends_with(&self, needle: &[T]) -> bool where T: PartialEq { + pub fn ends_with(&self, needle: &[T]) -> bool + where T: PartialEq + { core_slice::SliceExt::ends_with(self, needle) } @@ -692,7 +710,9 @@ impl [T] { /// assert!(match r { Ok(1...4) => true, _ => false, }); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn binary_search(&self, x: &T) -> Result where T: Ord { + pub fn binary_search(&self, x: &T) -> Result + where T: Ord + { core_slice::SliceExt::binary_search(self, x) } @@ -729,7 +749,9 @@ impl [T] { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn binary_search_by(&self, f: F) -> Result where F: FnMut(&T) -> Ordering { + pub fn binary_search_by(&self, f: F) -> Result + where F: FnMut(&T) -> Ordering + { core_slice::SliceExt::binary_search_by(self, f) } @@ -749,7 +771,9 @@ impl [T] { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn sort(&mut self) where T: Ord { + pub fn sort(&mut self) + where T: Ord + { self.sort_by(|a, b| a.cmp(b)) } @@ -772,7 +796,9 @@ impl [T] { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn sort_by(&mut self, compare: F) where F: FnMut(&T, &T) -> Ordering { + pub fn sort_by(&mut self, compare: F) + where F: FnMut(&T, &T) -> Ordering + { merge_sort(self, compare) } @@ -796,14 +822,18 @@ impl [T] { /// assert!(dst == [3, 4, 5]); /// ``` #[unstable(feature = "clone_from_slice", issue = "27750")] - pub fn clone_from_slice(&mut self, src: &[T]) -> usize where T: Clone { + pub fn clone_from_slice(&mut self, src: &[T]) -> usize + where T: Clone + { core_slice::SliceExt::clone_from_slice(self, src) } /// Copies `self` into a new `Vec`. #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn to_vec(&self) -> Vec where T: Clone { + pub fn to_vec(&self) -> Vec + where T: Clone + { // NB see hack module in this file hack::to_vec(self) } @@ -886,7 +916,11 @@ impl> SliceConcatExt for [V] { let mut result = Vec::with_capacity(size + self.len()); let mut first = true; for v in self { - if first { first = false } else { result.push(sep.clone()) } + if first { + first = false + } else { + result.push(sep.clone()) + } result.push_all(v.borrow()) } result @@ -903,33 +937,43 @@ impl> SliceConcatExt for [V] { #[stable(feature = "rust1", since = "1.0.0")] impl Borrow<[T]> for Vec { - fn borrow(&self) -> &[T] { &self[..] } + fn borrow(&self) -> &[T] { + &self[..] + } } #[stable(feature = "rust1", since = "1.0.0")] impl BorrowMut<[T]> for Vec { - fn borrow_mut(&mut self) -> &mut [T] { &mut self[..] } + fn borrow_mut(&mut self) -> &mut [T] { + &mut self[..] + } } #[stable(feature = "rust1", since = "1.0.0")] impl ToOwned for [T] { type Owned = Vec; #[cfg(not(test))] - fn to_owned(&self) -> Vec { self.to_vec() } + fn to_owned(&self) -> Vec { + self.to_vec() + } // HACK(japaric): with cfg(test) the inherent `[T]::to_vec`, which is required for this method // definition, is not available. Since we don't require this method for testing purposes, I'll // just stub it // NB see the slice::hack module in slice.rs for more information #[cfg(test)] - fn to_owned(&self) -> Vec { panic!("not available with cfg(test)") } + fn to_owned(&self) -> Vec { + panic!("not available with cfg(test)") + } } //////////////////////////////////////////////////////////////////////////////// // Sorting //////////////////////////////////////////////////////////////////////////////// -fn insertion_sort(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> Ordering { +fn insertion_sort(v: &mut [T], mut compare: F) + where F: FnMut(&T, &T) -> Ordering +{ let len = v.len() as isize; let buf_v = v.as_mut_ptr(); @@ -945,8 +989,7 @@ fn insertion_sort(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> O // rather than <=, to maintain stability. // 0 <= j - 1 < len, so .offset(j - 1) is in bounds. - while j > 0 && - compare(&*read_ptr, &*buf_v.offset(j - 1)) == Less { + while j > 0 && compare(&*read_ptr, &*buf_v.offset(j - 1)) == Less { j -= 1; } @@ -959,9 +1002,7 @@ fn insertion_sort(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> O if i != j { let tmp = ptr::read(read_ptr); - ptr::copy(&*buf_v.offset(j), - buf_v.offset(j + 1), - (i - j) as usize); + ptr::copy(&*buf_v.offset(j), buf_v.offset(j + 1), (i - j) as usize); ptr::copy_nonoverlapping(&tmp, buf_v.offset(j), 1); mem::forget(tmp); } @@ -969,7 +1010,9 @@ fn insertion_sort(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> O } } -fn merge_sort(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> Ordering { +fn merge_sort(v: &mut [T], mut compare: F) + where F: FnMut(&T, &T) -> Ordering +{ // warning: this wildly uses unsafe. const BASE_INSERTION: usize = 32; const LARGE_INSERTION: usize = 16; @@ -998,7 +1041,7 @@ fn merge_sort(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> Order let mut working_space = Vec::with_capacity(2 * len); // these both are buffers of length `len`. let mut buf_dat = working_space.as_mut_ptr(); - let mut buf_tmp = unsafe {buf_dat.offset(len as isize)}; + let mut buf_tmp = unsafe { buf_dat.offset(len as isize) }; // length `len`. let buf_v = v.as_ptr(); @@ -1010,7 +1053,7 @@ fn merge_sort(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> Order // We could hardcode the sorting comparisons here, and we could // manipulate/step the pointers themselves, rather than repeatedly // .offset-ing. - for start in (0.. len).step_by(insertion) { + for start in (0..len).step_by(insertion) { // start <= i < len; for i in start..cmp::min(start + insertion, len) { // j satisfies: start <= j <= i; @@ -1024,8 +1067,7 @@ fn merge_sort(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> Order // start <= j - 1 < len, so .offset(j - 1) is in // bounds. - while j > start as isize && - compare(&*read_ptr, &*buf_dat.offset(j - 1)) == Less { + while j > start as isize && compare(&*read_ptr, &*buf_dat.offset(j - 1)) == Less { j -= 1; } @@ -1035,9 +1077,7 @@ fn merge_sort(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> Order // j + 1 could be `len` (for the last `i`), but in // that case, `i == j` so we don't copy. The // `.offset(j)` is always in bounds. - ptr::copy(&*buf_dat.offset(j), - buf_dat.offset(j + 1), - i - j as usize); + ptr::copy(&*buf_dat.offset(j), buf_dat.offset(j + 1), i - j as usize); ptr::copy_nonoverlapping(read_ptr, buf_dat.offset(j), 1); } } -- cgit 1.4.1-3-g733a5