diff options
| author | Aaron Turon <aturon@mozilla.com> | 2014-12-30 10:51:18 -0800 |
|---|---|---|
| committer | Aaron Turon <aturon@mozilla.com> | 2014-12-30 17:06:08 -0800 |
| commit | 6abfac083feafc73e5d736177755cce3bfb7153f (patch) | |
| tree | d5502fab0dd68ea96057616eb20d90a2c9050218 /src/libcollections | |
| parent | 6e1879eaf1cb5e727eb134a3e27018f7535852eb (diff) | |
| download | rust-6abfac083feafc73e5d736177755cce3bfb7153f.tar.gz rust-6abfac083feafc73e5d736177755cce3bfb7153f.zip | |
Fallout from stabilization
Diffstat (limited to 'src/libcollections')
| -rw-r--r-- | src/libcollections/binary_heap.rs | 2 | ||||
| -rw-r--r-- | src/libcollections/bit.rs | 10 | ||||
| -rw-r--r-- | src/libcollections/btree/node.rs | 66 | ||||
| -rw-r--r-- | src/libcollections/dlist.rs | 6 | ||||
| -rw-r--r-- | src/libcollections/lib.rs | 4 | ||||
| -rw-r--r-- | src/libcollections/ring_buf.rs | 6 | ||||
| -rw-r--r-- | src/libcollections/slice.rs | 128 | ||||
| -rw-r--r-- | src/libcollections/str.rs | 88 | ||||
| -rw-r--r-- | src/libcollections/string.rs | 2 | ||||
| -rw-r--r-- | src/libcollections/vec.rs | 61 | ||||
| -rw-r--r-- | src/libcollections/vec_map.rs | 2 |
11 files changed, 178 insertions, 197 deletions
diff --git a/src/libcollections/binary_heap.rs b/src/libcollections/binary_heap.rs index a2f38bb6674..0834eacabd0 100644 --- a/src/libcollections/binary_heap.rs +++ b/src/libcollections/binary_heap.rs @@ -66,7 +66,7 @@ //! // for a simpler implementation. //! fn shortest_path(adj_list: &Vec<Vec<Edge>>, start: uint, goal: uint) -> uint { //! // dist[node] = current shortest distance from `start` to `node` -//! let mut dist = Vec::from_elem(adj_list.len(), uint::MAX); +//! let mut dist: Vec<_> = range(0, adj_list.len()).map(|_| uint::MAX).collect(); //! //! let mut heap = BinaryHeap::new(); //! diff --git a/src/libcollections/bit.rs b/src/libcollections/bit.rs index 430d7210bf6..12faf3b5d5f 100644 --- a/src/libcollections/bit.rs +++ b/src/libcollections/bit.rs @@ -85,7 +85,7 @@ use core::prelude::*; use core::cmp; use core::default::Default; use core::fmt; -use core::iter::{Cloned, Chain, Enumerate, Repeat, Skip, Take}; +use core::iter::{Cloned, Chain, Enumerate, Repeat, Skip, Take, repeat}; use core::iter; use core::num::Int; use core::slice::{Iter, IterMut}; @@ -267,7 +267,7 @@ impl Bitv { pub fn from_elem(nbits: uint, bit: bool) -> Bitv { let nblocks = blocks_for_bits(nbits); let mut bitv = Bitv { - storage: Vec::from_elem(nblocks, if bit { !0u32 } else { 0u32 }), + storage: repeat(if bit { !0u32 } else { 0u32 }).take(nblocks).collect(), nbits: nbits }; bitv.fix_last_block(); @@ -651,7 +651,7 @@ impl Bitv { let len = self.nbits/8 + if self.nbits % 8 == 0 { 0 } else { 1 }; - Vec::from_fn(len, |i| + range(0, len).map(|i| bit(self, i, 0) | bit(self, i, 1) | bit(self, i, 2) | @@ -660,7 +660,7 @@ impl Bitv { bit(self, i, 5) | bit(self, i, 6) | bit(self, i, 7) - ) + ).collect() } /// Deprecated: Use `iter().collect()`. @@ -834,7 +834,7 @@ impl Bitv { // Allocate new words, if needed if new_nblocks > self.storage.len() { let to_add = new_nblocks - self.storage.len(); - self.storage.grow(to_add, full_value); + self.storage.extend(repeat(full_value).take(to_add)); } // Adjust internal bit count diff --git a/src/libcollections/btree/node.rs b/src/libcollections/btree/node.rs index 2c3c546fdb7..9ed7cb18bc8 100644 --- a/src/libcollections/btree/node.rs +++ b/src/libcollections/btree/node.rs @@ -554,10 +554,10 @@ impl <K, V> Node<K, V> { let node = mem::replace(left_and_out, unsafe { Node::new_internal(capacity_from_b(b)) }); left_and_out._len = 1; unsafe { - ptr::write(left_and_out.keys_mut().unsafe_mut(0), key); - ptr::write(left_and_out.vals_mut().unsafe_mut(0), value); - ptr::write(left_and_out.edges_mut().unsafe_mut(0), node); - ptr::write(left_and_out.edges_mut().unsafe_mut(1), right); + ptr::write(left_and_out.keys_mut().get_unchecked_mut(0), key); + ptr::write(left_and_out.vals_mut().get_unchecked_mut(0), value); + ptr::write(left_and_out.edges_mut().get_unchecked_mut(0), node); + ptr::write(left_and_out.edges_mut().get_unchecked_mut(1), right); } } @@ -636,7 +636,7 @@ impl<'a, K: 'a, V: 'a> Handle<&'a Node<K, V>, handle::Edge, handle::Internal> { /// making it more suitable for moving down a chain of nodes. pub fn into_edge(self) -> &'a Node<K, V> { unsafe { - self.node.edges().unsafe_get(self.index) + self.node.edges().get_unchecked(self.index) } } } @@ -647,7 +647,7 @@ impl<'a, K: 'a, V: 'a> Handle<&'a mut Node<K, V>, handle::Edge, handle::Internal /// `edge_mut`, making it more suitable for moving down a chain of nodes. pub fn into_edge_mut(self) -> &'a mut Node<K, V> { unsafe { - self.node.edges_mut().unsafe_mut(self.index) + self.node.edges_mut().get_unchecked_mut(self.index) } } } @@ -721,7 +721,7 @@ impl<K, V, NodeRef: DerefMut<Node<K, V>>> Handle<NodeRef, handle::Edge, handle:: /// confused with `node`, which references the parent node of what is returned here. pub fn edge_mut(&mut self) -> &mut Node<K, V> { unsafe { - self.node.edges_mut().unsafe_mut(self.index) + self.node.edges_mut().get_unchecked_mut(self.index) } } @@ -829,8 +829,8 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<&'a Node<K, V>, handle::KV, NodeType> { let (keys, vals) = self.node.as_slices(); unsafe { ( - keys.unsafe_get(self.index), - vals.unsafe_get(self.index) + keys.get_unchecked(self.index), + vals.get_unchecked(self.index) ) } } @@ -844,8 +844,8 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<&'a mut Node<K, V>, handle::KV, NodeType let (keys, vals) = self.node.as_slices_mut(); unsafe { ( - keys.unsafe_mut(self.index), - vals.unsafe_mut(self.index) + keys.get_unchecked_mut(self.index), + vals.get_unchecked_mut(self.index) ) } } @@ -869,14 +869,14 @@ impl<'a, K: 'a, V: 'a, NodeRef: Deref<Node<K, V>> + 'a, NodeType> Handle<NodeRef // /// reference with a lifetime as large as `into_kv_mut`, but it also does not consume the // /// handle. // pub fn key(&'a self) -> &'a K { - // unsafe { self.node.keys().unsafe_get(self.index) } + // unsafe { self.node.keys().get_unchecked(self.index) } // } // // /// Returns a reference to the value pointed-to by this handle. This doesn't return a // /// reference with a lifetime as large as `into_kv_mut`, but it also does not consume the // /// handle. // pub fn val(&'a self) -> &'a V { - // unsafe { self.node.vals().unsafe_get(self.index) } + // unsafe { self.node.vals().get_unchecked(self.index) } // } } @@ -886,14 +886,14 @@ impl<'a, K: 'a, V: 'a, NodeRef: DerefMut<Node<K, V>> + 'a, NodeType> Handle<Node /// reference with a lifetime as large as `into_kv_mut`, but it also does not consume the /// handle. pub fn key_mut(&'a mut self) -> &'a mut K { - unsafe { self.node.keys_mut().unsafe_mut(self.index) } + unsafe { self.node.keys_mut().get_unchecked_mut(self.index) } } /// Returns a mutable reference to the value pointed-to by this handle. This doesn't return a /// reference with a lifetime as large as `into_kv_mut`, but it also does not consume the /// handle. pub fn val_mut(&'a mut self) -> &'a mut V { - unsafe { self.node.vals_mut().unsafe_mut(self.index) } + unsafe { self.node.vals_mut().get_unchecked_mut(self.index) } } } @@ -1077,7 +1077,7 @@ impl<K, V> Node<K, V> { debug_assert!(!self.is_leaf()); unsafe { - let ret = ptr::read(self.edges().unsafe_get(0)); + let ret = ptr::read(self.edges().get_unchecked(0)); self.destroy(); ptr::write(self, ret); } @@ -1091,8 +1091,8 @@ impl<K, V> Node<K, V> { unsafe fn push_kv(&mut self, key: K, val: V) { let len = self.len(); - ptr::write(self.keys_mut().unsafe_mut(len), key); - ptr::write(self.vals_mut().unsafe_mut(len), val); + ptr::write(self.keys_mut().get_unchecked_mut(len), key); + ptr::write(self.vals_mut().get_unchecked_mut(len), val); self._len += 1; } @@ -1102,7 +1102,7 @@ impl<K, V> Node<K, V> { unsafe fn push_edge(&mut self, edge: Node<K, V>) { let len = self.len(); - ptr::write(self.edges_mut().unsafe_mut(len), edge); + ptr::write(self.edges_mut().get_unchecked_mut(len), edge); } // This must be followed by insert_edge on an internal node. @@ -1119,12 +1119,12 @@ impl<K, V> Node<K, V> { self.len() - index ); - ptr::write(self.keys_mut().unsafe_mut(index), key); - ptr::write(self.vals_mut().unsafe_mut(index), val); + ptr::write(self.keys_mut().get_unchecked_mut(index), key); + ptr::write(self.vals_mut().get_unchecked_mut(index), val); self._len += 1; - self.vals_mut().unsafe_mut(index) + self.vals_mut().get_unchecked_mut(index) } // This can only be called immediately after a call to insert_kv. @@ -1135,14 +1135,14 @@ impl<K, V> Node<K, V> { self.edges().as_ptr().offset(index as int), self.len() - index ); - ptr::write(self.edges_mut().unsafe_mut(index), edge); + ptr::write(self.edges_mut().get_unchecked_mut(index), edge); } // This must be followed by pop_edge on an internal node. #[inline] unsafe fn pop_kv(&mut self) -> (K, V) { - let key = ptr::read(self.keys().unsafe_get(self.len() - 1)); - let val = ptr::read(self.vals().unsafe_get(self.len() - 1)); + let key = ptr::read(self.keys().get_unchecked(self.len() - 1)); + let val = ptr::read(self.vals().get_unchecked(self.len() - 1)); self._len -= 1; @@ -1152,7 +1152,7 @@ impl<K, V> Node<K, V> { // This can only be called immediately after a call to pop_kv. #[inline] unsafe fn pop_edge(&mut self) -> Node<K, V> { - let edge = ptr::read(self.edges().unsafe_get(self.len() + 1)); + let edge = ptr::read(self.edges().get_unchecked(self.len() + 1)); edge } @@ -1160,8 +1160,8 @@ impl<K, V> Node<K, V> { // This must be followed by remove_edge on an internal node. #[inline] unsafe fn remove_kv(&mut self, index: uint) -> (K, V) { - let key = ptr::read(self.keys().unsafe_get(index)); - let val = ptr::read(self.vals().unsafe_get(index)); + let key = ptr::read(self.keys().get_unchecked(index)); + let val = ptr::read(self.vals().get_unchecked(index)); ptr::copy_memory( self.keys_mut().as_mut_ptr().offset(index as int), @@ -1182,7 +1182,7 @@ impl<K, V> Node<K, V> { // This can only be called immediately after a call to remove_kv. #[inline] unsafe fn remove_edge(&mut self, index: uint) -> Node<K, V> { - let edge = ptr::read(self.edges().unsafe_get(index)); + let edge = ptr::read(self.edges().get_unchecked(index)); ptr::copy_memory( self.edges_mut().as_mut_ptr().offset(index as int), @@ -1229,8 +1229,8 @@ impl<K, V> Node<K, V> { ); } - let key = ptr::read(self.keys().unsafe_get(right_offset - 1)); - let val = ptr::read(self.vals().unsafe_get(right_offset - 1)); + let key = ptr::read(self.keys().get_unchecked(right_offset - 1)); + let val = ptr::read(self.vals().get_unchecked(right_offset - 1)); self._len = right_offset - 1; @@ -1249,8 +1249,8 @@ impl<K, V> Node<K, V> { let old_len = self.len(); self._len += right.len() + 1; - ptr::write(self.keys_mut().unsafe_mut(old_len), key); - ptr::write(self.vals_mut().unsafe_mut(old_len), val); + ptr::write(self.keys_mut().get_unchecked_mut(old_len), key); + ptr::write(self.vals_mut().get_unchecked_mut(old_len), val); ptr::copy_nonoverlapping_memory( self.keys_mut().as_mut_ptr().offset(old_len as int + 1), diff --git a/src/libcollections/dlist.rs b/src/libcollections/dlist.rs index d8ce79f4fe9..4eaaad07c99 100644 --- a/src/libcollections/dlist.rs +++ b/src/libcollections/dlist.rs @@ -1290,8 +1290,10 @@ mod tests { v.pop(); } 1 => { - m.pop_front(); - v.remove(0); + if !v.is_empty() { + m.pop_front(); + v.remove(0); + } } 2 | 4 => { m.push_front(-i); diff --git a/src/libcollections/lib.rs b/src/libcollections/lib.rs index fe9d8de440a..688214140c1 100644 --- a/src/libcollections/lib.rs +++ b/src/libcollections/lib.rs @@ -128,8 +128,8 @@ mod prelude { pub use unicode::char::UnicodeChar; // from collections. - pub use slice::{CloneSliceExt, VectorVector}; - pub use str::{IntoMaybeOwned, StrVector}; + pub use slice::{CloneSliceExt, SliceConcatExt}; + pub use str::IntoMaybeOwned; pub use string::{String, ToString}; pub use vec::Vec; } diff --git a/src/libcollections/ring_buf.rs b/src/libcollections/ring_buf.rs index df8e08f07a3..82b24e627d9 100644 --- a/src/libcollections/ring_buf.rs +++ b/src/libcollections/ring_buf.rs @@ -1137,7 +1137,7 @@ impl<'a, T> Iterator<&'a T> for Iter<'a, T> { } let tail = self.tail; self.tail = wrap_index(self.tail + 1, self.ring.len()); - unsafe { Some(self.ring.unsafe_get(tail)) } + unsafe { Some(self.ring.get_unchecked(tail)) } } #[inline] @@ -1154,7 +1154,7 @@ impl<'a, T> DoubleEndedIterator<&'a T> for Iter<'a, T> { return None; } self.head = wrap_index(self.head - 1, self.ring.len()); - unsafe { Some(self.ring.unsafe_get(self.head)) } + unsafe { Some(self.ring.get_unchecked(self.head)) } } } @@ -1173,7 +1173,7 @@ impl<'a, T> RandomAccessIterator<&'a T> for Iter<'a, T> { None } else { let idx = wrap_index(self.tail + j, self.ring.len()); - unsafe { Some(self.ring.unsafe_get(idx)) } + unsafe { Some(self.ring.get_unchecked(idx)) } } } } diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index 375ba38f29a..385380bb797 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -96,20 +96,26 @@ use core::mem::size_of; use core::mem; use core::ops::{FnMut,SliceMut}; use core::prelude::{Clone, Greater, Iterator, IteratorExt, Less, None, Option}; -use core::prelude::{Ord, Ordering, RawPtr, Some, range, IteratorCloneExt, Result}; +use core::prelude::{Ord, Ordering, PtrExt, Some, range, IteratorCloneExt, Result}; use core::ptr; use core::slice as core_slice; use self::Direction::*; use vec::Vec; -pub use core::slice::{Chunks, AsSlice, SplitN, Windows}; +pub use core::slice::{Chunks, AsSlice, Windows}; pub use core::slice::{Iter, IterMut, PartialEqSliceExt}; pub use core::slice::{IntSliceExt, SplitMut, ChunksMut, Split}; pub use core::slice::{SplitN, RSplitN, SplitNMut, RSplitNMut}; pub use core::slice::{bytes, mut_ref_slice, ref_slice}; pub use core::slice::{from_raw_buf, from_raw_mut_buf}; +#[deprecated = "use Iter instead"] +pub type Items<'a, T:'a> = Iter<'a, T>; + +#[deprecated = "use IterMut instead"] +pub type MutItems<'a, T:'a> = IterMut<'a, T>; + //////////////////////////////////////////////////////////////////////////////// // Basic slice extension methods //////////////////////////////////////////////////////////////////////////////// @@ -1412,7 +1418,7 @@ mod tests { use prelude::{Some, None, range, Vec, ToString, Clone, Greater, Less, Equal}; use prelude::{SliceExt, Iterator, IteratorExt, DoubleEndedIteratorExt}; use prelude::{OrdSliceExt, CloneSliceExt, PartialEqSliceExt, AsSlice}; - use prelude::{RandomAccessIterator, Ord, VectorVector}; + use prelude::{RandomAccessIterator, Ord, SliceConcatExt}; use core::cell::Cell; use core::default::Default; use core::mem; @@ -1678,15 +1684,19 @@ mod tests { fn test_swap_remove() { let mut v = vec![1i, 2, 3, 4, 5]; let mut e = v.swap_remove(0); - assert_eq!(e, Some(1)); + assert_eq!(e, 1); assert_eq!(v, vec![5i, 2, 3, 4]); e = v.swap_remove(3); - assert_eq!(e, Some(4)); + assert_eq!(e, 4); assert_eq!(v, vec![5i, 2, 3]); + } - e = v.swap_remove(3); - assert_eq!(e, None); - assert_eq!(v, vec![5i, 2, 3]); + #[test] + #[should_fail] + fn test_swap_remove_fail() { + let mut v = vec![1i]; + let _ = v.swap_remove(0); + let _ = v.swap_remove(0); } #[test] @@ -1970,48 +1980,48 @@ mod tests { } #[test] - fn test_binary_search_elem() { - assert_eq!([1i,2,3,4,5].binary_search_elem(&5).found(), Some(4)); - assert_eq!([1i,2,3,4,5].binary_search_elem(&4).found(), Some(3)); - assert_eq!([1i,2,3,4,5].binary_search_elem(&3).found(), Some(2)); - assert_eq!([1i,2,3,4,5].binary_search_elem(&2).found(), Some(1)); - assert_eq!([1i,2,3,4,5].binary_search_elem(&1).found(), Some(0)); + fn test_binary_search() { + assert_eq!([1i,2,3,4,5].binary_search(&5).ok(), Some(4)); + assert_eq!([1i,2,3,4,5].binary_search(&4).ok(), Some(3)); + assert_eq!([1i,2,3,4,5].binary_search(&3).ok(), Some(2)); + assert_eq!([1i,2,3,4,5].binary_search(&2).ok(), Some(1)); + assert_eq!([1i,2,3,4,5].binary_search(&1).ok(), Some(0)); - assert_eq!([2i,4,6,8,10].binary_search_elem(&1).found(), None); - assert_eq!([2i,4,6,8,10].binary_search_elem(&5).found(), None); - assert_eq!([2i,4,6,8,10].binary_search_elem(&4).found(), Some(1)); - assert_eq!([2i,4,6,8,10].binary_search_elem(&10).found(), Some(4)); + assert_eq!([2i,4,6,8,10].binary_search(&1).ok(), None); + assert_eq!([2i,4,6,8,10].binary_search(&5).ok(), None); + assert_eq!([2i,4,6,8,10].binary_search(&4).ok(), Some(1)); + assert_eq!([2i,4,6,8,10].binary_search(&10).ok(), Some(4)); - assert_eq!([2i,4,6,8].binary_search_elem(&1).found(), None); - assert_eq!([2i,4,6,8].binary_search_elem(&5).found(), None); - assert_eq!([2i,4,6,8].binary_search_elem(&4).found(), Some(1)); - assert_eq!([2i,4,6,8].binary_search_elem(&8).found(), Some(3)); + assert_eq!([2i,4,6,8].binary_search(&1).ok(), None); + assert_eq!([2i,4,6,8].binary_search(&5).ok(), None); + assert_eq!([2i,4,6,8].binary_search(&4).ok(), Some(1)); + assert_eq!([2i,4,6,8].binary_search(&8).ok(), Some(3)); - assert_eq!([2i,4,6].binary_search_elem(&1).found(), None); - assert_eq!([2i,4,6].binary_search_elem(&5).found(), None); - assert_eq!([2i,4,6].binary_search_elem(&4).found(), Some(1)); - assert_eq!([2i,4,6].binary_search_elem(&6).found(), Some(2)); + assert_eq!([2i,4,6].binary_search(&1).ok(), None); + assert_eq!([2i,4,6].binary_search(&5).ok(), None); + assert_eq!([2i,4,6].binary_search(&4).ok(), Some(1)); + assert_eq!([2i,4,6].binary_search(&6).ok(), Some(2)); - assert_eq!([2i,4].binary_search_elem(&1).found(), None); - assert_eq!([2i,4].binary_search_elem(&5).found(), None); - assert_eq!([2i,4].binary_search_elem(&2).found(), Some(0)); - assert_eq!([2i,4].binary_search_elem(&4).found(), Some(1)); + assert_eq!([2i,4].binary_search(&1).ok(), None); + assert_eq!([2i,4].binary_search(&5).ok(), None); + assert_eq!([2i,4].binary_search(&2).ok(), Some(0)); + assert_eq!([2i,4].binary_search(&4).ok(), Some(1)); - assert_eq!([2i].binary_search_elem(&1).found(), None); - assert_eq!([2i].binary_search_elem(&5).found(), None); - assert_eq!([2i].binary_search_elem(&2).found(), Some(0)); + assert_eq!([2i].binary_search(&1).ok(), None); + assert_eq!([2i].binary_search(&5).ok(), None); + assert_eq!([2i].binary_search(&2).ok(), Some(0)); - assert_eq!([].binary_search_elem(&1i).found(), None); - assert_eq!([].binary_search_elem(&5i).found(), None); + assert_eq!([].binary_search(&1i).ok(), None); + assert_eq!([].binary_search(&5i).ok(), None); - assert!([1i,1,1,1,1].binary_search_elem(&1).found() != None); - assert!([1i,1,1,1,2].binary_search_elem(&1).found() != None); - assert!([1i,1,1,2,2].binary_search_elem(&1).found() != None); - assert!([1i,1,2,2,2].binary_search_elem(&1).found() != None); - assert_eq!([1i,2,2,2,2].binary_search_elem(&1).found(), Some(0)); + assert!([1i,1,1,1,1].binary_search(&1).ok() != None); + assert!([1i,1,1,1,2].binary_search(&1).ok() != None); + assert!([1i,1,1,2,2].binary_search(&1).ok() != None); + assert!([1i,1,2,2,2].binary_search(&1).ok() != None); + assert_eq!([1i,2,2,2,2].binary_search(&1).ok(), Some(0)); - assert_eq!([1i,2,3,4,5].binary_search_elem(&6).found(), None); - assert_eq!([1i,2,3,4,5].binary_search_elem(&0).found(), None); + assert_eq!([1i,2,3,4,5].binary_search(&6).ok(), None); + assert_eq!([1i,2,3,4,5].binary_search(&0).ok(), None); } #[test] @@ -2106,13 +2116,15 @@ mod tests { #[test] fn test_concat() { let v: [Vec<int>, ..0] = []; - assert_eq!(v.concat_vec(), vec![]); - assert_eq!([vec![1i], vec![2i,3i]].concat_vec(), vec![1, 2, 3]); + let c: Vec<int> = v.concat(); + assert_eq!(c, []); + let d: Vec<int> = [vec![1i], vec![2i,3i]].concat(); + assert_eq!(d, vec![1i, 2, 3]); let v: [&[int], ..2] = [&[1], &[2, 3]]; - assert_eq!(v.connect_vec(&0), vec![1, 0, 2, 3]); - let v: [&[int], ..3] = [&[1], &[2], &[3]]; - assert_eq!(v.connect_vec(&0), vec![1, 0, 2, 0, 3]); + assert_eq!(v.connect(&0), vec![1i, 0, 2, 3]); + let v: [&[int], ..3] = [&[1i], &[2], &[3]]; + assert_eq!(v.connect(&0), vec![1i, 0, 2, 0, 3]); } #[test] @@ -2158,23 +2170,25 @@ mod tests { fn test_remove() { let mut a = vec![1i,2,3,4]; - assert_eq!(a.remove(2), Some(3)); + assert_eq!(a.remove(2), 3); assert_eq!(a, vec![1i,2,4]); - assert_eq!(a.remove(2), Some(4)); - assert_eq!(a, vec![1i,2]); - - assert_eq!(a.remove(2), None); + assert_eq!(a.remove(2), 4); assert_eq!(a, vec![1i,2]); - assert_eq!(a.remove(0), Some(1)); + assert_eq!(a.remove(0), 1); assert_eq!(a, vec![2i]); - assert_eq!(a.remove(0), Some(2)); + assert_eq!(a.remove(0), 2); assert_eq!(a, vec![]); + } - assert_eq!(a.remove(0), None); - assert_eq!(a.remove(10), None); + #[test] + #[should_fail] + fn test_remove_fail() { + let mut a = vec![1i]; + let _ = a.remove(0); + let _ = a.remove(0); } #[test] @@ -2870,7 +2884,7 @@ mod bench { let xss: Vec<Vec<uint>> = Vec::from_fn(100, |i| range(0u, i).collect()); b.iter(|| { - xss.as_slice().concat_vec() + xss.as_slice().concat(); }); } diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs index 60449c817cb..8d5d76f9598 100644 --- a/src/libcollections/str.rs +++ b/src/libcollections/str.rs @@ -96,40 +96,43 @@ Section: Creating a string impl<S: Str> SliceConcatExt<str, String> for [S] { fn concat(&self) -> String { - if self.is_empty() { + let s = self.as_slice(); + + if s.is_empty() { return String::new(); } // `len` calculation may overflow but push_str will check boundaries - let len = self.iter().map(|s| s.as_slice().len()).sum(); - + let len = s.iter().map(|s| s.as_slice().len()).sum(); let mut result = String::with_capacity(len); - for s in self.iter() { - result.push_str(s.as_slice()); + for s in s.iter() { + result.push_str(s.as_slice()) } result } fn connect(&self, sep: &str) -> String { - if self.is_empty() { + let s = self.as_slice(); + + if s.is_empty() { return String::new(); } // concat is faster if sep.is_empty() { - return self.concat(); + return s.concat(); } // this is wrong without the guarantee that `self` is non-empty // `len` calculation may overflow but push_str but will check boundaries - let len = sep.len() * (self.len() - 1) - + self.iter().map(|s| s.as_slice().len()).sum(); + let len = sep.len() * (s.len() - 1) + + s.iter().map(|s| s.as_slice().len()).sum(); let mut result = String::with_capacity(len); let mut first = true; - for s in self.iter() { + for s in s.iter() { if first { first = false; } else { @@ -141,11 +144,6 @@ impl<S: Str> SliceConcatExt<str, String> for [S] { } } -impl<S: Str> SliceConcatExt<str, String> for Vec<S> { - fn concat(&self) -> String { self[].concat() } - fn connect(&self, sep: &str) -> String { self[].connect(sep) } -} - /* Section: Iterators */ @@ -186,7 +184,7 @@ pub struct Decompositions<'a> { impl<'a> Iterator<char> for Decompositions<'a> { #[inline] fn next(&mut self) -> Option<char> { - match self.buffer.head() { + match self.buffer.first() { Some(&(c, 0)) => { self.sorted = false; self.buffer.remove(0); @@ -233,13 +231,16 @@ impl<'a> Iterator<char> for Decompositions<'a> { self.sorted = true; } - match self.buffer.remove(0) { - Some((c, 0)) => { - self.sorted = false; - Some(c) + if self.buffer.is_empty() { + None + } else { + match self.buffer.remove(0) { + (c, 0) => { + self.sorted = false; + Some(c) + } + (c, _) => Some(c), } - Some((c, _)) => Some(c), - None => None } } @@ -712,7 +713,7 @@ pub trait StrExt for Sized?: ops::Slice<uint, str> { if me.is_empty() { return t.chars().count(); } if t.is_empty() { return me.chars().count(); } - let mut dcol = Vec::from_fn(t.len() + 1, |x| x); + let mut dcol: Vec<_> = range(0, t.len() + 1).collect(); let mut t_last = 0; for (i, sc) in me.chars().enumerate() { @@ -1857,22 +1858,12 @@ mod tests { assert_eq!("ะเทศไท", "ประเทศไทย中华Việt Nam".slice_chars(2, 8)); } - struct S { - x: [String, .. 2] - } - - impl AsSlice<String> for S { - fn as_slice<'a> (&'a self) -> &'a [String] { - &self.x - } - } - fn s(x: &str) -> String { x.into_string() } macro_rules! test_concat { ($expected: expr, $string: expr) => { { - let s = $string.concat(); + let s: String = $string.concat(); assert_eq!($expected, s); } } @@ -1880,22 +1871,10 @@ mod tests { #[test] fn test_concat_for_different_types() { - test_concat!("ab", ["a", "b"]); - test_concat!("ab", [s("a"), s("b")]); + test_concat!("ab", vec![s("a"), s("b")]); test_concat!("ab", vec!["a", "b"]); test_concat!("ab", vec!["a", "b"].as_slice()); test_concat!("ab", vec![s("a"), s("b")]); - - let mut v0 = ["a", "b"]; - let mut v1 = [s("a"), s("b")]; - unsafe { - use std::c_vec::CVec; - - test_concat!("ab", CVec::new(v0.as_mut_ptr(), v0.len())); - test_concat!("ab", CVec::new(v1.as_mut_ptr(), v1.len())); - } - - test_concat!("ab", S { x: [s("a"), s("b")] }); } #[test] @@ -1924,17 +1903,6 @@ mod tests { test_connect!("a-b", vec!["a", "b"], hyphen.as_slice()); test_connect!("a-b", vec!["a", "b"].as_slice(), "-"); test_connect!("a-b", vec![s("a"), s("b")], "-"); - - let mut v0 = ["a", "b"]; - let mut v1 = [s("a"), s("b")]; - unsafe { - use std::c_vec::CVec; - - test_connect!("a-b", CVec::new(v0.as_mut_ptr(), v0.len()), "-"); - test_connect!("a-b", CVec::new(v1.as_mut_ptr(), v1.len()), hyphen.as_slice()); - } - - test_connect!("a-b", S { x: [s("a"), s("b")] }, "-"); } #[test] @@ -3304,7 +3272,7 @@ mod tests { #[cfg(test)] mod bench { use super::*; - use prelude::{SliceExt, IteratorExt, DoubleEndedIteratorExt}; + use prelude::{SliceExt, IteratorExt, DoubleEndedIteratorExt, SliceConcatExt}; use test::Bencher; use test::black_box; @@ -3467,7 +3435,7 @@ mod bench { fn bench_connect(b: &mut Bencher) { let s = "ศไทย中华Việt Nam; Mary had a little lamb, Little lamb"; let sep = "→"; - let v = [s, s, s, s, s, s, s, s, s, s]; + let v = vec![s, s, s, s, s, s, s, s, s, s]; b.iter(|| { assert_eq!(v.connect(sep).len(), s.len() * 10 + sep.len() * 9); }) diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index c6c19cae75f..433384b625f 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -151,7 +151,7 @@ impl String { let mut i = 0; let total = v.len(); fn unsafe_get(xs: &[u8], i: uint) -> u8 { - unsafe { *xs.unsafe_get(i) } + unsafe { *xs.get_unchecked(i) } } fn safe_get(xs: &[u8], i: uint, total: uint) -> u8 { if i >= total { diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index 2d71705d80d..a694242d1cc 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -173,8 +173,7 @@ impl<T> Vec<T> { /// /// It is important to note that this function does not specify the *length* of the returned /// vector, but only the *capacity*. (For an explanation of the difference between length and - /// capacity, see the main `Vec<T>` docs above, 'Capacity and reallocation'.) To create a - /// vector of a given length, use `Vec::from_elem` or `Vec::from_fn`. + /// capacity, see the main `Vec<T>` docs above, 'Capacity and reallocation'.) /// /// # Examples /// @@ -272,7 +271,7 @@ impl<T> Vec<T> { /// Deprecated: use `into_iter().partition(f)` instead. #[inline] #[deprecated = "use into_iter().partition(f) instead"] - pub fn partition<F>(self, mut f: F) -> (Vec<T>, Vec<T>) where F: FnMut(&T) -> bool { + pub fn partition<F>(self, f: F) -> (Vec<T>, Vec<T>) where F: FnMut(&T) -> bool { self.into_iter().partition(f) } @@ -298,7 +297,7 @@ impl<T> Vec<T> { } /// Reserves capacity for at least `additional` more elements to be inserted in the given - /// `Vec`. The collection may reserve more space to avoid frequent reallocations. + /// `Vec<T>`. The collection may reserve more space to avoid frequent reallocations. /// /// # Panics /// @@ -322,7 +321,7 @@ impl<T> Vec<T> { } /// Reserves the minimum capacity for exactly `additional` more elements to - /// be inserted in the given `Vec`. Does nothing if the capacity is already + /// be inserted in the given `Vec<T>`. Does nothing if the capacity is already /// sufficient. /// /// Note that the allocator may give the collection more space than it @@ -350,9 +349,10 @@ impl<T> Vec<T> { } } - /// Shrinks the capacity of the vector as much as possible. It will drop - /// down as close as possible to the length but the allocator may still - /// inform the vector that there is space for a few more elements. + /// Shrinks the capacity of the vector as much as possible. + /// + /// It will drop down as close as possible to the length but the allocator + /// may still inform the vector that there is space for a few more elements. /// /// # Examples /// @@ -370,7 +370,7 @@ impl<T> Vec<T> { if self.len == 0 { if self.cap != 0 { unsafe { - dealloc(self.ptr, self.cap) + dealloc(*self.ptr, self.cap) } self.cap = 0; } @@ -378,11 +378,12 @@ impl<T> Vec<T> { unsafe { // Overflow check is unnecessary as the vector is already at // least this large. - self.ptr = reallocate(self.ptr as *mut u8, - self.cap * mem::size_of::<T>(), - self.len * mem::size_of::<T>(), - mem::min_align_of::<T>()) as *mut T; - if self.ptr.is_null() { ::alloc::oom() } + let ptr = reallocate(*self.ptr as *mut u8, + self.cap * mem::size_of::<T>(), + self.len * mem::size_of::<T>(), + mem::min_align_of::<T>()) as *mut T; + if ptr.is_null() { ::alloc::oom() } + self.ptr = NonZero::new(ptr); } self.cap = self.len; } @@ -443,15 +444,15 @@ impl<T> Vec<T> { pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] { unsafe { mem::transmute(RawSlice { - data: self.ptr as *const T, + data: *self.ptr as *const T, len: self.len, }) } } - /// Creates a consuming iterator, that is, one that moves each - /// value out of the vector (from start to end). The vector cannot - /// be used after calling this. + /// Creates a consuming iterator, that is, one that moves each value out of + /// the vector (from start to end). The vector cannot be used after calling + /// this. /// /// # Examples /// @@ -466,9 +467,9 @@ impl<T> Vec<T> { #[stable] pub fn into_iter(self) -> IntoIter<T> { unsafe { - let ptr = self.ptr; + let ptr = *self.ptr; let cap = self.cap; - let begin = self.ptr as *const T; + let begin = ptr as *const T; let end = if mem::size_of::<T>() == 0 { (ptr as uint + self.len()) as *const T } else { @@ -512,13 +513,11 @@ impl<T> Vec<T> { /// ``` /// let mut v = vec!["foo", "bar", "baz", "qux"]; /// - /// assert_eq!(v.swap_remove(1), Some("bar")); + /// assert_eq!(v.swap_remove(1), "bar"); /// assert_eq!(v, vec!["foo", "qux", "baz"]); /// - /// assert_eq!(v.swap_remove(0), Some("foo")); + /// assert_eq!(v.swap_remove(0), "foo"); /// assert_eq!(v, vec!["baz", "qux"]); - /// - /// assert_eq!(v.swap_remove(2), None); /// ``` #[inline] #[stable] @@ -577,11 +576,7 @@ impl<T> Vec<T> { /// /// ``` /// let mut v = vec![1i, 2, 3]; - /// assert_eq!(v.remove(1), Some(2)); - /// assert_eq!(v, vec![1, 3]); - /// - /// assert_eq!(v.remove(4), None); - /// // v is unchanged: + /// assert_eq!(v.remove(1), 2); /// assert_eq!(v, vec![1, 3]); /// ``` #[stable] @@ -1185,8 +1180,9 @@ impl<T> Vec<T> { let size = capacity.checked_mul(mem::size_of::<T>()) .expect("capacity overflow"); unsafe { - self.ptr = alloc_or_realloc(self.ptr, self.cap * mem::size_of::<T>(), size); - if self.ptr.is_null() { ::alloc::oom() } + let ptr = alloc_or_realloc(*self.ptr, self.cap * mem::size_of::<T>(), size); + if ptr.is_null() { ::alloc::oom() } + self.ptr = NonZero::new(ptr); } self.cap = capacity; } @@ -2207,9 +2203,10 @@ mod tests { } #[test] + #[should_fail] fn test_swap_remove_empty() { let mut vec: Vec<uint> = vec!(); - assert_eq!(vec.swap_remove(0), None); + vec.swap_remove(0); } #[test] diff --git a/src/libcollections/vec_map.rs b/src/libcollections/vec_map.rs index 5ebcc736624..42d4a28405c 100644 --- a/src/libcollections/vec_map.rs +++ b/src/libcollections/vec_map.rs @@ -448,7 +448,7 @@ impl<V> VecMap<V> { pub fn insert(&mut self, key: uint, value: V) -> Option<V> { let len = self.v.len(); if len <= key { - self.v.grow_fn(key - len + 1, |_| None); + self.v.extend(range(0, key - len + 1).map(|_| None)); } replace(&mut self.v[key], Some(value)) } |
