From 09ed97e1ee45755f471418d1bc2a25d127c9cabe Mon Sep 17 00:00:00 2001 From: Srinivas Reddy Thatiparthy Date: Mon, 30 May 2016 21:03:05 +0530 Subject: [PATCH] run rustfmt on libcollections module --- src/libcollections/binary_heap.rs | 4 +- src/libcollections/borrow.rs | 36 +- src/libcollections/btree/map.rs | 474 ++++++++++++---------- src/libcollections/btree/node.rs | 627 +++++++++++------------------ src/libcollections/btree/search.rs | 62 +-- src/libcollections/btree/set.rs | 16 +- src/libcollections/enum_set.rs | 5 +- src/libcollections/fmt.rs | 6 +- src/libcollections/linked_list.rs | 32 +- src/libcollections/range.rs | 2 +- src/libcollections/str.rs | 16 +- src/libcollections/string.rs | 4 +- src/libcollections/vec_deque.rs | 65 ++- 13 files changed, 642 insertions(+), 707 deletions(-) diff --git a/src/libcollections/binary_heap.rs b/src/libcollections/binary_heap.rs index 43c6e6e81209d..8cee0fce48ae9 100644 --- a/src/libcollections/binary_heap.rs +++ b/src/libcollections/binary_heap.rs @@ -1034,7 +1034,9 @@ impl IntoIterator for BinaryHeap { } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T> IntoIterator for &'a BinaryHeap where T: Ord { +impl<'a, T> IntoIterator for &'a BinaryHeap + where T: Ord +{ type Item = &'a T; type IntoIter = Iter<'a, T>; diff --git a/src/libcollections/borrow.rs b/src/libcollections/borrow.rs index 6ca0db68a88ce..84a12f7699c45 100644 --- a/src/libcollections/borrow.rs +++ b/src/libcollections/borrow.rs @@ -66,7 +66,9 @@ pub trait ToOwned { } #[stable(feature = "rust1", since = "1.0.0")] -impl ToOwned for T where T: Clone { +impl ToOwned for T + where T: Clone +{ type Owned = T; fn to_owned(&self) -> T { self.clone() @@ -107,17 +109,19 @@ pub enum Cow<'a, B: ?Sized + 'a> { /// Borrowed data. #[stable(feature = "rust1", since = "1.0.0")] - Borrowed(#[stable(feature = "rust1", since = "1.0.0")] &'a B), + Borrowed(#[stable(feature = "rust1", since = "1.0.0")] + &'a B), /// Owned data. #[stable(feature = "rust1", since = "1.0.0")] - Owned( - #[stable(feature = "rust1", since = "1.0.0")] ::Owned - ), + Owned(#[stable(feature = "rust1", since = "1.0.0")] + ::Owned), } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, B: ?Sized> Clone for Cow<'a, B> where B: ToOwned { +impl<'a, B: ?Sized> Clone for Cow<'a, B> + where B: ToOwned +{ fn clone(&self) -> Cow<'a, B> { match *self { Borrowed(b) => Borrowed(b), @@ -129,7 +133,9 @@ impl<'a, B: ?Sized> Clone for Cow<'a, B> where B: ToOwned { } } -impl<'a, B: ?Sized> Cow<'a, B> where B: ToOwned { +impl<'a, B: ?Sized> Cow<'a, B> + where B: ToOwned +{ /// Acquires a mutable reference to the owned form of the data. /// /// Clones the data if it is not already owned. @@ -181,7 +187,9 @@ impl<'a, B: ?Sized> Cow<'a, B> where B: ToOwned { } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, B: ?Sized> Deref for Cow<'a, B> where B: ToOwned { +impl<'a, B: ?Sized> Deref for Cow<'a, B> + where B: ToOwned +{ type Target = B; fn deref(&self) -> &B { @@ -196,7 +204,9 @@ impl<'a, B: ?Sized> Deref for Cow<'a, B> where B: ToOwned { impl<'a, B: ?Sized> Eq for Cow<'a, B> where B: Eq + ToOwned {} #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, B: ?Sized> Ord for Cow<'a, B> where B: Ord + ToOwned { +impl<'a, B: ?Sized> Ord for Cow<'a, B> + where B: Ord + ToOwned +{ #[inline] fn cmp(&self, other: &Cow<'a, B>) -> Ordering { Ord::cmp(&**self, &**other) @@ -215,7 +225,9 @@ impl<'a, 'b, B: ?Sized, C: ?Sized> PartialEq> for Cow<'a, B> } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, B: ?Sized> PartialOrd for Cow<'a, B> where B: PartialOrd + ToOwned { +impl<'a, B: ?Sized> PartialOrd for Cow<'a, B> + where B: PartialOrd + ToOwned +{ #[inline] fn partial_cmp(&self, other: &Cow<'a, B>) -> Option { PartialOrd::partial_cmp(&**self, &**other) @@ -249,7 +261,9 @@ impl<'a, B: ?Sized> fmt::Display for Cow<'a, B> } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, B: ?Sized> Hash for Cow<'a, B> where B: Hash + ToOwned { +impl<'a, B: ?Sized> Hash for Cow<'a, B> + where B: Hash + ToOwned +{ #[inline] fn hash(&self, state: &mut H) { Hash::hash(&**self, state) diff --git a/src/libcollections/btree/map.rs b/src/libcollections/btree/map.rs index ec2f4a9f7f0b8..5edb12e90db08 100644 --- a/src/libcollections/btree/map.rs +++ b/src/libcollections/btree/map.rs @@ -17,9 +17,9 @@ use core::ops::Index; use core::{fmt, intrinsics, mem, ptr}; use borrow::Borrow; -use Bound::{self, Included, Excluded, Unbounded}; +use Bound::{self, Excluded, Included, Unbounded}; -use super::node::{self, NodeRef, Handle, marker}; +use super::node::{self, Handle, NodeRef, marker}; use super::search; use super::node::InsertResult::*; @@ -129,35 +129,38 @@ use self::Entry::*; #[stable(feature = "rust1", since = "1.0.0")] pub struct BTreeMap { root: node::Root, - length: usize + length: usize, } impl Drop for BTreeMap { #[unsafe_destructor_blind_to_params] fn drop(&mut self) { unsafe { - for _ in ptr::read(self).into_iter() { } + for _ in ptr::read(self).into_iter() { + } } } } impl Clone for BTreeMap { fn clone(&self) -> BTreeMap { - fn clone_subtree( - node: node::NodeRef) - -> BTreeMap { + fn clone_subtree(node: node::NodeRef) + -> BTreeMap { match node.force() { Leaf(leaf) => { let mut out_tree = BTreeMap { root: node::Root::new_leaf(), - length: 0 + length: 0, }; { let mut out_node = match out_tree.root.as_mut().force() { Leaf(leaf) => leaf, - Internal(_) => unreachable!() + Internal(_) => unreachable!(), }; let mut in_edge = leaf.first_edge(); @@ -171,7 +174,7 @@ impl Clone for BTreeMap { } out_tree - }, + } Internal(internal) => { let mut out_tree = clone_subtree(internal.first_edge().descend()); @@ -218,7 +221,7 @@ impl super::Recover for BTreeMap fn get(&self, key: &Q) -> Option<&K> { match search::search_tree(self.root.as_ref(), key) { Found(handle) => Some(handle.into_kv().0), - GoDown(_) => None + GoDown(_) => None, } } @@ -226,12 +229,14 @@ impl super::Recover for BTreeMap match search::search_tree(self.root.as_mut(), key) { Found(handle) => { Some(OccupiedEntry { - handle: handle, - length: &mut self.length, - _marker: PhantomData, - }.remove_kv().0) - }, - GoDown(_) => None + handle: handle, + length: &mut self.length, + _marker: PhantomData, + } + .remove_kv() + .0) + } + GoDown(_) => None, } } @@ -244,7 +249,8 @@ impl super::Recover for BTreeMap handle: handle, length: &mut self.length, _marker: PhantomData, - }.insert(()); + } + .insert(()); None } } @@ -255,14 +261,14 @@ impl super::Recover for BTreeMap #[stable(feature = "rust1", since = "1.0.0")] pub struct Iter<'a, K: 'a, V: 'a> { range: Range<'a, K, V>, - length: usize + length: usize, } /// A mutable iterator over a BTreeMap's entries. #[stable(feature = "rust1", since = "1.0.0")] pub struct IterMut<'a, K: 'a, V: 'a> { range: RangeMut<'a, K, V>, - length: usize + length: usize, } /// An owning iterator over a BTreeMap's entries. @@ -270,7 +276,7 @@ pub struct IterMut<'a, K: 'a, V: 'a> { pub struct IntoIter { front: Handle, marker::Edge>, back: Handle, marker::Edge>, - length: usize + length: usize, } /// An iterator over a BTreeMap's keys. @@ -294,7 +300,7 @@ pub struct ValuesMut<'a, K: 'a, V: 'a> { /// An iterator over a sub-range of BTreeMap's entries. pub struct Range<'a, K: 'a, V: 'a> { front: Handle, K, V, marker::Leaf>, marker::Edge>, - back: Handle, K, V, marker::Leaf>, marker::Edge> + back: Handle, K, V, marker::Leaf>, marker::Edge>, } /// A mutable iterator over a sub-range of BTreeMap's entries. @@ -311,15 +317,13 @@ pub struct RangeMut<'a, K: 'a, V: 'a> { pub enum Entry<'a, K: 'a, V: 'a> { /// A vacant Entry #[stable(feature = "rust1", since = "1.0.0")] - Vacant( - #[stable(feature = "rust1", since = "1.0.0")] VacantEntry<'a, K, V> - ), + Vacant(#[stable(feature = "rust1", since = "1.0.0")] + VacantEntry<'a, K, V>), /// An occupied Entry #[stable(feature = "rust1", since = "1.0.0")] - Occupied( - #[stable(feature = "rust1", since = "1.0.0")] OccupiedEntry<'a, K, V> - ), + Occupied(#[stable(feature = "rust1", since = "1.0.0")] + OccupiedEntry<'a, K, V>), } /// A vacant Entry. @@ -336,11 +340,7 @@ pub struct VacantEntry<'a, K: 'a, V: 'a> { /// An occupied Entry. #[stable(feature = "rust1", since = "1.0.0")] pub struct OccupiedEntry<'a, K: 'a, V: 'a> { - handle: Handle, - K, V, - marker::LeafOrInternal - >, marker::KV>, + handle: Handle, K, V, marker::LeafOrInternal>, marker::KV>, length: &'a mut usize, @@ -349,7 +349,7 @@ pub struct OccupiedEntry<'a, K: 'a, V: 'a> { } // An iterator for merging two sorted sequences into one -struct MergeIter> { +struct MergeIter> { left: Peekable, right: Peekable, } @@ -373,7 +373,7 @@ impl BTreeMap { pub fn new() -> BTreeMap { BTreeMap { root: node::Root::new_leaf(), - length: 0 + length: 0, } } @@ -415,10 +415,13 @@ impl BTreeMap { /// assert_eq!(map.get(&2), None); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn get(&self, key: &Q) -> Option<&V> where K: Borrow, Q: Ord { + pub fn get(&self, key: &Q) -> Option<&V> + where K: Borrow, + Q: Ord + { match search::search_tree(self.root.as_ref(), key) { Found(handle) => Some(handle.into_kv().1), - GoDown(_) => None + GoDown(_) => None, } } @@ -440,7 +443,10 @@ impl BTreeMap { /// assert_eq!(map.contains_key(&2), false); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn contains_key(&self, key: &Q) -> bool where K: Borrow, Q: Ord { + pub fn contains_key(&self, key: &Q) -> bool + where K: Borrow, + Q: Ord + { self.get(key).is_some() } @@ -465,10 +471,13 @@ impl BTreeMap { /// ``` // See `get` for implementation notes, this is basically a copy-paste with mut's added #[stable(feature = "rust1", since = "1.0.0")] - pub fn get_mut(&mut self, key: &Q) -> Option<&mut V> where K: Borrow, Q: Ord { + pub fn get_mut(&mut self, key: &Q) -> Option<&mut V> + where K: Borrow, + Q: Ord + { match search::search_tree(self.root.as_mut(), key) { Found(handle) => Some(handle.into_kv_mut().1), - GoDown(_) => None + GoDown(_) => None, } } @@ -528,16 +537,20 @@ impl BTreeMap { /// assert_eq!(map.remove(&1), None); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - pub fn remove(&mut self, key: &Q) -> Option where K: Borrow, Q: Ord { + pub fn remove(&mut self, key: &Q) -> Option + where K: Borrow, + Q: Ord + { match search::search_tree(self.root.as_mut(), key) { Found(handle) => { Some(OccupiedEntry { - handle: handle, - length: &mut self.length, - _marker: PhantomData, - }.remove()) - }, - GoDown(_) => None + handle: handle, + length: &mut self.length, + _marker: PhantomData, + } + .remove()) + } + GoDown(_) => None, } } @@ -628,47 +641,63 @@ impl BTreeMap { min: Bound<&Min>, max: Bound<&Max>) -> Range - where K: Borrow + Borrow, + where K: Borrow + Borrow { let front = match min { - Included(key) => match search::search_tree(self.root.as_ref(), key) { - Found(kv_handle) => match kv_handle.left_edge().force() { - Leaf(bottom) => bottom, - Internal(internal) => last_leaf_edge(internal.descend()) - }, - GoDown(bottom) => bottom - }, - Excluded(key) => match search::search_tree(self.root.as_ref(), key) { - Found(kv_handle) => match kv_handle.right_edge().force() { - Leaf(bottom) => bottom, - Internal(internal) => first_leaf_edge(internal.descend()) - }, - GoDown(bottom) => bottom - }, - Unbounded => first_leaf_edge(self.root.as_ref()) + Included(key) => { + match search::search_tree(self.root.as_ref(), key) { + Found(kv_handle) => { + match kv_handle.left_edge().force() { + Leaf(bottom) => bottom, + Internal(internal) => last_leaf_edge(internal.descend()), + } + } + GoDown(bottom) => bottom, + } + } + Excluded(key) => { + match search::search_tree(self.root.as_ref(), key) { + Found(kv_handle) => { + match kv_handle.right_edge().force() { + Leaf(bottom) => bottom, + Internal(internal) => first_leaf_edge(internal.descend()), + } + } + GoDown(bottom) => bottom, + } + } + Unbounded => first_leaf_edge(self.root.as_ref()), }; let back = match max { - Included(key) => match search::search_tree(self.root.as_ref(), key) { - Found(kv_handle) => match kv_handle.right_edge().force() { - Leaf(bottom) => bottom, - Internal(internal) => first_leaf_edge(internal.descend()) - }, - GoDown(bottom) => bottom - }, - Excluded(key) => match search::search_tree(self.root.as_ref(), key) { - Found(kv_handle) => match kv_handle.left_edge().force() { - Leaf(bottom) => bottom, - Internal(internal) => last_leaf_edge(internal.descend()) - }, - GoDown(bottom) => bottom - }, - Unbounded => last_leaf_edge(self.root.as_ref()) + Included(key) => { + match search::search_tree(self.root.as_ref(), key) { + Found(kv_handle) => { + match kv_handle.right_edge().force() { + Leaf(bottom) => bottom, + Internal(internal) => first_leaf_edge(internal.descend()), + } + } + GoDown(bottom) => bottom, + } + } + Excluded(key) => { + match search::search_tree(self.root.as_ref(), key) { + Found(kv_handle) => { + match kv_handle.left_edge().force() { + Leaf(bottom) => bottom, + Internal(internal) => last_leaf_edge(internal.descend()), + } + } + GoDown(bottom) => bottom, + } + } + Unbounded => last_leaf_edge(self.root.as_ref()), }; Range { front: front, - back: back + back: back, } } @@ -704,51 +733,67 @@ impl BTreeMap { min: Bound<&Min>, max: Bound<&Max>) -> RangeMut - where K: Borrow + Borrow, + where K: Borrow + Borrow { let root1 = self.root.as_mut(); let root2 = unsafe { ptr::read(&root1) }; let front = match min { - Included(key) => match search::search_tree(root1, key) { - Found(kv_handle) => match kv_handle.left_edge().force() { - Leaf(bottom) => bottom, - Internal(internal) => last_leaf_edge(internal.descend()) - }, - GoDown(bottom) => bottom - }, - Excluded(key) => match search::search_tree(root1, key) { - Found(kv_handle) => match kv_handle.right_edge().force() { - Leaf(bottom) => bottom, - Internal(internal) => first_leaf_edge(internal.descend()) - }, - GoDown(bottom) => bottom - }, - Unbounded => first_leaf_edge(root1) + Included(key) => { + match search::search_tree(root1, key) { + Found(kv_handle) => { + match kv_handle.left_edge().force() { + Leaf(bottom) => bottom, + Internal(internal) => last_leaf_edge(internal.descend()), + } + } + GoDown(bottom) => bottom, + } + } + Excluded(key) => { + match search::search_tree(root1, key) { + Found(kv_handle) => { + match kv_handle.right_edge().force() { + Leaf(bottom) => bottom, + Internal(internal) => first_leaf_edge(internal.descend()), + } + } + GoDown(bottom) => bottom, + } + } + Unbounded => first_leaf_edge(root1), }; let back = match max { - Included(key) => match search::search_tree(root2, key) { - Found(kv_handle) => match kv_handle.right_edge().force() { - Leaf(bottom) => bottom, - Internal(internal) => first_leaf_edge(internal.descend()) - }, - GoDown(bottom) => bottom - }, - Excluded(key) => match search::search_tree(root2, key) { - Found(kv_handle) => match kv_handle.left_edge().force() { - Leaf(bottom) => bottom, - Internal(internal) => last_leaf_edge(internal.descend()) - }, - GoDown(bottom) => bottom - }, - Unbounded => last_leaf_edge(root2) + Included(key) => { + match search::search_tree(root2, key) { + Found(kv_handle) => { + match kv_handle.right_edge().force() { + Leaf(bottom) => bottom, + Internal(internal) => first_leaf_edge(internal.descend()), + } + } + GoDown(bottom) => bottom, + } + } + Excluded(key) => { + match search::search_tree(root2, key) { + Found(kv_handle) => { + match kv_handle.left_edge().force() { + Leaf(bottom) => bottom, + Internal(internal) => last_leaf_edge(internal.descend()), + } + } + GoDown(bottom) => bottom, + } + } + Unbounded => last_leaf_edge(root2), }; RangeMut { front: front, back: back, - _marker: PhantomData + _marker: PhantomData, } } @@ -773,21 +818,25 @@ impl BTreeMap { #[stable(feature = "rust1", since = "1.0.0")] pub fn entry(&mut self, key: K) -> Entry { match search::search_tree(self.root.as_mut(), &key) { - Found(handle) => Occupied(OccupiedEntry { - handle: handle, - length: &mut self.length, - _marker: PhantomData, - }), - GoDown(handle) => Vacant(VacantEntry { - key: key, - handle: handle, - length: &mut self.length, - _marker: PhantomData, - }) + Found(handle) => { + Occupied(OccupiedEntry { + handle: handle, + length: &mut self.length, + _marker: PhantomData, + }) + } + GoDown(handle) => { + Vacant(VacantEntry { + key: key, + handle: handle, + length: &mut self.length, + _marker: PhantomData, + }) + } } } - fn from_sorted_iter>(&mut self, iter: I) { + fn from_sorted_iter>(&mut self, iter: I) { let mut cur_node = last_leaf_edge(self.root.as_mut()).into_node(); // Iterate through all key-value pairs, pushing them into nodes at the right level. for (key, value) in iter { @@ -810,12 +859,12 @@ impl BTreeMap { // Go up again. test_node = parent.forget_type(); } - }, + } Err(node) => { // We are at the top, create a new root node and push there. open_node = node.into_root_mut().push_level(); break; - }, + } } } @@ -848,7 +897,7 @@ impl BTreeMap { Ok(left) => left, Err(_) => unreachable!(), }; - last_kv.bulk_steal_left(node::CAPACITY/2 - right_child_len); + last_kv.bulk_steal_left(node::CAPACITY / 2 - right_child_len); last_edge = last_kv.right_edge(); } @@ -896,14 +945,16 @@ impl<'a, K: 'a, V: 'a> DoubleEndedIterator for Iter<'a, K, V> { } impl<'a, K: 'a, V: 'a> ExactSizeIterator for Iter<'a, K, V> { - fn len(&self) -> usize { self.length } + fn len(&self) -> usize { + self.length + } } impl<'a, K, V> Clone for Iter<'a, K, V> { fn clone(&self) -> Iter<'a, K, V> { Iter { range: self.range.clone(), - length: self.length + length: self.length, } } } @@ -946,7 +997,9 @@ impl<'a, K: 'a, V: 'a> DoubleEndedIterator for IterMut<'a, K, V> { } impl<'a, K: 'a, V: 'a> ExactSizeIterator for IterMut<'a, K, V> { - fn len(&self) -> usize { self.length } + fn len(&self) -> usize { + self.length + } } impl IntoIterator for BTreeMap { @@ -962,14 +1015,15 @@ impl IntoIterator for BTreeMap { IntoIter { front: first_leaf_edge(root1), back: last_leaf_edge(root2), - length: len + length: len, } } } impl Drop for IntoIter { fn drop(&mut self) { - for _ in &mut *self { } + for _ in &mut *self { + } unsafe { let leaf_node = ptr::read(&self.front).into_node(); if let Some(first_parent) = leaf_node.deallocate_and_ascend() { @@ -1000,10 +1054,10 @@ impl Iterator for IntoIter { let v = unsafe { ptr::read(kv.reborrow().into_kv().1) }; self.front = kv.right_edge(); return Some((k, v)); - }, + } Err(last_edge) => unsafe { unwrap_unchecked(last_edge.into_node().deallocate_and_ascend()) - } + }, }; loop { @@ -1013,10 +1067,10 @@ impl Iterator for IntoIter { let v = unsafe { ptr::read(kv.reborrow().into_kv().1) }; self.front = first_leaf_edge(kv.right_edge().descend()); return Some((k, v)); - }, + } Err(last_edge) => unsafe { cur_handle = unwrap_unchecked(last_edge.into_node().deallocate_and_ascend()); - } + }, } } } @@ -1042,10 +1096,10 @@ impl DoubleEndedIterator for IntoIter { let v = unsafe { ptr::read(kv.reborrow().into_kv().1) }; self.back = kv.left_edge(); return Some((k, v)); - }, + } Err(last_edge) => unsafe { unwrap_unchecked(last_edge.into_node().deallocate_and_ascend()) - } + }, }; loop { @@ -1055,17 +1109,19 @@ impl DoubleEndedIterator for IntoIter { let v = unsafe { ptr::read(kv.reborrow().into_kv().1) }; self.back = last_leaf_edge(kv.left_edge().descend()); return Some((k, v)); - }, + } Err(last_edge) => unsafe { cur_handle = unwrap_unchecked(last_edge.into_node().deallocate_and_ascend()); - } + }, } } } } impl ExactSizeIterator for IntoIter { - fn len(&self) -> usize { self.length } + fn len(&self) -> usize { + self.length + } } impl<'a, K, V> Iterator for Keys<'a, K, V> { @@ -1094,9 +1150,7 @@ impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> { impl<'a, K, V> Clone for Keys<'a, K, V> { fn clone(&self) -> Keys<'a, K, V> { - Keys { - inner: self.inner.clone() - } + Keys { inner: self.inner.clone() } } } @@ -1126,9 +1180,7 @@ impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> { impl<'a, K, V> Clone for Values<'a, K, V> { fn clone(&self) -> Values<'a, K, V> { - Values { - inner: self.inner.clone() - } + Values { inner: self.inner.clone() } } } @@ -1180,7 +1232,7 @@ impl<'a, K, V> Range<'a, K, V> { let ret = kv.into_kv(); self.front = kv.right_edge(); return ret; - }, + } Err(last_edge) => { let next_level = last_edge.into_node().ascend().ok(); unwrap_unchecked(next_level) @@ -1193,7 +1245,7 @@ impl<'a, K, V> Range<'a, K, V> { let ret = kv.into_kv(); self.front = first_leaf_edge(kv.right_edge().descend()); return ret; - }, + } Err(last_edge) => { let next_level = last_edge.into_node().ascend().ok(); cur_handle = unwrap_unchecked(next_level); @@ -1222,7 +1274,7 @@ impl<'a, K, V> Range<'a, K, V> { let ret = kv.into_kv(); self.back = kv.left_edge(); return ret; - }, + } Err(last_edge) => { let next_level = last_edge.into_node().ascend().ok(); unwrap_unchecked(next_level) @@ -1235,7 +1287,7 @@ impl<'a, K, V> Range<'a, K, V> { let ret = kv.into_kv(); self.back = last_leaf_edge(kv.left_edge().descend()); return ret; - }, + } Err(last_edge) => { let next_level = last_edge.into_node().ascend().ok(); cur_handle = unwrap_unchecked(next_level); @@ -1249,7 +1301,7 @@ impl<'a, K, V> Clone for Range<'a, K, V> { fn clone(&self) -> Range<'a, K, V> { Range { front: self.front, - back: self.back + back: self.back, } } } @@ -1261,7 +1313,7 @@ impl<'a, K, V> Iterator for RangeMut<'a, K, V> { if self.front == self.back { None } else { - unsafe { Some (self.next_unchecked()) } + unsafe { Some(self.next_unchecked()) } } } } @@ -1275,7 +1327,7 @@ impl<'a, K, V> RangeMut<'a, K, V> { let (k, v) = ptr::read(&kv).into_kv_mut(); self.front = kv.right_edge(); return (k, v); - }, + } Err(last_edge) => { let next_level = last_edge.into_node().ascend().ok(); unwrap_unchecked(next_level) @@ -1288,7 +1340,7 @@ impl<'a, K, V> RangeMut<'a, K, V> { let (k, v) = ptr::read(&kv).into_kv_mut(); self.front = first_leaf_edge(kv.right_edge().descend()); return (k, v); - }, + } Err(last_edge) => { let next_level = last_edge.into_node().ascend().ok(); cur_handle = unwrap_unchecked(next_level); @@ -1317,7 +1369,7 @@ impl<'a, K, V> RangeMut<'a, K, V> { let (k, v) = ptr::read(&kv).into_kv_mut(); self.back = kv.left_edge(); return (k, v); - }, + } Err(last_edge) => { let next_level = last_edge.into_node().ascend().ok(); unwrap_unchecked(next_level) @@ -1330,7 +1382,7 @@ impl<'a, K, V> RangeMut<'a, K, V> { let (k, v) = ptr::read(&kv).into_kv_mut(); self.back = last_leaf_edge(kv.left_edge().descend()); return (k, v); - }, + } Err(last_edge) => { let next_level = last_edge.into_node().ascend().ok(); cur_handle = unwrap_unchecked(next_level); @@ -1341,7 +1393,7 @@ impl<'a, K, V> RangeMut<'a, K, V> { } impl FromIterator<(K, V)> for BTreeMap { - fn from_iter>(iter: T) -> BTreeMap { + fn from_iter>(iter: T) -> BTreeMap { let mut map = BTreeMap::new(); map.extend(iter); map @@ -1350,7 +1402,7 @@ impl FromIterator<(K, V)> for BTreeMap { impl Extend<(K, V)> for BTreeMap { #[inline] - fn extend>(&mut self, iter: T) { + fn extend>(&mut self, iter: T) { for (k, v) in iter { self.insert(k, v); } @@ -1358,7 +1410,7 @@ impl Extend<(K, V)> for BTreeMap { } impl<'a, K: Ord + Copy, V: Copy> Extend<(&'a K, &'a V)> for BTreeMap { - fn extend>(&mut self, iter: I) { + fn extend>(&mut self, iter: I) { self.extend(iter.into_iter().map(|(&key, &value)| (key, value))); } } @@ -1379,8 +1431,7 @@ impl Default for BTreeMap { impl PartialEq for BTreeMap { fn eq(&self, other: &BTreeMap) -> bool { - self.len() == other.len() && - self.iter().zip(other).all(|(a, b)| a == b) + self.len() == other.len() && self.iter().zip(other).all(|(a, b)| a == b) } } @@ -1407,7 +1458,8 @@ impl Debug for BTreeMap { } impl<'a, K: Ord, Q: ?Sized, V> Index<&'a Q> for BTreeMap - where K: Borrow, Q: Ord + where K: Borrow, + Q: Ord { type Output = V; @@ -1417,11 +1469,9 @@ impl<'a, K: Ord, Q: ?Sized, V> Index<&'a Q> for BTreeMap } } -fn first_leaf_edge( - mut node: NodeRef - ) -> Handle, marker::Edge> { +fn first_leaf_edge + (mut node: NodeRef) + -> Handle, marker::Edge> { loop { match node.force() { Leaf(leaf) => return leaf.first_edge(), @@ -1432,11 +1482,9 @@ fn first_leaf_edge( } } -fn last_leaf_edge( - mut node: NodeRef - ) -> Handle, marker::Edge> { +fn last_leaf_edge + (mut node: NodeRef) + -> Handle, marker::Edge> { loop { match node.force() { Leaf(leaf) => return leaf.last_edge(), @@ -1485,9 +1533,9 @@ impl BTreeMap { Iter { range: Range { front: first_leaf_edge(self.root.as_ref()), - back: last_leaf_edge(self.root.as_ref()) + back: last_leaf_edge(self.root.as_ref()), }, - length: self.length + length: self.length, } } @@ -1522,7 +1570,7 @@ impl BTreeMap { back: last_leaf_edge(root2), _marker: PhantomData, }, - length: self.length + length: self.length, } } @@ -1697,15 +1745,17 @@ impl<'a, K: Ord, V> VacantEntry<'a, K, V> { loop { match cur_parent { - Ok(parent) => match parent.insert(ins_k, ins_v, ins_edge) { - Fit(_) => return unsafe { &mut *out_ptr }, - Split(left, k, v, right) => { - ins_k = k; - ins_v = v; - ins_edge = right; - cur_parent = left.ascend().map_err(|n| n.into_root_mut()); + Ok(parent) => { + match parent.insert(ins_k, ins_v, ins_edge) { + Fit(_) => return unsafe { &mut *out_ptr }, + Split(left, k, v, right) => { + ins_k = k; + ins_v = v; + ins_edge = right; + cur_parent = left.ascend().map_err(|n| n.into_root_mut()); + } } - }, + } Err(root) => { root.push_level().push(ins_k, ins_v, ins_edge); return unsafe { &mut *out_ptr }; @@ -1760,7 +1810,7 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> { Leaf(leaf) => { let (hole, old_key, old_val) = leaf.remove(); (hole.into_node(), old_key, old_val) - }, + } Internal(mut internal) => { let key_loc = internal.kv_mut().0 as *mut K; let val_loc = internal.kv_mut().1 as *mut V; @@ -1770,12 +1820,8 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> { let (hole, key, val) = to_remove.remove(); - let old_key = unsafe { - mem::replace(&mut *key_loc, key) - }; - let old_val = unsafe { - mem::replace(&mut *val_loc, val) - }; + let old_key = unsafe { mem::replace(&mut *key_loc, key) }; + let old_val = unsafe { mem::replace(&mut *val_loc, val) }; (hole.into_node(), old_key, old_val) } @@ -1787,14 +1833,16 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> { match handle_underfull_node(cur_node) { AtRoot => break, EmptyParent(_) => unreachable!(), - Merged(parent) => if parent.len() == 0 { - // We must be at the root - parent.into_root_mut().pop_level(); - break; - } else { - cur_node = parent.forget_type(); - }, - Stole(_) => break + Merged(parent) => { + if parent.len() == 0 { + // We must be at the root + parent.into_root_mut().pop_level(); + break; + } else { + cur_node = parent.forget_type(); + } + } + Stole(_) => break, } } @@ -1806,13 +1854,11 @@ enum UnderflowResult<'a, K, V> { AtRoot, EmptyParent(NodeRef, K, V, marker::Internal>), Merged(NodeRef, K, V, marker::Internal>), - Stole(NodeRef, K, V, marker::Internal>) + Stole(NodeRef, K, V, marker::Internal>), } -fn handle_underfull_node<'a, K, V>(node: NodeRef, - K, V, - marker::LeafOrInternal>) - -> UnderflowResult<'a, K, V> { +fn handle_underfull_node<'a, K, V>(node: NodeRef, K, V, marker::LeafOrInternal>) + -> UnderflowResult<'a, K, V> { let parent = if let Ok(parent) = node.ascend() { parent } else { @@ -1821,10 +1867,12 @@ fn handle_underfull_node<'a, K, V>(node: NodeRef, let (is_left, mut handle) = match parent.left_kv() { Ok(left) => (true, left), - Err(parent) => match parent.right_kv() { - Ok(right) => (false, right), - Err(parent) => { - return EmptyParent(parent.into_node()); + Err(parent) => { + match parent.right_kv() { + Ok(right) => (false, right), + Err(parent) => { + return EmptyParent(parent.into_node()); + } } } }; @@ -1841,7 +1889,7 @@ fn handle_underfull_node<'a, K, V>(node: NodeRef, } } -impl> Iterator for MergeIter { +impl> Iterator for MergeIter { type Item = (K, V); fn next(&mut self) -> Option<(K, V)> { @@ -1855,16 +1903,12 @@ impl> Iterator for MergeIter { // Check which elements comes first and only advance the corresponding iterator. // If two keys are equal, take the value from `right`. match res { - Ordering::Less => { - self.left.next() - }, - Ordering::Greater => { - self.right.next() - }, + Ordering::Less => self.left.next(), + Ordering::Greater => self.right.next(), Ordering::Equal => { self.left.next(); self.right.next() - }, + } } } } diff --git a/src/libcollections/btree/node.rs b/src/libcollections/btree/node.rs index ca1cf6bcc5028..d7f5e660cecd5 100644 --- a/src/libcollections/btree/node.rs +++ b/src/libcollections/btree/node.rs @@ -94,7 +94,7 @@ impl LeafNode { vals: mem::uninitialized(), parent: ptr::null(), parent_idx: mem::uninitialized(), - len: 0 + len: 0, } } } @@ -124,7 +124,7 @@ impl InternalNode { unsafe fn new() -> Self { InternalNode { data: LeafNode::new(), - edges: mem::uninitialized() + edges: mem::uninitialized(), } } } @@ -134,20 +134,16 @@ impl InternalNode { /// of nodes is acutally behind the box, and, partially due to this lack of information, has no /// destructor. struct BoxedNode { - ptr: Unique> + ptr: Unique>, } impl BoxedNode { fn from_leaf(node: Box>) -> Self { - unsafe { - BoxedNode { ptr: Unique::new(Box::into_raw(node)) } - } + unsafe { BoxedNode { ptr: Unique::new(Box::into_raw(node)) } } } fn from_internal(node: Box>) -> Self { - unsafe { - BoxedNode { ptr: Unique::new(Box::into_raw(node) as *mut LeafNode) } - } + unsafe { BoxedNode { ptr: Unique::new(Box::into_raw(node) as *mut LeafNode) } } } unsafe fn from_ptr(ptr: NonZero<*const LeafNode>) -> Self { @@ -155,9 +151,7 @@ impl BoxedNode { } fn as_ptr(&self) -> NonZero<*const LeafNode> { - unsafe { - NonZero::new(*self.ptr as *const LeafNode) - } + unsafe { NonZero::new(*self.ptr as *const LeafNode) } } } @@ -165,22 +159,21 @@ impl BoxedNode { /// and must be cleaned up manually. pub struct Root { node: BoxedNode, - height: usize + height: usize, } -unsafe impl Sync for Root { } -unsafe impl Send for Root { } +unsafe impl Sync for Root {} +unsafe impl Send for Root {} impl Root { pub fn new_leaf() -> Self { Root { node: BoxedNode::from_leaf(Box::new(unsafe { LeafNode::new() })), - height: 0 + height: 0, } } - pub fn as_ref(&self) - -> NodeRef { + pub fn as_ref(&self) -> NodeRef { NodeRef { height: self.height, node: self.node.as_ptr(), @@ -189,8 +182,7 @@ impl Root { } } - pub fn as_mut(&mut self) - -> NodeRef { + pub fn as_mut(&mut self) -> NodeRef { NodeRef { height: self.height, node: self.node.as_ptr(), @@ -199,8 +191,7 @@ impl Root { } } - pub fn into_ref(self) - -> NodeRef { + pub fn into_ref(self) -> NodeRef { NodeRef { height: self.height, node: self.node.as_ptr(), @@ -211,8 +202,7 @@ impl Root { /// Adds a new internal node with a single edge, pointing to the previous root, and make that /// new node the root. This increases the height by 1 and is the opposite of `pop_level`. - pub fn push_level(&mut self) - -> NodeRef { + pub fn push_level(&mut self) -> NodeRef { let mut new_node = Box::new(unsafe { InternalNode::new() }); new_node.edges[0] = unsafe { BoxedNode::from_ptr(self.node.as_ptr()) }; @@ -223,7 +213,7 @@ impl Root { height: self.height, node: self.node.as_ptr(), root: self as *mut _, - _marker: PhantomData + _marker: PhantomData, }; unsafe { @@ -253,11 +243,9 @@ impl Root { self.as_mut().as_leaf_mut().parent = ptr::null(); unsafe { - heap::deallocate( - top, - mem::size_of::>(), - mem::align_of::>() - ); + heap::deallocate(top, + mem::size_of::>(), + mem::align_of::>()); } } } @@ -284,39 +272,31 @@ pub struct NodeRef { node: NonZero<*const LeafNode>, // This is null unless the borrow type is `Mut` root: *const Root, - _marker: PhantomData<(BorrowType, Type)> + _marker: PhantomData<(BorrowType, Type)>, } -impl<'a, K: 'a, V: 'a, Type> Copy for NodeRef, K, V, Type> { } +impl<'a, K: 'a, V: 'a, Type> Copy for NodeRef, K, V, Type> {} impl<'a, K: 'a, V: 'a, Type> Clone for NodeRef, K, V, Type> { fn clone(&self) -> Self { *self } } -unsafe impl Sync - for NodeRef { } +unsafe impl Sync for NodeRef {} -unsafe impl<'a, K: Sync + 'a, V: Sync + 'a, Type> Send - for NodeRef, K, V, Type> { } -unsafe impl<'a, K: Send + 'a, V: Send + 'a, Type> Send - for NodeRef, K, V, Type> { } -unsafe impl Send - for NodeRef { } +unsafe impl<'a, K: Sync + 'a, V: Sync + 'a, Type> Send for NodeRef, K, V, Type> {} +unsafe impl<'a, K: Send + 'a, V: Send + 'a, Type> Send for NodeRef, K, V, Type> {} +unsafe impl Send for NodeRef {} impl NodeRef { fn as_internal(&self) -> &InternalNode { - unsafe { - &*(*self.node as *const InternalNode) - } + unsafe { &*(*self.node as *const InternalNode) } } } impl<'a, K, V> NodeRef, K, V, marker::Internal> { fn as_internal_mut(&mut self) -> &mut InternalNode { - unsafe { - &mut *(*self.node as *mut InternalNode) - } + unsafe { &mut *(*self.node as *mut InternalNode) } } } @@ -341,7 +321,7 @@ impl NodeRef { height: self.height, node: self.node, root: self.root, - _marker: PhantomData + _marker: PhantomData, } } @@ -351,14 +331,12 @@ impl NodeRef { height: self.height, node: self.node, root: self.root, - _marker: PhantomData + _marker: PhantomData, } } fn as_leaf(&self) -> &LeafNode { - unsafe { - &**self.node - } + unsafe { &**self.node } } pub fn keys(&self) -> &[K] { @@ -376,31 +354,21 @@ impl NodeRef { /// /// `edge.descend().ascend().unwrap()` and `node.ascend().unwrap().descend()` should /// both, upon success, do nothing. - pub fn ascend(self) -> Result< - Handle< - NodeRef< - BorrowType, - K, V, - marker::Internal - >, - marker::Edge - >, - Self - > { + pub fn ascend + (self) + -> Result, marker::Edge>, Self> { if self.as_leaf().parent.is_null() { Err(self) } else { Ok(Handle { node: NodeRef { height: self.height + 1, - node: unsafe { - NonZero::new(self.as_leaf().parent as *mut LeafNode) - }, + node: unsafe { NonZero::new(self.as_leaf().parent as *mut LeafNode) }, root: self.root, - _marker: PhantomData + _marker: PhantomData, }, idx: self.as_leaf().parent_idx as usize, - _marker: PhantomData + _marker: PhantomData, }) } } @@ -419,19 +387,14 @@ impl NodeRef { /// Similar to `ascend`, gets a reference to a node's parent node, but also /// deallocate the current node in the process. This is unsafe because the /// current node will still be accessible despite being deallocated. - pub unsafe fn deallocate_and_ascend(self) -> Option< - Handle< - NodeRef< - marker::Owned, - K, V, - marker::Internal - >, - marker::Edge - > - > { + pub unsafe fn deallocate_and_ascend + (self) + -> Option, marker::Edge>> { let ptr = self.as_leaf() as *const LeafNode as *const u8 as *mut u8; let ret = self.ascend().ok(); - heap::deallocate(ptr, mem::size_of::>(), mem::align_of::>()); + heap::deallocate(ptr, + mem::size_of::>(), + mem::align_of::>()); ret } } @@ -440,23 +403,14 @@ impl NodeRef { /// Similar to `ascend`, gets a reference to a node's parent node, but also /// deallocate the current node in the process. This is unsafe because the /// current node will still be accessible despite being deallocated. - pub unsafe fn deallocate_and_ascend(self) -> Option< - Handle< - NodeRef< - marker::Owned, - K, V, - marker::Internal - >, - marker::Edge - > - > { + pub unsafe fn deallocate_and_ascend + (self) + -> Option, marker::Edge>> { let ptr = self.as_internal() as *const InternalNode as *const u8 as *mut u8; let ret = self.ascend().ok(); - heap::deallocate( - ptr, - mem::size_of::>(), - mem::align_of::>() - ); + heap::deallocate(ptr, + mem::size_of::>(), + mem::align_of::>()); ret } } @@ -464,14 +418,13 @@ impl NodeRef { impl<'a, K, V, Type> NodeRef, K, V, Type> { /// Unsafely asserts to the compiler some static information about whether this /// node is a `Leaf`. - unsafe fn cast_unchecked(&mut self) - -> NodeRef { + unsafe fn cast_unchecked(&mut self) -> NodeRef { NodeRef { height: self.height, node: self.node, root: self.root, - _marker: PhantomData + _marker: PhantomData, } } @@ -490,14 +443,12 @@ impl<'a, K, V, Type> NodeRef, K, V, Type> { height: self.height, node: self.node, root: self.root, - _marker: PhantomData + _marker: PhantomData, } } fn as_leaf_mut(&mut self) -> &mut LeafNode { - unsafe { - &mut *(*self.node as *mut LeafNode) - } + unsafe { &mut *(*self.node as *mut LeafNode) } } pub fn keys_mut(&mut self) -> &mut [K] { @@ -512,16 +463,8 @@ impl<'a, K, V, Type> NodeRef, K, V, Type> { impl<'a, K: 'a, V: 'a, Type> NodeRef, K, V, Type> { pub fn into_slices(self) -> (&'a [K], &'a [V]) { unsafe { - ( - slice::from_raw_parts( - self.as_leaf().keys.as_ptr(), - self.len() - ), - slice::from_raw_parts( - self.as_leaf().vals.as_ptr(), - self.len() - ) - ) + (slice::from_raw_parts(self.as_leaf().keys.as_ptr(), self.len()), + slice::from_raw_parts(self.as_leaf().vals.as_ptr(), self.len())) } } } @@ -530,23 +473,15 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef, K, V, Type> { /// Gets a mutable reference to the root itself. This is useful primarily when the /// height of the tree needs to be adjusted. Never call this on a reborrowed pointer. pub fn into_root_mut(self) -> &'a mut Root { - unsafe { - &mut *(self.root as *mut Root) - } + unsafe { &mut *(self.root as *mut Root) } } pub fn into_slices_mut(mut self) -> (&'a mut [K], &'a mut [V]) { unsafe { - ( - slice::from_raw_parts_mut( - &mut self.as_leaf_mut().keys as *mut [K] as *mut K, - self.len() - ), - slice::from_raw_parts_mut( - &mut self.as_leaf_mut().vals as *mut [V] as *mut V, - self.len() - ) - ) + (slice::from_raw_parts_mut(&mut self.as_leaf_mut().keys as *mut [K] as *mut K, + self.len()), + slice::from_raw_parts_mut(&mut self.as_leaf_mut().vals as *mut [V] as *mut V, + self.len())) } } } @@ -594,7 +529,8 @@ impl<'a, K, V> NodeRef, K, V, marker::Internal> { unsafe { ptr::write(self.keys_mut().get_unchecked_mut(idx), key); ptr::write(self.vals_mut().get_unchecked_mut(idx), val); - ptr::write(self.as_internal_mut().edges.get_unchecked_mut(idx + 1), edge.node); + ptr::write(self.as_internal_mut().edges.get_unchecked_mut(idx + 1), + edge.node); self.as_leaf_mut().len += 1; @@ -612,18 +548,14 @@ impl<'a, K, V> NodeRef, K, V, marker::Internal> { unsafe { slice_insert(self.keys_mut(), 0, key); slice_insert(self.vals_mut(), 0, val); - slice_insert( - slice::from_raw_parts_mut( - self.as_internal_mut().edges.as_mut_ptr(), - self.len()+1 - ), - 0, - edge.node - ); + slice_insert(slice::from_raw_parts_mut(self.as_internal_mut().edges.as_mut_ptr(), + self.len() + 1), + 0, + edge.node); self.as_leaf_mut().len += 1; - for i in 0..self.len()+1 { + for i in 0..self.len() + 1 { Handle::new_edge(self.reborrow_mut(), i).correct_parent_link(); } } @@ -647,7 +579,10 @@ impl<'a, K, V> NodeRef, K, V, marker::LeafOrInternal> { ForceResult::Leaf(_) => None, ForceResult::Internal(internal) => { let edge = ptr::read(internal.as_internal().edges.get_unchecked(idx + 1)); - let mut new_root = Root { node: edge, height: internal.height - 1 }; + let mut new_root = Root { + node: edge, + height: internal.height - 1, + }; new_root.as_mut().as_leaf_mut().parent = ptr::null(); Some(new_root) } @@ -672,15 +607,16 @@ impl<'a, K, V> NodeRef, K, V, marker::LeafOrInternal> { let edge = match self.reborrow_mut().force() { ForceResult::Leaf(_) => None, ForceResult::Internal(mut internal) => { - let edge = slice_remove( - slice::from_raw_parts_mut( - internal.as_internal_mut().edges.as_mut_ptr(), - old_len+1 - ), - 0 - ); - - let mut new_root = Root { node: edge, height: internal.height - 1 }; + let edge = slice_remove(slice::from_raw_parts_mut(internal.as_internal_mut() + .edges + .as_mut_ptr(), + old_len + 1), + 0); + + let mut new_root = Root { + node: edge, + height: internal.height - 1, + }; new_root.as_mut().as_leaf_mut().parent = ptr::null(); for i in 0..old_len { @@ -700,23 +636,22 @@ impl<'a, K, V> NodeRef, K, V, marker::LeafOrInternal> { impl NodeRef { /// Checks whether a node is an `Internal` node or a `Leaf` node. - pub fn force(self) -> ForceResult< - NodeRef, - NodeRef - > { + pub fn force(self) + -> ForceResult, + NodeRef> { if self.height == 0 { ForceResult::Leaf(NodeRef { height: self.height, node: self.node, root: self.root, - _marker: PhantomData + _marker: PhantomData, }) } else { ForceResult::Internal(NodeRef { height: self.height, node: self.node, root: self.root, - _marker: PhantomData + _marker: PhantomData, }) } } @@ -733,10 +668,10 @@ impl NodeRef { pub struct Handle { node: Node, idx: usize, - _marker: PhantomData + _marker: PhantomData, } -impl Copy for Handle { } +impl Copy for Handle {} // We don't need the full generality of `#[derive(Clone)]`, as the only time `Node` will be // `Clone`able is when it is an immutable reference and therefore `Copy`. impl Clone for Handle { @@ -761,7 +696,7 @@ impl Handle, mar Handle { node: node, idx: idx, - _marker: PhantomData + _marker: PhantomData, } } @@ -774,33 +709,30 @@ impl Handle, mar } } -impl PartialEq - for Handle, HandleType> { - +impl PartialEq for Handle, + HandleType> { fn eq(&self, other: &Self) -> bool { self.node.node == other.node.node && self.idx == other.idx } } -impl - Handle, HandleType> { - +impl Handle, HandleType> { /// Temporarily takes out another, immutable handle on the same location. - pub fn reborrow(&self) - -> Handle, HandleType> { + pub fn reborrow(&self) -> Handle, HandleType> { // We can't use Handle::new_kv or Handle::new_edge because we don't know our type Handle { node: self.node.reborrow(), idx: self.idx, - _marker: PhantomData + _marker: PhantomData, } } } -impl<'a, K, V, NodeType, HandleType> - Handle, K, V, NodeType>, HandleType> { - +impl<'a, K, V, NodeType, HandleType> Handle, K, V, NodeType>, HandleType> { /// Temporarily takes out another, mutable handle on the same location. Beware, as /// this method is very dangerous, doubly so since it may not immediately appear /// dangerous. @@ -811,21 +743,18 @@ impl<'a, K, V, NodeType, HandleType> /// of a reborrowed handle, out of bounds. // FIXME(@gereeter) consider adding yet another type parameter to `NodeRef` that restricts // the use of `ascend` and `into_root_mut` on reborrowed pointers, preventing this unsafety. - pub unsafe fn reborrow_mut(&mut self) - -> Handle, HandleType> { + pub unsafe fn reborrow_mut(&mut self) -> Handle, HandleType> { // We can't use Handle::new_kv or Handle::new_edge because we don't know our type Handle { node: self.node.reborrow_mut(), idx: self.idx, - _marker: PhantomData + _marker: PhantomData, } } } -impl - Handle, marker::Edge> { - +impl Handle, marker::Edge> { /// Creates a new handle to an edge in `node`. `idx` must be less than or equal to /// `node.len()`. pub fn new_edge(node: NodeRef, idx: usize) -> Self { @@ -835,12 +764,11 @@ impl Handle { node: node, idx: idx, - _marker: PhantomData + _marker: PhantomData, } } - pub fn left_kv(self) - -> Result, marker::KV>, Self> { + pub fn left_kv(self) -> Result, marker::KV>, Self> { if self.idx > 0 { Ok(Handle::new_kv(self.node, self.idx - 1)) @@ -849,8 +777,7 @@ impl } } - pub fn right_kv(self) - -> Result, marker::KV>, Self> { + pub fn right_kv(self) -> Result, marker::KV>, Self> { if self.idx < self.node.len() { Ok(Handle::new_kv(self.node, self.idx)) @@ -884,8 +811,7 @@ impl<'a, K, V> Handle, K, V, marker::Leaf>, marker::Edge /// this edge. This method splits the node if there isn't enough room. /// /// The returned pointer points to the inserted value. - pub fn insert(mut self, key: K, val: V) - -> (InsertResult<'a, K, V, marker::Leaf>, *mut V) { + pub fn insert(mut self, key: K, val: V) -> (InsertResult<'a, K, V, marker::Leaf>, *mut V) { if self.node.len() < CAPACITY { let ptr = self.insert_fit(key, val); @@ -894,15 +820,12 @@ impl<'a, K, V> Handle, K, V, marker::Leaf>, marker::Edge let middle = Handle::new_kv(self.node, B); let (mut left, k, v, mut right) = middle.split(); let ptr = if self.idx <= B { - unsafe { - Handle::new_edge(left.reborrow_mut(), self.idx).insert_fit(key, val) - } + unsafe { Handle::new_edge(left.reborrow_mut(), self.idx).insert_fit(key, val) } } else { unsafe { - Handle::new_edge( - right.as_mut().cast_unchecked::(), - self.idx - (B + 1) - ).insert_fit(key, val) + Handle::new_edge(right.as_mut().cast_unchecked::(), + self.idx - (B + 1)) + .insert_fit(key, val) } }; (InsertResult::Split(left, k, v, right), ptr) @@ -923,8 +846,9 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: /// Unsafely asserts to the compiler some static information about whether the underlying /// node of this handle is a `Leaf`. - unsafe fn cast_unchecked(&mut self) - -> Handle, marker::Edge> { + unsafe fn cast_unchecked + (&mut self) + -> Handle, marker::Edge> { Handle::new_edge(self.node.cast_unchecked(), self.idx) } @@ -941,16 +865,12 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: // This cast is a lie, but it allows us to reuse the key/value insertion logic. self.cast_unchecked::().insert_fit(key, val); - slice_insert( - slice::from_raw_parts_mut( - self.node.as_internal_mut().edges.as_mut_ptr(), - self.node.len() - ), - self.idx + 1, - edge.node - ); + slice_insert(slice::from_raw_parts_mut(self.node.as_internal_mut().edges.as_mut_ptr(), + self.node.len()), + self.idx + 1, + edge.node); - for i in (self.idx+1)..(self.node.len()+1) { + for i in (self.idx + 1)..(self.node.len() + 1) { Handle::new_edge(self.node.reborrow_mut(), i).correct_parent_link(); } } @@ -959,8 +879,11 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: /// Inserts a new key/value pair and an edge that will go to the right of that new pair /// between this edge and the key/value pair to the right of this edge. This method splits /// the node if there isn't enough room. - pub fn insert(mut self, key: K, val: V, edge: Root) - -> InsertResult<'a, K, V, marker::Internal> { + pub fn insert(mut self, + key: K, + val: V, + edge: Root) + -> InsertResult<'a, K, V, marker::Internal> { // Necessary for correctness, but this is an internal module debug_assert!(edge.height == self.node.height - 1); @@ -977,10 +900,9 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: } } else { unsafe { - Handle::new_edge( - right.as_mut().cast_unchecked::(), - self.idx - (B + 1) - ).insert_fit(key, val, edge); + Handle::new_edge(right.as_mut().cast_unchecked::(), + self.idx - (B + 1)) + .insert_fit(key, val, edge); } } InsertResult::Split(left, k, v, right) @@ -988,9 +910,7 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: } } -impl - Handle, marker::Edge> { - +impl Handle, marker::Edge> { /// Finds the node pointed to by this edge. /// /// `edge.descend().ascend().unwrap()` and `node.ascend().unwrap().descend()` should @@ -1000,30 +920,22 @@ impl height: self.node.height - 1, node: unsafe { self.node.as_internal().edges.get_unchecked(self.idx).as_ptr() }, root: self.node.root, - _marker: PhantomData + _marker: PhantomData, } } } -impl<'a, K: 'a, V: 'a, NodeType> - Handle, K, V, NodeType>, marker::KV> { - +impl<'a, K: 'a, V: 'a, NodeType> Handle, K, V, NodeType>, marker::KV> { pub fn into_kv(self) -> (&'a K, &'a V) { let (keys, vals) = self.node.into_slices(); - unsafe { - (keys.get_unchecked(self.idx), vals.get_unchecked(self.idx)) - } + unsafe { (keys.get_unchecked(self.idx), vals.get_unchecked(self.idx)) } } } -impl<'a, K: 'a, V: 'a, NodeType> - Handle, K, V, NodeType>, marker::KV> { - +impl<'a, K: 'a, V: 'a, NodeType> Handle, K, V, NodeType>, marker::KV> { pub fn into_kv_mut(self) -> (&'a mut K, &'a mut V) { let (mut keys, mut vals) = self.node.into_slices_mut(); - unsafe { - (keys.get_unchecked_mut(self.idx), vals.get_unchecked_mut(self.idx)) - } + unsafe { (keys.get_unchecked_mut(self.idx), vals.get_unchecked_mut(self.idx)) } } } @@ -1044,8 +956,7 @@ impl<'a, K, V> Handle, K, V, marker::Leaf>, marker::KV> /// - The key and value pointed to by this handle and extracted. /// - All the key/value pairs to the right of this handle are put into a newly /// allocated node. - pub fn split(mut self) - -> (NodeRef, K, V, marker::Leaf>, K, V, Root) { + pub fn split(mut self) -> (NodeRef, K, V, marker::Leaf>, K, V, Root) { unsafe { let mut new_node = Box::new(LeafNode::new()); @@ -1054,35 +965,30 @@ impl<'a, K, V> Handle, K, V, marker::Leaf>, marker::KV> let new_len = self.node.len() - self.idx - 1; - ptr::copy_nonoverlapping( - self.node.keys().as_ptr().offset(self.idx as isize + 1), - new_node.keys.as_mut_ptr(), - new_len - ); - ptr::copy_nonoverlapping( - self.node.vals().as_ptr().offset(self.idx as isize + 1), - new_node.vals.as_mut_ptr(), - new_len - ); + ptr::copy_nonoverlapping(self.node.keys().as_ptr().offset(self.idx as isize + 1), + new_node.keys.as_mut_ptr(), + new_len); + ptr::copy_nonoverlapping(self.node.vals().as_ptr().offset(self.idx as isize + 1), + new_node.vals.as_mut_ptr(), + new_len); self.node.as_leaf_mut().len = self.idx as u16; new_node.len = new_len as u16; - ( - self.node, - k, v, - Root { - node: BoxedNode::from_leaf(new_node), - height: 0 - } - ) + (self.node, + k, + v, + Root { + node: BoxedNode::from_leaf(new_node), + height: 0, + }) } } /// Removes the key/value pair pointed to by this handle, returning the edge between the /// now adjacent key/value pairs to the left and right of this handle. pub fn remove(mut self) - -> (Handle, K, V, marker::Leaf>, marker::Edge>, K, V) { + -> (Handle, K, V, marker::Leaf>, marker::Edge>, K, V) { unsafe { let k = slice_remove(self.node.keys_mut(), self.idx); let v = slice_remove(self.node.vals_mut(), self.idx); @@ -1100,8 +1006,7 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: /// - The key and value pointed to by this handle and extracted. /// - All the edges and key/value pairs to the right of this handle are put into /// a newly allocated node. - pub fn split(mut self) - -> (NodeRef, K, V, marker::Internal>, K, V, Root) { + pub fn split(mut self) -> (NodeRef, K, V, marker::Internal>, K, V, Root) { unsafe { let mut new_node = Box::new(InternalNode::new()); @@ -1111,39 +1016,33 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: let height = self.node.height; let new_len = self.node.len() - self.idx - 1; - ptr::copy_nonoverlapping( - self.node.keys().as_ptr().offset(self.idx as isize + 1), - new_node.data.keys.as_mut_ptr(), - new_len - ); - ptr::copy_nonoverlapping( - self.node.vals().as_ptr().offset(self.idx as isize + 1), - new_node.data.vals.as_mut_ptr(), - new_len - ); - ptr::copy_nonoverlapping( - self.node.as_internal().edges.as_ptr().offset(self.idx as isize + 1), - new_node.edges.as_mut_ptr(), - new_len + 1 - ); + ptr::copy_nonoverlapping(self.node.keys().as_ptr().offset(self.idx as isize + 1), + new_node.data.keys.as_mut_ptr(), + new_len); + ptr::copy_nonoverlapping(self.node.vals().as_ptr().offset(self.idx as isize + 1), + new_node.data.vals.as_mut_ptr(), + new_len); + ptr::copy_nonoverlapping(self.node + .as_internal() + .edges + .as_ptr() + .offset(self.idx as isize + 1), + new_node.edges.as_mut_ptr(), + new_len + 1); self.node.as_leaf_mut().len = self.idx as u16; new_node.data.len = new_len as u16; let mut new_root = Root { node: BoxedNode::from_internal(new_node), - height: height + height: height, }; - for i in 0..(new_len+1) { + for i in 0..(new_len + 1) { Handle::new_edge(new_root.as_mut().cast_unchecked(), i).correct_parent_link(); } - ( - self.node, - k, v, - new_root - ) + (self.node, k, v, new_root) } } @@ -1151,17 +1050,14 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: /// a node to hold the combination of the nodes to the left and right of this handle along /// with the key/value pair at this handle. pub fn can_merge(&self) -> bool { - ( - self.reborrow() - .left_edge() - .descend() - .len() - + self.reborrow() - .right_edge() - .descend() - .len() - + 1 - ) <= CAPACITY + (self.reborrow() + .left_edge() + .descend() + .len() + + self.reborrow() + .right_edge() + .descend() + .len() + 1) <= CAPACITY } /// Combines the node immediately to the left of this handle, the key/value pair pointed @@ -1169,8 +1065,7 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: /// child of the underlying node, returning an edge referencing that new child. /// /// Assumes that this edge `.can_merge()`. - pub fn merge(mut self) - -> Handle, K, V, marker::Internal>, marker::Edge> { + pub fn merge(mut self) -> Handle, K, V, marker::Internal>, marker::Edge> { let self1 = unsafe { ptr::read(&self) }; let self2 = unsafe { ptr::read(&self) }; let mut left_node = self1.left_edge().descend(); @@ -1184,21 +1079,21 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: unsafe { ptr::write(left_node.keys_mut().get_unchecked_mut(left_len), slice_remove(self.node.keys_mut(), self.idx)); - ptr::copy_nonoverlapping( - right_node.keys().as_ptr(), - left_node.keys_mut().as_mut_ptr().offset(left_len as isize + 1), - right_len - ); + ptr::copy_nonoverlapping(right_node.keys().as_ptr(), + left_node.keys_mut() + .as_mut_ptr() + .offset(left_len as isize + 1), + right_len); ptr::write(left_node.vals_mut().get_unchecked_mut(left_len), slice_remove(self.node.vals_mut(), self.idx)); - ptr::copy_nonoverlapping( - right_node.vals().as_ptr(), - left_node.vals_mut().as_mut_ptr().offset(left_len as isize + 1), - right_len - ); + ptr::copy_nonoverlapping(right_node.vals().as_ptr(), + left_node.vals_mut() + .as_mut_ptr() + .offset(left_len as isize + 1), + right_len); slice_remove(&mut self.node.as_internal_mut().edges, self.idx + 1); - for i in self.idx+1..self.node.len() { + for i in self.idx + 1..self.node.len() { Handle::new_edge(self.node.reborrow_mut(), i).correct_parent_link(); } self.node.as_leaf_mut().len -= 1; @@ -1206,34 +1101,26 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: left_node.as_leaf_mut().len += right_len as u16 + 1; if self.node.height > 1 { - ptr::copy_nonoverlapping( - right_node.cast_unchecked().as_internal().edges.as_ptr(), - left_node.cast_unchecked() - .as_internal_mut() - .edges - .as_mut_ptr() - .offset(left_len as isize + 1), - right_len + 1 - ); - - for i in left_len+1..left_len+right_len+2 { - Handle::new_edge( - left_node.cast_unchecked().reborrow_mut(), - i - ).correct_parent_link(); + ptr::copy_nonoverlapping(right_node.cast_unchecked().as_internal().edges.as_ptr(), + left_node.cast_unchecked() + .as_internal_mut() + .edges + .as_mut_ptr() + .offset(left_len as isize + 1), + right_len + 1); + + for i in left_len + 1..left_len + right_len + 2 { + Handle::new_edge(left_node.cast_unchecked().reborrow_mut(), i) + .correct_parent_link(); } - heap::deallocate( - *right_node.node as *mut u8, - mem::size_of::>(), - mem::align_of::>() - ); + heap::deallocate(*right_node.node as *mut u8, + mem::size_of::>(), + mem::align_of::>()); } else { - heap::deallocate( - *right_node.node as *mut u8, - mem::size_of::>(), - mem::align_of::>() - ); + heap::deallocate(*right_node.node as *mut u8, + mem::size_of::>(), + mem::align_of::>()); } Handle::new_edge(self.node, self.idx) @@ -1252,7 +1139,7 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: match self.reborrow_mut().right_edge().descend().force() { ForceResult::Leaf(mut leaf) => leaf.push_front(k, v), - ForceResult::Internal(mut internal) => internal.push_front(k, v, edge.unwrap()) + ForceResult::Internal(mut internal) => internal.push_front(k, v, edge.unwrap()), } } } @@ -1269,7 +1156,7 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: match self.reborrow_mut().left_edge().descend().force() { ForceResult::Leaf(mut leaf) => leaf.push(k, v), - ForceResult::Internal(mut internal) => internal.push(k, v, edge.unwrap()) + ForceResult::Internal(mut internal) => internal.push(k, v, edge.unwrap()), } } } @@ -1285,9 +1172,9 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: left.keys_mut().as_mut_ptr(), left.vals_mut().as_mut_ptr(), match left.force() { - ForceResult::Leaf(_) => None, - ForceResult::Internal(mut i) => Some(i.as_internal_mut().edges.as_mut_ptr()), - }) + ForceResult::Leaf(_) => None, + ForceResult::Internal(mut i) => Some(i.as_internal_mut().edges.as_mut_ptr()), + }) }; // Get raw pointers to right child's keys, values and edges. @@ -1298,9 +1185,9 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: right.keys_mut().as_mut_ptr(), right.vals_mut().as_mut_ptr(), match right.force() { - ForceResult::Leaf(_) => None, - ForceResult::Internal(mut i) => Some(i.as_internal_mut().edges.as_mut_ptr()), - }) + ForceResult::Leaf(_) => None, + ForceResult::Internal(mut i) => Some(i.as_internal_mut().edges.as_mut_ptr()), + }) }; // Get raw pointers to parent's key and value. @@ -1314,51 +1201,31 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: debug_assert!(left_len >= n); // Make room for stolen elements in right child. - ptr::copy(right_k, - right_k.offset(n as isize), - right_len); - ptr::copy(right_v, - right_v.offset(n as isize), - right_len); + ptr::copy(right_k, right_k.offset(n as isize), right_len); + ptr::copy(right_v, right_v.offset(n as isize), right_len); if let Some(edges) = right_e { - ptr::copy(edges, - edges.offset(n as isize), - right_len+1); + ptr::copy(edges, edges.offset(n as isize), right_len + 1); } // Move elements from the left child to the right one. let left_ind = (left_len - n) as isize; - ptr::copy_nonoverlapping(left_k.offset(left_ind + 1), - right_k, - n - 1); - ptr::copy_nonoverlapping(left_v.offset(left_ind + 1), - right_v, - n - 1); + ptr::copy_nonoverlapping(left_k.offset(left_ind + 1), right_k, n - 1); + ptr::copy_nonoverlapping(left_v.offset(left_ind + 1), right_v, n - 1); match (left_e, right_e) { (Some(left), Some(right)) => { - ptr::copy_nonoverlapping(left.offset(left_ind + 1), - right, - n); - }, + ptr::copy_nonoverlapping(left.offset(left_ind + 1), right, n); + } (Some(_), None) => unreachable!(), (None, Some(_)) => unreachable!(), - (None, None) => {}, + (None, None) => {} } // Copy parent key/value pair to right child. - ptr::copy_nonoverlapping(parent_k, - right_k.offset(n as isize - 1), - 1); - ptr::copy_nonoverlapping(parent_v, - right_v.offset(n as isize - 1), - 1); + ptr::copy_nonoverlapping(parent_k, right_k.offset(n as isize - 1), 1); + ptr::copy_nonoverlapping(parent_v, right_v.offset(n as isize - 1), 1); // Copy left-most stolen pair to parent. - ptr::copy_nonoverlapping(left_k.offset(left_ind), - parent_k, - 1); - ptr::copy_nonoverlapping(left_v.offset(left_ind), - parent_v, - 1); + ptr::copy_nonoverlapping(left_k.offset(left_ind), parent_k, 1); + ptr::copy_nonoverlapping(left_v.offset(left_ind), parent_v, 1); // Fix lengths of left and right child and parent pointers in children of the right // child. @@ -1366,7 +1233,7 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: let mut right = self.reborrow_mut().right_edge().descend(); right.as_leaf_mut().len += n as u16; if let ForceResult::Internal(mut node) = right.force() { - for i in 0..(right_len+n+1) { + for i in 0..(right_len + n + 1) { Handle::new_edge(node.reborrow_mut(), i as usize).correct_parent_link(); } } @@ -1374,37 +1241,39 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: } } -impl - Handle, HandleType> { - +impl Handle, + HandleType> { /// Check whether the underlying node is an `Internal` node or a `Leaf` node. - pub fn force(self) -> ForceResult< - Handle, HandleType>, - Handle, HandleType> - > { + pub fn force(self) + -> ForceResult, HandleType>, + Handle, HandleType>> { match self.node.force() { - ForceResult::Leaf(node) => ForceResult::Leaf(Handle { - node: node, - idx: self.idx, - _marker: PhantomData - }), - ForceResult::Internal(node) => ForceResult::Internal(Handle { - node: node, - idx: self.idx, - _marker: PhantomData - }) + ForceResult::Leaf(node) => { + ForceResult::Leaf(Handle { + node: node, + idx: self.idx, + _marker: PhantomData, + }) + } + ForceResult::Internal(node) => { + ForceResult::Internal(Handle { + node: node, + idx: self.idx, + _marker: PhantomData, + }) + } } } } pub enum ForceResult { Leaf(Leaf), - Internal(Internal) + Internal(Internal), } pub enum InsertResult<'a, K, V, Type> { Fit(Handle, K, V, Type>, marker::KV>), - Split(NodeRef, K, V, Type>, K, V, Root) + Split(NodeRef, K, V, Type>, K, V, Root), } pub mod marker { @@ -1423,20 +1292,16 @@ pub mod marker { } unsafe fn slice_insert(slice: &mut [T], idx: usize, val: T) { - ptr::copy( - slice.as_ptr().offset(idx as isize), - slice.as_mut_ptr().offset(idx as isize + 1), - slice.len() - idx - ); + ptr::copy(slice.as_ptr().offset(idx as isize), + slice.as_mut_ptr().offset(idx as isize + 1), + slice.len() - idx); ptr::write(slice.get_unchecked_mut(idx), val); } unsafe fn slice_remove(slice: &mut [T], idx: usize) -> T { let ret = ptr::read(slice.get_unchecked(idx)); - ptr::copy( - slice.as_ptr().offset(idx as isize + 1), - slice.as_mut_ptr().offset(idx as isize), - slice.len() - idx - 1 - ); + ptr::copy(slice.as_ptr().offset(idx as isize + 1), + slice.as_mut_ptr().offset(idx as isize), + slice.len() - idx - 1); ret } diff --git a/src/libcollections/btree/search.rs b/src/libcollections/btree/search.rs index c94b570bfed8b..a04fec4a30e2e 100644 --- a/src/libcollections/btree/search.rs +++ b/src/libcollections/btree/search.rs @@ -19,58 +19,60 @@ use self::SearchResult::*; pub enum SearchResult { Found(Handle, marker::KV>), - GoDown(Handle, marker::Edge>) + GoDown(Handle, marker::Edge>), } -pub fn search_tree( - mut node: NodeRef, - key: &Q -) -> SearchResult - where Q: Ord, K: Borrow { +pub fn search_tree + (mut node: NodeRef, + key: &Q) + -> SearchResult + where Q: Ord, + K: Borrow +{ loop { match search_node(node, key) { Found(handle) => return Found(handle), - GoDown(handle) => match handle.force() { - Leaf(leaf) => return GoDown(leaf), - Internal(internal) => { - node = internal.descend(); - continue; + GoDown(handle) => { + match handle.force() { + Leaf(leaf) => return GoDown(leaf), + Internal(internal) => { + node = internal.descend(); + continue; + } } } } } } -pub fn search_node( - node: NodeRef, - key: &Q -) -> SearchResult - where Q: Ord, K: Borrow { +pub fn search_node + (node: NodeRef, + key: &Q) + -> SearchResult + where Q: Ord, + K: Borrow +{ match search_linear(&node, key) { - (idx, true) => Found( - Handle::new_kv(node, idx) - ), - (idx, false) => SearchResult::GoDown( - Handle::new_edge(node, idx) - ) + (idx, true) => Found(Handle::new_kv(node, idx)), + (idx, false) => SearchResult::GoDown(Handle::new_edge(node, idx)), } } -fn search_linear( - node: &NodeRef, - key: &Q -) -> (usize, bool) - where Q: Ord, K: Borrow { +fn search_linear(node: &NodeRef, + key: &Q) + -> (usize, bool) + where Q: Ord, + K: Borrow +{ for (i, k) in node.keys().iter().enumerate() { match key.cmp(k.borrow()) { - Ordering::Greater => {}, + Ordering::Greater => {} Ordering::Equal => return (i, true), - Ordering::Less => return (i, false) + Ordering::Less => return (i, false), } } (node.keys().len(), false) } - diff --git a/src/libcollections/btree/set.rs b/src/libcollections/btree/set.rs index 3ee42499a38f8..90c725d9cdf8f 100644 --- a/src/libcollections/btree/set.rs +++ b/src/libcollections/btree/set.rs @@ -11,12 +11,12 @@ // This is pretty much entirely stolen from TreeSet, since BTreeMap has an identical interface // to TreeMap -use core::cmp::Ordering::{self, Less, Greater, Equal}; -use core::cmp::{min, max}; +use core::cmp::Ordering::{self, Equal, Greater, Less}; +use core::cmp::{max, min}; use core::fmt::Debug; use core::fmt; -use core::iter::{Peekable, FromIterator}; -use core::ops::{BitOr, BitAnd, BitXor, Sub}; +use core::iter::{FromIterator, Peekable}; +use core::ops::{BitAnd, BitOr, BitXor, Sub}; use borrow::Borrow; use btree_map::{BTreeMap, Keys}; @@ -770,7 +770,9 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> { } #[stable(feature = "rust1", since = "1.0.0")] impl<'a, T> ExactSizeIterator for Iter<'a, T> { - fn len(&self) -> usize { self.iter.len() } + fn len(&self) -> usize { + self.iter.len() + } } @@ -793,7 +795,9 @@ impl DoubleEndedIterator for IntoIter { } #[stable(feature = "rust1", since = "1.0.0")] impl ExactSizeIterator for IntoIter { - fn len(&self) -> usize { self.iter.len() } + fn len(&self) -> usize { + self.iter.len() + } } diff --git a/src/libcollections/enum_set.rs b/src/libcollections/enum_set.rs index 0c66c0564c3ea..b893c271bf603 100644 --- a/src/libcollections/enum_set.rs +++ b/src/libcollections/enum_set.rs @@ -21,7 +21,7 @@ use core::marker; use core::fmt; use core::iter::FromIterator; -use core::ops::{Sub, BitOr, BitAnd, BitXor}; +use core::ops::{BitAnd, BitOr, BitXor, Sub}; // FIXME(contentions): implement union family of methods? (general design may be // wrong here) @@ -275,7 +275,8 @@ impl FromIterator for EnumSet { } #[stable(feature = "rust1", since = "1.0.0")] -impl<'a, E> IntoIterator for &'a EnumSet where E: CLike +impl<'a, E> IntoIterator for &'a EnumSet + where E: CLike { type Item = E; type IntoIter = Iter; diff --git a/src/libcollections/fmt.rs b/src/libcollections/fmt.rs index 0ebb89b8a22a3..2930ac6a14bb2 100644 --- a/src/libcollections/fmt.rs +++ b/src/libcollections/fmt.rs @@ -494,11 +494,11 @@ pub use core::fmt::rt; #[stable(feature = "rust1", since = "1.0.0")] pub use core::fmt::{Formatter, Result, Write}; #[stable(feature = "rust1", since = "1.0.0")] -pub use core::fmt::{Octal, Binary}; +pub use core::fmt::{Binary, Octal}; #[stable(feature = "rust1", since = "1.0.0")] -pub use core::fmt::{Display, Debug}; +pub use core::fmt::{Debug, Display}; #[stable(feature = "rust1", since = "1.0.0")] -pub use core::fmt::{LowerHex, UpperHex, Pointer}; +pub use core::fmt::{LowerHex, Pointer, UpperHex}; #[stable(feature = "rust1", since = "1.0.0")] pub use core::fmt::{LowerExp, UpperExp}; #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libcollections/linked_list.rs b/src/libcollections/linked_list.rs index 406b979a37087..ac07ba42c075f 100644 --- a/src/libcollections/linked_list.rs +++ b/src/libcollections/linked_list.rs @@ -24,7 +24,7 @@ use alloc::boxed::{Box, IntermediateBox}; use core::cmp::Ordering; use core::fmt; -use core::hash::{Hasher, Hash}; +use core::hash::{Hash, Hasher}; use core::iter::FromIterator; use core::mem; use core::ops::{BoxPlace, InPlace, Place, Placer}; @@ -696,7 +696,10 @@ impl LinkedList { reason = "method name and placement protocol are subject to change", issue = "30172")] pub fn front_place(&mut self) -> FrontPlace { - FrontPlace { list: self, node: IntermediateBox::make_place() } + FrontPlace { + list: self, + node: IntermediateBox::make_place(), + } } /// Returns a place for insertion at the back of the list. @@ -721,7 +724,10 @@ impl LinkedList { reason = "method name and placement protocol are subject to change", issue = "30172")] pub fn back_place(&mut self) -> BackPlace { - BackPlace { list: self, node: IntermediateBox::make_place() } + BackPlace { + list: self, + node: IntermediateBox::make_place(), + } } } @@ -986,7 +992,7 @@ impl Extend for LinkedList { } impl SpecExtend for LinkedList { - default fn spec_extend(&mut self, iter: I) { + fn spec_extend(&mut self, iter: I) { for elt in iter { self.push_back(elt); } @@ -1157,17 +1163,23 @@ impl<'a, T> InPlace for BackPlace<'a, T> { // Ensure that `LinkedList` and its read-only iterators are covariant in their type parameters. #[allow(dead_code)] fn assert_covariance() { - fn a<'a>(x: LinkedList<&'static str>) -> LinkedList<&'a str> { x } - fn b<'i, 'a>(x: Iter<'i, &'static str>) -> Iter<'i, &'a str> { x } - fn c<'a>(x: IntoIter<&'static str>) -> IntoIter<&'a str> { x } + fn a<'a>(x: LinkedList<&'static str>) -> LinkedList<&'a str> { + x + } + fn b<'i, 'a>(x: Iter<'i, &'static str>) -> Iter<'i, &'a str> { + x + } + fn c<'a>(x: IntoIter<&'static str>) -> IntoIter<&'a str> { + x + } } #[cfg(test)] mod tests { use std::clone::Clone; - use std::iter::{Iterator, IntoIterator, Extend}; - use std::option::Option::{self, Some, None}; - use std::__rand::{thread_rng, Rng}; + use std::iter::{Extend, IntoIterator, Iterator}; + use std::option::Option::{self, None, Some}; + use std::__rand::{Rng, thread_rng}; use std::thread; use std::vec::Vec; diff --git a/src/libcollections/range.rs b/src/libcollections/range.rs index 4e39191b472ee..ed8932bebe224 100644 --- a/src/libcollections/range.rs +++ b/src/libcollections/range.rs @@ -15,7 +15,7 @@ //! Range syntax. use core::option::Option::{self, None, Some}; -use core::ops::{RangeFull, Range, RangeTo, RangeFrom}; +use core::ops::{Range, RangeFrom, RangeFull, RangeTo}; /// **RangeArgument** is implemented by Rust's built-in range types, produced /// by range syntax like `..`, `a..`, `..b` or `c..d`. diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs index f3770816cb6b3..c2b79a77a2563 100644 --- a/src/libcollections/str.rs +++ b/src/libcollections/str.rs @@ -21,7 +21,7 @@ use core::str as core_str; use core::str::pattern::Pattern; -use core::str::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher}; +use core::str::pattern::{DoubleEndedSearcher, ReverseSearcher, Searcher}; use core::mem; use rustc_unicode::str::{UnicodeStr, Utf16Encoder}; @@ -37,21 +37,21 @@ use boxed::Box; pub use core::str::{FromStr, Utf8Error}; #[allow(deprecated)] #[stable(feature = "rust1", since = "1.0.0")] -pub use core::str::{Lines, LinesAny, CharRange}; +pub use core::str::{CharRange, Lines, LinesAny}; #[stable(feature = "rust1", since = "1.0.0")] -pub use core::str::{Split, RSplit}; +pub use core::str::{RSplit, Split}; #[stable(feature = "rust1", since = "1.0.0")] -pub use core::str::{SplitN, RSplitN}; +pub use core::str::{RSplitN, SplitN}; #[stable(feature = "rust1", since = "1.0.0")] -pub use core::str::{SplitTerminator, RSplitTerminator}; +pub use core::str::{RSplitTerminator, SplitTerminator}; #[stable(feature = "rust1", since = "1.0.0")] pub use core::str::{Matches, RMatches}; #[stable(feature = "rust1", since = "1.0.0")] pub use core::str::{MatchIndices, RMatchIndices}; #[stable(feature = "rust1", since = "1.0.0")] -pub use core::str::{from_utf8, Chars, CharIndices, Bytes}; +pub use core::str::{Bytes, CharIndices, Chars, from_utf8}; #[stable(feature = "rust1", since = "1.0.0")] -pub use core::str::{from_utf8_unchecked, ParseBoolError}; +pub use core::str::{ParseBoolError, from_utf8_unchecked}; #[stable(feature = "rust1", since = "1.0.0")] pub use rustc_unicode::str::SplitWhitespace; #[stable(feature = "rust1", since = "1.0.0")] @@ -1897,7 +1897,7 @@ impl str { } fn case_ignoreable_then_cased>(iter: I) -> bool { - use rustc_unicode::derived_property::{Cased, Case_Ignorable}; + use rustc_unicode::derived_property::{Case_Ignorable, Cased}; match iter.skip_while(|&c| Case_Ignorable(c)).next() { Some(c) => Cased(c), None => false, diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index eedf4c2c11f34..97a008dfb45ed 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -62,12 +62,12 @@ use core::mem; use core::ops::{self, Add, Index, IndexMut}; use core::ptr; use core::str::pattern::Pattern; -use rustc_unicode::char::{decode_utf16, REPLACEMENT_CHARACTER}; +use rustc_unicode::char::{REPLACEMENT_CHARACTER, decode_utf16}; use rustc_unicode::str as unicode_str; use borrow::{Cow, ToOwned}; use range::RangeArgument; -use str::{self, FromStr, Utf8Error, Chars}; +use str::{self, Chars, FromStr, Utf8Error}; use vec::Vec; use boxed::Box; diff --git a/src/libcollections/vec_deque.rs b/src/libcollections/vec_deque.rs index 84a0bbbd24983..0183a40992183 100644 --- a/src/libcollections/vec_deque.rs +++ b/src/libcollections/vec_deque.rs @@ -20,7 +20,7 @@ use core::cmp::Ordering; use core::fmt; -use core::iter::{repeat, FromIterator}; +use core::iter::{FromIterator, repeat}; use core::mem; use core::ops::{Index, IndexMut}; use core::ptr; @@ -1231,9 +1231,7 @@ impl VecDeque { let contiguous = self.is_contiguous(); - match (contiguous, - distance_to_tail <= distance_to_head, - idx >= self.tail) { + match (contiguous, distance_to_tail <= distance_to_head, idx >= self.tail) { (true, true, _) if index == 0 => { // push_front // @@ -1448,9 +1446,7 @@ impl VecDeque { let contiguous = self.is_contiguous(); - match (contiguous, - distance_to_tail <= distance_to_head, - idx >= self.tail) { + match (contiguous, distance_to_tail <= distance_to_head, idx >= self.tail) { (true, true, _) => { unsafe { // contiguous, remove closer to tail: @@ -1941,17 +1937,15 @@ impl<'a, T: 'a> Drop for Drain<'a, T> { (_, 0) => { source_deque.head = drain_tail; } - _ => { - unsafe { - if tail_len <= head_len { - source_deque.tail = source_deque.wrap_sub(drain_head, tail_len); - source_deque.wrap_copy(source_deque.tail, orig_tail, tail_len); - } else { - source_deque.head = source_deque.wrap_add(drain_tail, head_len); - source_deque.wrap_copy(drain_tail, drain_head, head_len); - } + _ => unsafe { + if tail_len <= head_len { + source_deque.tail = source_deque.wrap_sub(drain_head, tail_len); + source_deque.wrap_copy(source_deque.tail, orig_tail, tail_len); + } else { + source_deque.head = source_deque.wrap_add(drain_tail, head_len); + source_deque.wrap_copy(drain_tail, drain_head, head_len); } - } + }, } } } @@ -2144,10 +2138,8 @@ impl From> for VecDeque { // We need to extend the buf if it's not a power of two, too small // or doesn't have at least one free space - if !buf.cap().is_power_of_two() - || (buf.cap() < (MINIMUM_CAPACITY + 1)) - || (buf.cap() == len) - { + if !buf.cap().is_power_of_two() || (buf.cap() < (MINIMUM_CAPACITY + 1)) || + (buf.cap() == len) { let cap = cmp::max(buf.cap() + 1, MINIMUM_CAPACITY + 1).next_power_of_two(); buf.reserve_exact(len, cap - len); } @@ -2155,7 +2147,7 @@ impl From> for VecDeque { VecDeque { tail: 0, head: len, - buf: buf + buf: buf, } } } @@ -2180,18 +2172,17 @@ impl From> for Vec { // do this in at most three copy moves. if (cap - tail) > head { // right hand block is the long one; move that enough for the left - ptr::copy( - buf.offset(tail as isize), - buf.offset((tail - head) as isize), - cap - tail); + ptr::copy(buf.offset(tail as isize), + buf.offset((tail - head) as isize), + cap - tail); // copy left in the end ptr::copy(buf, buf.offset((cap - head) as isize), head); // shift the new thing to the start - ptr::copy(buf.offset((tail-head) as isize), buf, len); + ptr::copy(buf.offset((tail - head) as isize), buf, len); } else { // left hand block is the long one, we can do it in two! - ptr::copy(buf, buf.offset((cap-tail) as isize), head); - ptr::copy(buf.offset(tail as isize), buf, cap-tail); + ptr::copy(buf, buf.offset((cap - tail) as isize), head); + ptr::copy(buf.offset(tail as isize), buf, cap - tail); } } else { // Need to use N swaps to move the ring @@ -2552,19 +2543,19 @@ mod tests { let cap = (2i32.pow(cap_pwr) - 1) as usize; // In these cases there is enough free space to solve it with copies - for len in 0..((cap+1)/2) { + for len in 0..((cap + 1) / 2) { // Test contiguous cases - for offset in 0..(cap-len) { + for offset in 0..(cap - len) { create_vec_and_test_convert(cap, offset, len) } // Test cases where block at end of buffer is bigger than block at start - for offset in (cap-len)..(cap-(len/2)) { + for offset in (cap - len)..(cap - (len / 2)) { create_vec_and_test_convert(cap, offset, len) } // Test cases where block at start of buffer is bigger than block at end - for offset in (cap-(len/2))..cap { + for offset in (cap - (len / 2))..cap { create_vec_and_test_convert(cap, offset, len) } } @@ -2573,19 +2564,19 @@ mod tests { // the ring will use swapping when: // (cap + 1 - offset) > (cap + 1 - len) && (len - (cap + 1 - offset)) > (cap + 1 - len)) // right block size > free space && left block size > free space - for len in ((cap+1)/2)..cap { + for len in ((cap + 1) / 2)..cap { // Test contiguous cases - for offset in 0..(cap-len) { + for offset in 0..(cap - len) { create_vec_and_test_convert(cap, offset, len) } // Test cases where block at end of buffer is bigger than block at start - for offset in (cap-len)..(cap-(len/2)) { + for offset in (cap - len)..(cap - (len / 2)) { create_vec_and_test_convert(cap, offset, len) } // Test cases where block at start of buffer is bigger than block at end - for offset in (cap-(len/2))..cap { + for offset in (cap - (len / 2))..cap { create_vec_and_test_convert(cap, offset, len) } }