From d6560ddd20f6cc3da04ae07b06a3fabffafe7562 Mon Sep 17 00:00:00 2001 From: Srinivas Reddy Thatiparthy Date: Sun, 5 Jun 2016 11:53:44 +0530 Subject: run rustfmt on map.rs in libcollections/btree folder --- src/libcollections/btree/map.rs | 494 ++++++++++++++++++++++------------------ 1 file changed, 274 insertions(+), 220 deletions(-) (limited to 'src') diff --git a/src/libcollections/btree/map.rs b/src/libcollections/btree/map.rs index 29f3e4b1b61..248240a7de8 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; - }, + } } } @@ -890,7 +939,9 @@ impl BTreeMap { #[unstable(feature = "btree_split_off", reason = "recently added as part of collections reform 2", issue = "19986")] - pub fn split_off(&mut self, key: &Q) -> Self where K: Borrow { + pub fn split_off(&mut self, key: &Q) -> Self + where K: Borrow + { if self.is_empty() { return Self::new(); } @@ -910,7 +961,7 @@ impl BTreeMap { let mut split_edge = match search::search_node(left_node, key) { // key is going to the right tree Found(handle) => handle.left_edge(), - GoDown(handle) => handle + GoDown(handle) => handle, }; split_edge.move_suffix(&mut right_node); @@ -920,8 +971,12 @@ impl BTreeMap { left_node = edge.descend(); right_node = node.first_edge().descend(); } - (Leaf(_), Leaf(_)) => { break; }, - _ => { unreachable!(); } + (Leaf(_), Leaf(_)) => { + break; + } + _ => { + unreachable!(); + } } } } @@ -950,8 +1005,12 @@ impl BTreeMap { loop { res += dfs(edge.reborrow().descend()); match edge.right_kv() { - Ok(right_kv) => { edge = right_kv.right_edge(); }, - Err(_) => { break; } + Ok(right_kv) => { + edge = right_kv.right_edge(); + } + Err(_) => { + break; + } } } } @@ -1064,14 +1123,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, } } } @@ -1114,7 +1175,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 { @@ -1130,14 +1193,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() { @@ -1168,10 +1232,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 { @@ -1181,10 +1245,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()); - } + }, } } } @@ -1210,10 +1274,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 { @@ -1223,17 +1287,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> { @@ -1262,9 +1328,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() } } } @@ -1294,9 +1358,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() } } } @@ -1348,7 +1410,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) @@ -1361,7 +1423,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); @@ -1390,7 +1452,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) @@ -1403,7 +1465,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); @@ -1417,7 +1479,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, } } } @@ -1429,7 +1491,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()) } } } } @@ -1443,7 +1505,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) @@ -1456,7 +1518,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); @@ -1485,7 +1547,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) @@ -1498,7 +1560,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); @@ -1509,7 +1571,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 @@ -1518,7 +1580,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); } @@ -1526,7 +1588,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))); } } @@ -1547,8 +1609,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) } } @@ -1575,7 +1636,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; @@ -1585,11 +1647,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(), @@ -1600,11 +1660,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(), @@ -1653,9 +1711,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, } } @@ -1690,7 +1748,7 @@ impl BTreeMap { back: last_leaf_edge(root2), _marker: PhantomData, }, - length: self.length + length: self.length, } } @@ -1865,15 +1923,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 }; @@ -1928,7 +1988,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; @@ -1938,12 +1998,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) } @@ -1955,14 +2011,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, } } @@ -1974,13 +2032,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 { @@ -1989,10 +2045,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()); + } } } }; @@ -2009,7 +2067,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)> { @@ -2023,16 +2081,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() - }, + } } } } -- cgit 1.4.1-3-g733a5 From 27d4ed4d340b30d0bc6ba7b49f27af68566af06a Mon Sep 17 00:00:00 2001 From: Srinivas Reddy Thatiparthy Date: Sun, 5 Jun 2016 13:01:11 +0530 Subject: run rustfmt on librustc_bitflags folder --- src/librustc_bitflags/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/librustc_bitflags/lib.rs b/src/librustc_bitflags/lib.rs index e2025eaa8ee..afc2e04d446 100644 --- a/src/librustc_bitflags/lib.rs +++ b/src/librustc_bitflags/lib.rs @@ -291,8 +291,8 @@ macro_rules! bitflags { #[cfg(test)] #[allow(non_upper_case_globals)] mod tests { - use std::hash::{Hasher, Hash, SipHasher}; - use std::option::Option::{Some, None}; + use std::hash::{Hash, Hasher, SipHasher}; + use std::option::Option::{None, Some}; bitflags! { #[doc = "> The first principle is that you must not fool yourself — and"] -- cgit 1.4.1-3-g733a5 From 75fc40c668ca564b08674caebc32c77325900689 Mon Sep 17 00:00:00 2001 From: Joachim Viide Date: Tue, 7 Jun 2016 02:37:12 +0300 Subject: Remove a gotcha from book/error-handling.md The book's "Error handling with Box" section talks about Box. In the actual example Box is used instead so that the corresponding From impls could be used to convert a plain string to an error type. Rust 1.7 added support for conversion from &str/String to Box, so this gotcha and later references to it can now be removed. --- src/doc/book/error-handling.md | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/doc/book/error-handling.md b/src/doc/book/error-handling.md index d94eeaebf40..544f837d69b 100644 --- a/src/doc/book/error-handling.md +++ b/src/doc/book/error-handling.md @@ -1829,7 +1829,7 @@ use std::error::Error; fn search> (file_path: P, city: &str) - -> Result, Box> { + -> Result, Box> { let mut found = vec![]; let file = try!(File::open(file_path)); let mut rdr = csv::Reader::from_reader(file); @@ -1858,20 +1858,17 @@ Instead of `x.unwrap()`, we now have `try!(x)`. Since our function returns a `Result`, the `try!` macro will return early from the function if an error occurs. -There is one big gotcha in this code: we used `Box` -instead of `Box`. We did this so we could convert a plain string to an -error type. We need these extra bounds so that we can use the -[corresponding `From` -impls](../std/convert/trait.From.html): +At the end of `search` we also convert a plain string to an error type +by using the [corresponding `From` impls](../std/convert/trait.From.html): ```rust,ignore // We are making use of this impl in the code above, since we call `From::from` // on a `&'static str`. -impl<'a, 'b> From<&'b str> for Box +impl<'a> From<&'a str> for Box // But this is also useful when you need to allocate a new string for an // error message, usually with `format!`. -impl From for Box +impl From for Box ``` Since `search` now returns a `Result`, `main` should use case analysis @@ -1964,7 +1961,7 @@ use std::io; fn search> (file_path: &Option

, city: &str) - -> Result, Box> { + -> Result, Box> { let mut found = vec![]; let input: Box = match *file_path { None => Box::new(io::stdin()), @@ -2175,9 +2172,8 @@ heuristics! `unwrap`. Be warned: if it winds up in someone else's hands, don't be surprised if they are agitated by poor error messages! * If you're writing a quick 'n' dirty program and feel ashamed about panicking - anyway, then use either a `String` or a `Box` for your - error type (the `Box` type is because of the - [available `From` impls](../std/convert/trait.From.html)). + anyway, then use either a `String` or a `Box` for your + error type. * Otherwise, in a program, define your own error types with appropriate [`From`](../std/convert/trait.From.html) and -- cgit 1.4.1-3-g733a5 From 4504df751448dce8427ac244d07dfecc020319b7 Mon Sep 17 00:00:00 2001 From: Jacob Clark Date: Tue, 7 Jun 2016 11:38:32 +0100 Subject: Test case for borrowk ICE #25579 --- src/test/run-pass/issue-25579.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/test/run-pass/issue-25579.rs (limited to 'src') diff --git a/src/test/run-pass/issue-25579.rs b/src/test/run-pass/issue-25579.rs new file mode 100644 index 00000000000..53fe67cfc5a --- /dev/null +++ b/src/test/run-pass/issue-25579.rs @@ -0,0 +1,27 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +enum Sexpression { + Num(()), + Cons(&'static mut Sexpression) +} + +fn causes_ice(mut l: &mut Sexpression) +{ + loop { match l { + &mut Sexpression::Num(ref mut n) => {}, + &mut Sexpression::Cons(ref mut expr) => { + l = &mut **expr; + } + }} +} + +fn main() { +} -- cgit 1.4.1-3-g733a5 From 96c85f4e3765ae415852e19c0c899f419f49e2fc Mon Sep 17 00:00:00 2001 From: Srinivas Reddy Thatiparthy Date: Tue, 7 Jun 2016 21:29:35 +0530 Subject: run rustfmt on libflate folder --- src/libflate/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/libflate/lib.rs b/src/libflate/lib.rs index b578b064d67..63913f2878c 100644 --- a/src/libflate/lib.rs +++ b/src/libflate/lib.rs @@ -31,7 +31,7 @@ extern crate libc; -use libc::{c_void, size_t, c_int}; +use libc::{c_int, c_void, size_t}; use std::fmt; use std::ops::Deref; use std::ptr::Unique; @@ -76,9 +76,9 @@ impl Drop for Bytes { #[link(name = "miniz", kind = "static")] #[cfg(not(cargobuild))] -extern {} +extern "C" {} -extern { +extern "C" { /// Raw miniz compression function. fn tdefl_compress_mem_to_heap(psrc_buf: *const c_void, src_buf_len: size_t, @@ -154,8 +154,8 @@ pub fn inflate_bytes_zlib(bytes: &[u8]) -> Result { #[cfg(test)] mod tests { #![allow(deprecated)] - use super::{inflate_bytes, deflate_bytes}; - use std::__rand::{thread_rng, Rng}; + use super::{deflate_bytes, inflate_bytes}; + use std::__rand::{Rng, thread_rng}; #[test] fn test_flate_round_trip() { -- cgit 1.4.1-3-g733a5 From 7abdbd4483ae66fd462c8310a0e67a62d76137ce Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 7 Jun 2016 20:19:08 +0300 Subject: docs: simplify wording --- src/libcore/any.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/libcore/any.rs b/src/libcore/any.rs index dfd2ba9154d..49304b1f3bf 100644 --- a/src/libcore/any.rs +++ b/src/libcore/any.rs @@ -85,7 +85,7 @@ use marker::{Reflect, Sized}; /// A type to emulate dynamic typing. /// -/// Every type with no non-`'static` references implements `Any`. +/// Most types implement `Any`. However, any type which contains a non-`'static` reference does not. /// See the [module-level documentation][mod] for more details. /// /// [mod]: index.html -- cgit 1.4.1-3-g733a5 From 0379493bd9def567b73f105319495e9ca106deca Mon Sep 17 00:00:00 2001 From: Jacob Clark Date: Tue, 7 Jun 2016 12:35:41 +0100 Subject: Resolving build failure --- src/test/compile-fail/issue-25579.rs | 27 +++++++++++++++++++++++++++ src/test/run-pass/issue-25579.rs | 27 --------------------------- 2 files changed, 27 insertions(+), 27 deletions(-) create mode 100644 src/test/compile-fail/issue-25579.rs delete mode 100644 src/test/run-pass/issue-25579.rs (limited to 'src') diff --git a/src/test/compile-fail/issue-25579.rs b/src/test/compile-fail/issue-25579.rs new file mode 100644 index 00000000000..95b07b208d1 --- /dev/null +++ b/src/test/compile-fail/issue-25579.rs @@ -0,0 +1,27 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +enum Sexpression { + Num(()), + Cons(&'static mut Sexpression) +} + +fn causes_ice(mut l: &mut Sexpression) { + loop { match l { + &mut Sexpression::Num(ref mut n) => {}, + &mut Sexpression::Cons(ref mut expr) => { //~ ERROR cannot borrow `l.0` as mutable more than once at a time [E0499] + //~| ERROR cannot borrow `l.0` as mutable more than once at a time [E0499] + l = &mut **expr; //~ ERROR cannot assign to `l` because it is borrowed [E0506] + } + }} +} + +fn main() { +} \ No newline at end of file diff --git a/src/test/run-pass/issue-25579.rs b/src/test/run-pass/issue-25579.rs deleted file mode 100644 index 53fe67cfc5a..00000000000 --- a/src/test/run-pass/issue-25579.rs +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2015 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -enum Sexpression { - Num(()), - Cons(&'static mut Sexpression) -} - -fn causes_ice(mut l: &mut Sexpression) -{ - loop { match l { - &mut Sexpression::Num(ref mut n) => {}, - &mut Sexpression::Cons(ref mut expr) => { - l = &mut **expr; - } - }} -} - -fn main() { -} -- cgit 1.4.1-3-g733a5 From 4c5f3a6d65247cafc44cf676111770899daa1c0f Mon Sep 17 00:00:00 2001 From: Jacob Clark Date: Wed, 8 Jun 2016 13:26:18 +0100 Subject: Resolving line length build fail --- src/test/compile-fail/issue-25579.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/test/compile-fail/issue-25579.rs b/src/test/compile-fail/issue-25579.rs index 95b07b208d1..849c9aa18c9 100644 --- a/src/test/compile-fail/issue-25579.rs +++ b/src/test/compile-fail/issue-25579.rs @@ -16,12 +16,12 @@ enum Sexpression { fn causes_ice(mut l: &mut Sexpression) { loop { match l { &mut Sexpression::Num(ref mut n) => {}, - &mut Sexpression::Cons(ref mut expr) => { //~ ERROR cannot borrow `l.0` as mutable more than once at a time [E0499] - //~| ERROR cannot borrow `l.0` as mutable more than once at a time [E0499] - l = &mut **expr; //~ ERROR cannot assign to `l` because it is borrowed [E0506] + &mut Sexpression::Cons(ref mut expr) => { //~ ERROR cannot borrow `l.0` + //~| ERROR cannot borrow `l.0` + l = &mut **expr; //~ ERROR cannot assign to `l` } }} } fn main() { -} \ No newline at end of file +} -- cgit 1.4.1-3-g733a5 From 92e8228813a248520a4c3cadf6457c5225502525 Mon Sep 17 00:00:00 2001 From: Hendrik Sollich Date: Tue, 7 Jun 2016 11:30:38 +0200 Subject: Fixed two little Game Of Thrones References Fixed: conflicting spelling of "Jon Snow" Fixed: It's call "Night's Watch" --- src/librustc_typeck/diagnostics.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index 67a082ee520..7181f514ed9 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -1174,7 +1174,7 @@ Erroneous code example: ```compile_fail #[repr(i32)] -enum NightWatch {} // error: unsupported representation for zero-variant enum +enum NightsWatch {} // error: unsupported representation for zero-variant enum ``` It is impossible to define an integer type to be used to represent zero-variant @@ -1184,8 +1184,8 @@ two solutions. Either you add variants in your enum: ``` #[repr(i32)] -enum NightWatch { - JohnSnow, +enum NightsWatch { + JonSnow, Commander, } ``` @@ -1193,7 +1193,7 @@ enum NightWatch { or you remove the integer represention of your enum: ``` -enum NightWatch {} +enum NightsWatch {} ``` "##, -- cgit 1.4.1-3-g733a5 From bc4def9e74305112b46ca72bd47940c71480e09a Mon Sep 17 00:00:00 2001 From: Oliver Middleton Date: Wed, 8 Jun 2016 19:24:03 +0100 Subject: docs: Improve char::to_{lower,upper}case examples Collect the results to a String to make it clear that it will not always return only one char and add examples showing that. --- src/librustc_unicode/char.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/librustc_unicode/char.rs b/src/librustc_unicode/char.rs index 77de51b32e2..f570375de5e 100644 --- a/src/librustc_unicode/char.rs +++ b/src/librustc_unicode/char.rs @@ -668,10 +668,13 @@ impl char { /// Basic usage: /// /// ``` - /// assert_eq!('C'.to_lowercase().next(), Some('c')); + /// assert_eq!('C'.to_lowercase().collect::(), "c"); + /// + /// // Sometimes the result is more than one character: + /// assert_eq!('İ'.to_lowercase().collect::(), "i\u{307}"); /// /// // Japanese scripts do not have case, and so: - /// assert_eq!('山'.to_lowercase().next(), Some('山')); + /// assert_eq!('山'.to_lowercase().collect::(), "山"); /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] @@ -702,10 +705,13 @@ impl char { /// Basic usage: /// /// ``` - /// assert_eq!('c'.to_uppercase().next(), Some('C')); + /// assert_eq!('c'.to_uppercase().collect::(), "C"); + /// + /// // Sometimes the result is more than one character: + /// assert_eq!('ß'.to_uppercase().collect::(), "SS"); /// /// // Japanese does not have case, and so: - /// assert_eq!('山'.to_uppercase().next(), Some('山')); + /// assert_eq!('山'.to_uppercase().collect::(), "山"); /// ``` /// /// In Turkish, the equivalent of 'i' in Latin has five forms instead of two: @@ -716,17 +722,17 @@ impl char { /// Note that the lowercase dotted 'i' is the same as the Latin. Therefore: /// /// ``` - /// let upper_i = 'i'.to_uppercase().next(); + /// let upper_i: String = 'i'.to_uppercase().collect(); /// ``` /// /// The value of `upper_i` here relies on the language of the text: if we're - /// in `en-US`, it should be `Some('I')`, but if we're in `tr_TR`, it should - /// be `Some('İ')`. `to_uppercase()` does not take this into account, and so: + /// in `en-US`, it should be `"I"`, but if we're in `tr_TR`, it should + /// be `"İ"`. `to_uppercase()` does not take this into account, and so: /// /// ``` - /// let upper_i = 'i'.to_uppercase().next(); + /// let upper_i: String = 'i'.to_uppercase().collect(); /// - /// assert_eq!(Some('I'), upper_i); + /// assert_eq!(upper_i, "I"); /// ``` /// /// holds across languages. -- cgit 1.4.1-3-g733a5 From 8180a910f9957a8b9d192a1a7f129dcc459f0d43 Mon Sep 17 00:00:00 2001 From: Pavel Pravosud Date: Wed, 8 Jun 2016 20:32:24 -0700 Subject: Fix BTreeMap example typo The whole example is made around movies reviews, but that one line says "review some books". --- src/libcollections/btree/map.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/libcollections/btree/map.rs b/src/libcollections/btree/map.rs index 29f3e4b1b61..70dc7cc40b5 100644 --- a/src/libcollections/btree/map.rs +++ b/src/libcollections/btree/map.rs @@ -68,7 +68,7 @@ use self::Entry::*; /// // would be `BTreeMap<&str, &str>` in this example). /// let mut movie_reviews = BTreeMap::new(); /// -/// // review some books. +/// // review some movies. /// movie_reviews.insert("Office Space", "Deals with real issues in the workplace."); /// movie_reviews.insert("Pulp Fiction", "Masterpiece."); /// movie_reviews.insert("The Godfather", "Very enjoyable."); -- cgit 1.4.1-3-g733a5 From 3dfc8c186919140767671c9d56e751c89710e355 Mon Sep 17 00:00:00 2001 From: Tshepang Lekhonkhobe Date: Thu, 9 Jun 2016 23:23:08 +0200 Subject: doc: intro should be 1 sentence Also, do not repeat name of type --- src/libstd/process.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/libstd/process.rs b/src/libstd/process.rs index 3ce9bcc79f2..96ddda32ae4 100644 --- a/src/libstd/process.rs +++ b/src/libstd/process.rs @@ -182,8 +182,10 @@ impl FromInner for ChildStderr { } } -/// The `Command` type acts as a process builder, providing fine-grained control -/// over how a new process should be spawned. A default configuration can be +/// A process builder, providing fine-grained control +/// over how a new process should be spawned. +/// +/// A default configuration can be /// generated using `Command::new(program)`, where `program` gives a path to the /// program to be executed. Additional builder methods allow the configuration /// to be changed (for example, by adding arguments) prior to spawning: -- cgit 1.4.1-3-g733a5