diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2014-06-06 10:27:49 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2014-06-11 15:02:17 -0700 |
| commit | 3316b1eb7c3eb520896af489dd45c4d17190d0a8 (patch) | |
| tree | c94cde854a882cad33d8ec7fe0067b43b5cb96d7 /src/libcollections | |
| parent | f9260d41d6e37653bf71b08a041be0310098716a (diff) | |
| download | rust-3316b1eb7c3eb520896af489dd45c4d17190d0a8.tar.gz rust-3316b1eb7c3eb520896af489dd45c4d17190d0a8.zip | |
rustc: Remove ~[T] from the language
The following features have been removed * box [a, b, c] * ~[a, b, c] * box [a, ..N] * ~[a, ..N] * ~[T] (as a type) * deprecated_owned_vector lint All users of ~[T] should move to using Vec<T> instead.
Diffstat (limited to 'src/libcollections')
| -rw-r--r-- | src/libcollections/hash/mod.rs | 7 | ||||
| -rw-r--r-- | src/libcollections/hash/sip.rs | 5 | ||||
| -rw-r--r-- | src/libcollections/slice.rs | 319 | ||||
| -rw-r--r-- | src/libcollections/str.rs | 24 | ||||
| -rw-r--r-- | src/libcollections/vec.rs | 90 |
5 files changed, 111 insertions, 334 deletions
diff --git a/src/libcollections/hash/mod.rs b/src/libcollections/hash/mod.rs index b484b2c8128..bd7bab456ba 100644 --- a/src/libcollections/hash/mod.rs +++ b/src/libcollections/hash/mod.rs @@ -213,13 +213,6 @@ impl<'a, S: Writer, T: Hash<S>> Hash<S> for &'a mut [T] { } } -impl<S: Writer, T: Hash<S>> Hash<S> for ~[T] { - #[inline] - fn hash(&self, state: &mut S) { - self.as_slice().hash(state); - } -} - impl<S: Writer, T: Hash<S>> Hash<S> for Vec<T> { #[inline] fn hash(&self, state: &mut S) { diff --git a/src/libcollections/hash/sip.rs b/src/libcollections/hash/sip.rs index 74e93284d2a..887b0fb0b8a 100644 --- a/src/libcollections/hash/sip.rs +++ b/src/libcollections/hash/sip.rs @@ -276,6 +276,7 @@ mod tests { use str::Str; use string::String; use slice::{Vector, ImmutableVector}; + use vec::Vec; use super::super::{Hash, Writer}; use super::{SipState, hash, hash_with_keys}; @@ -376,8 +377,8 @@ mod tests { s } - fn result_bytes(h: u64) -> ~[u8] { - box [(h >> 0) as u8, + fn result_bytes(h: u64) -> Vec<u8> { + vec![(h >> 0) as u8, (h >> 8) as u8, (h >> 16) as u8, (h >> 24) as u8, diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index 1bc56368693..865da9eff13 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -101,11 +101,8 @@ There are a number of free functions that create or take vectors, for example: use core::prelude::*; -use alloc::heap::{allocate, deallocate}; use core::cmp; -use core::finally::try_finally; use core::mem::size_of; -use core::mem::transmute; use core::mem; use core::ptr; use core::iter::{range_step, MultiplicativeIterator}; @@ -255,18 +252,18 @@ impl Iterator<(uint, uint)> for ElementSwaps { /// Generates even and odd permutations alternately. pub struct Permutations<T> { swaps: ElementSwaps, - v: ~[T], + v: Vec<T>, } -impl<T: Clone> Iterator<~[T]> for Permutations<T> { +impl<T: Clone> Iterator<Vec<T>> for Permutations<T> { #[inline] - fn next(&mut self) -> Option<~[T]> { + fn next(&mut self) -> Option<Vec<T>> { match self.swaps.next() { None => None, Some((0,0)) => Some(self.v.clone()), Some((a, b)) => { let elt = self.v.clone(); - self.v.swap(a, b); + self.v.as_mut_slice().swap(a, b); Some(elt) } } @@ -281,73 +278,20 @@ impl<T: Clone> Iterator<~[T]> for Permutations<T> { /// Extension methods for vector slices with cloneable elements pub trait CloneableVector<T> { /// Copy `self` into a new owned vector - fn to_owned(&self) -> ~[T]; + fn to_owned(&self) -> Vec<T>; /// Convert `self` into an owned vector, not making a copy if possible. - fn into_owned(self) -> ~[T]; + fn into_owned(self) -> Vec<T>; } /// Extension methods for vector slices impl<'a, T: Clone> CloneableVector<T> for &'a [T] { /// Returns a copy of `v`. #[inline] - fn to_owned(&self) -> ~[T] { - use RawVec = core::raw::Vec; - use core::num::{CheckedAdd, CheckedMul}; - use core::ptr; - - let len = self.len(); - let data_size = len.checked_mul(&mem::size_of::<T>()); - let data_size = data_size.expect("overflow in to_owned()"); - let size = mem::size_of::<RawVec<()>>().checked_add(&data_size); - let size = size.expect("overflow in to_owned()"); - - unsafe { - // this should pass the real required alignment - let ret = allocate(size, 8) as *mut RawVec<()>; - - let a_size = mem::size_of::<T>(); - let a_size = if a_size == 0 {1} else {a_size}; - (*ret).fill = len * a_size; - (*ret).alloc = len * a_size; - - // Be careful with the following loop. We want it to be optimized - // to a memcpy (or something similarly fast) when T is Copy. LLVM - // is easily confused, so any extra operations during the loop can - // prevent this optimization. - let mut i = 0; - let p = &mut (*ret).data as *mut _ as *mut T; - try_finally( - &mut i, (), - |i, ()| while *i < len { - ptr::write( - &mut(*p.offset(*i as int)), - self.unsafe_ref(*i).clone()); - *i += 1; - }, - |i| if *i < len { - // we must be failing, clean up after ourselves - for j in range(0, *i as int) { - ptr::read(&*p.offset(j)); - } - // FIXME: #13994 (should pass align and size here) - deallocate(ret as *mut u8, 0, 8); - }); - mem::transmute(ret) - } - } + fn to_owned(&self) -> Vec<T> { Vec::from_slice(*self) } #[inline(always)] - fn into_owned(self) -> ~[T] { self.to_owned() } -} - -/// Extension methods for owned vectors -impl<T: Clone> CloneableVector<T> for ~[T] { - #[inline] - fn to_owned(&self) -> ~[T] { self.clone() } - - #[inline(always)] - fn into_owned(self) -> ~[T] { self } + fn into_owned(self) -> Vec<T> { self.to_owned() } } /// Extension methods for vectors containing `Clone` elements. @@ -387,57 +331,6 @@ impl<'a,T:Clone> ImmutableCloneableVector<T> for &'a [T] { } -/// Extension methods for owned vectors. -pub trait OwnedVector<T> { - /// 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 - /// - /// ```rust - /// let v = ~["a".to_string(), "b".to_string()]; - /// for s in v.move_iter() { - /// // s has type ~str, not &~str - /// println!("{}", s); - /// } - /// ``` - fn move_iter(self) -> MoveItems<T>; - - /** - * Partitions the vector into two vectors `(A,B)`, where all - * elements of `A` satisfy `f` and all elements of `B` do not. - */ - fn partition(self, f: |&T| -> bool) -> (Vec<T>, Vec<T>); -} - -impl<T> OwnedVector<T> for ~[T] { - #[inline] - fn move_iter(self) -> MoveItems<T> { - unsafe { - let iter = transmute(self.iter()); - let ptr = transmute(self); - MoveItems { allocation: ptr, iter: iter } - } - } - - #[inline] - fn partition(self, f: |&T| -> bool) -> (Vec<T>, Vec<T>) { - let mut lefts = Vec::new(); - let mut rights = Vec::new(); - - for elt in self.move_iter() { - if f(&elt) { - lefts.push(elt); - } else { - rights.push(elt); - } - } - - (lefts, rights) - } -} - fn insertion_sort<T>(v: &mut [T], compare: |&T, &T| -> Ordering) { let len = v.len() as int; let buf_v = v.as_mut_ptr(); @@ -676,7 +569,7 @@ pub trait MutableVectorAllocating<'a, T> { * * start - The index into `src` to start copying from * * end - The index into `str` to stop copying from */ - fn move_from(self, src: ~[T], start: uint, end: uint) -> uint; + fn move_from(self, src: Vec<T>, start: uint, end: uint) -> uint; } impl<'a,T> MutableVectorAllocating<'a, T> for &'a mut [T] { @@ -686,7 +579,7 @@ impl<'a,T> MutableVectorAllocating<'a, T> for &'a mut [T] { } #[inline] - fn move_from(self, mut src: ~[T], start: uint, end: uint) -> uint { + fn move_from(self, mut src: Vec<T>, start: uint, end: uint) -> uint { for (a, b) in self.mut_iter().zip(src.mut_slice(start, end).mut_iter()) { mem::swap(a, b); } @@ -815,47 +708,6 @@ pub mod raw { pub use core::slice::raw::{shift_ptr, pop_ptr}; } -/// An iterator that moves out of a vector. -pub struct MoveItems<T> { - allocation: *mut u8, // the block of memory allocated for the vector - iter: Items<'static, T> -} - -impl<T> Iterator<T> for MoveItems<T> { - #[inline] - fn next(&mut self) -> Option<T> { - unsafe { - self.iter.next().map(|x| ptr::read(x)) - } - } - - #[inline] - fn size_hint(&self) -> (uint, Option<uint>) { - self.iter.size_hint() - } -} - -impl<T> DoubleEndedIterator<T> for MoveItems<T> { - #[inline] - fn next_back(&mut self) -> Option<T> { - unsafe { - self.iter.next_back().map(|x| ptr::read(x)) - } - } -} - -#[unsafe_destructor] -impl<T> Drop for MoveItems<T> { - fn drop(&mut self) { - // destroy the remaining elements - for _x in *self {} - unsafe { - // FIXME: #13994 (should pass align and size here) - deallocate(self.allocation, 0, 8) - } - } -} - #[cfg(test)] mod tests { use std::cell::Cell; @@ -944,92 +796,92 @@ mod tests { #[test] fn test_get() { - let mut a = box [11]; - assert_eq!(a.get(1), None); - a = box [11, 12]; - assert_eq!(a.get(1).unwrap(), &12); - a = box [11, 12, 13]; - assert_eq!(a.get(1).unwrap(), &12); + let mut a = vec![11]; + assert_eq!(a.as_slice().get(1), None); + a = vec![11, 12]; + assert_eq!(a.as_slice().get(1).unwrap(), &12); + a = vec![11, 12, 13]; + assert_eq!(a.as_slice().get(1).unwrap(), &12); } #[test] fn test_head() { - let mut a = box []; - assert_eq!(a.head(), None); - a = box [11]; - assert_eq!(a.head().unwrap(), &11); - a = box [11, 12]; - assert_eq!(a.head().unwrap(), &11); + let mut a = vec![]; + assert_eq!(a.as_slice().head(), None); + a = vec![11]; + assert_eq!(a.as_slice().head().unwrap(), &11); + a = vec![11, 12]; + assert_eq!(a.as_slice().head().unwrap(), &11); } #[test] fn test_tail() { - let mut a = box [11]; + let mut a = vec![11]; assert_eq!(a.tail(), &[]); - a = box [11, 12]; + a = vec![11, 12]; assert_eq!(a.tail(), &[12]); } #[test] #[should_fail] fn test_tail_empty() { - let a: ~[int] = box []; + let a: Vec<int> = vec![]; a.tail(); } #[test] fn test_tailn() { - let mut a = box [11, 12, 13]; + let mut a = vec![11, 12, 13]; assert_eq!(a.tailn(0), &[11, 12, 13]); - a = box [11, 12, 13]; + a = vec![11, 12, 13]; assert_eq!(a.tailn(2), &[13]); } #[test] #[should_fail] fn test_tailn_empty() { - let a: ~[int] = box []; + let a: Vec<int> = vec![]; a.tailn(2); } #[test] fn test_init() { - let mut a = box [11]; + let mut a = vec![11]; assert_eq!(a.init(), &[]); - a = box [11, 12]; + a = vec![11, 12]; assert_eq!(a.init(), &[11]); } #[test] #[should_fail] fn test_init_empty() { - let a: ~[int] = box []; + let a: Vec<int> = vec![]; a.init(); } #[test] fn test_initn() { - let mut a = box [11, 12, 13]; - assert_eq!(a.initn(0), &[11, 12, 13]); - a = box [11, 12, 13]; - assert_eq!(a.initn(2), &[11]); + let mut a = vec![11, 12, 13]; + assert_eq!(a.as_slice().initn(0), &[11, 12, 13]); + a = vec![11, 12, 13]; + assert_eq!(a.as_slice().initn(2), &[11]); } #[test] #[should_fail] fn test_initn_empty() { - let a: ~[int] = box []; - a.initn(2); + let a: Vec<int> = vec![]; + a.as_slice().initn(2); } #[test] fn test_last() { - let mut a = box []; - assert_eq!(a.last(), None); - a = box [11]; - assert_eq!(a.last().unwrap(), &11); - a = box [11, 12]; - assert_eq!(a.last().unwrap(), &12); + let mut a = vec![]; + assert_eq!(a.as_slice().last(), None); + a = vec![11]; + assert_eq!(a.as_slice().last().unwrap(), &11); + a = vec![11, 12]; + assert_eq!(a.as_slice().last().unwrap(), &12); } #[test] @@ -1038,6 +890,7 @@ mod tests { let vec_fixed = [1, 2, 3, 4]; let v_a = vec_fixed.slice(1u, vec_fixed.len()).to_owned(); assert_eq!(v_a.len(), 3u); + let v_a = v_a.as_slice(); assert_eq!(v_a[0], 2); assert_eq!(v_a[1], 3); assert_eq!(v_a[2], 4); @@ -1046,13 +899,15 @@ mod tests { let vec_stack = &[1, 2, 3]; let v_b = vec_stack.slice(1u, 3u).to_owned(); assert_eq!(v_b.len(), 2u); + let v_b = v_b.as_slice(); assert_eq!(v_b[0], 2); assert_eq!(v_b[1], 3); // Test `Box<[T]>` - let vec_unique = box [1, 2, 3, 4, 5, 6]; + let vec_unique = vec![1, 2, 3, 4, 5, 6]; let v_d = vec_unique.slice(1u, 6u).to_owned(); assert_eq!(v_d.len(), 5u); + let v_d = v_d.as_slice(); assert_eq!(v_d[0], 2); assert_eq!(v_d[1], 3); assert_eq!(v_d[2], 4); @@ -1295,15 +1150,15 @@ mod tests { let (min_size, max_opt) = it.size_hint(); assert_eq!(min_size, 3*2); assert_eq!(max_opt.unwrap(), 3*2); - assert_eq!(it.next(), Some(box [1,2,3])); - assert_eq!(it.next(), Some(box [1,3,2])); - assert_eq!(it.next(), Some(box [3,1,2])); + assert_eq!(it.next(), Some(vec![1,2,3])); + assert_eq!(it.next(), Some(vec![1,3,2])); + assert_eq!(it.next(), Some(vec![3,1,2])); let (min_size, max_opt) = it.size_hint(); assert_eq!(min_size, 3); assert_eq!(max_opt.unwrap(), 3); - assert_eq!(it.next(), Some(box [3,2,1])); - assert_eq!(it.next(), Some(box [2,3,1])); - assert_eq!(it.next(), Some(box [2,1,3])); + assert_eq!(it.next(), Some(vec![3,2,1])); + assert_eq!(it.next(), Some(vec![2,3,1])); + assert_eq!(it.next(), Some(vec![2,1,3])); assert_eq!(it.next(), None); } { @@ -1378,11 +1233,11 @@ mod tests { fn test_position_elem() { assert!([].position_elem(&1).is_none()); - let v1 = box [1, 2, 3, 3, 2, 5]; - assert_eq!(v1.position_elem(&1), Some(0u)); - assert_eq!(v1.position_elem(&2), Some(1u)); - assert_eq!(v1.position_elem(&5), Some(5u)); - assert!(v1.position_elem(&4).is_none()); + let v1 = vec![1, 2, 3, 3, 2, 5]; + assert_eq!(v1.as_slice().position_elem(&1), Some(0u)); + assert_eq!(v1.as_slice().position_elem(&2), Some(1u)); + assert_eq!(v1.as_slice().position_elem(&5), Some(5u)); + assert!(v1.as_slice().position_elem(&4).is_none()); } #[test] @@ -1432,14 +1287,14 @@ mod tests { #[test] fn test_reverse() { - let mut v: ~[int] = box [10, 20]; - assert_eq!(v[0], 10); - assert_eq!(v[1], 20); + let mut v: Vec<int> = vec![10, 20]; + assert_eq!(*v.get(0), 10); + assert_eq!(*v.get(1), 20); v.reverse(); - assert_eq!(v[0], 20); - assert_eq!(v[1], 10); + assert_eq!(*v.get(0), 20); + assert_eq!(*v.get(1), 10); - let mut v3: ~[int] = box []; + let mut v3: Vec<int> = vec![]; v3.reverse(); assert!(v3.is_empty()); } @@ -1505,10 +1360,10 @@ mod tests { #[test] fn test_partition() { - assert_eq!((box []).partition(|x: &int| *x < 3), (vec![], vec![])); - assert_eq!((box [1, 2, 3]).partition(|x: &int| *x < 4), (vec![1, 2, 3], vec![])); - assert_eq!((box [1, 2, 3]).partition(|x: &int| *x < 2), (vec![1], vec![2, 3])); - assert_eq!((box [1, 2, 3]).partition(|x: &int| *x < 0), (vec![], vec![1, 2, 3])); + assert_eq!((vec![]).partition(|x: &int| *x < 3), (vec![], vec![])); + assert_eq!((vec![1, 2, 3]).partition(|x: &int| *x < 4), (vec![1, 2, 3], vec![])); + assert_eq!((vec![1, 2, 3]).partition(|x: &int| *x < 2), (vec![1], vec![2, 3])); + assert_eq!((vec![1, 2, 3]).partition(|x: &int| *x < 0), (vec![], vec![1, 2, 3])); } #[test] @@ -1521,19 +1376,19 @@ mod tests { #[test] fn test_concat() { - let v: [~[int], ..0] = []; + let v: [Vec<int>, ..0] = []; assert_eq!(v.concat_vec(), vec![]); - assert_eq!([box [1], box [2,3]].concat_vec(), vec![1, 2, 3]); + assert_eq!([vec![1], vec![2,3]].concat_vec(), vec![1, 2, 3]); assert_eq!([&[1], &[2,3]].concat_vec(), vec![1, 2, 3]); } #[test] fn test_connect() { - let v: [~[int], ..0] = []; + let v: [Vec<int>, ..0] = []; assert_eq!(v.connect_vec(&0), vec![]); - assert_eq!([box [1], box [2, 3]].connect_vec(&0), vec![1, 0, 2, 3]); - assert_eq!([box [1], box [2], box [3]].connect_vec(&0), vec![1, 0, 2, 0, 3]); + assert_eq!([vec![1], vec![2, 3]].connect_vec(&0), vec![1, 0, 2, 3]); + assert_eq!([vec![1], vec![2], vec![3]].connect_vec(&0), vec![1, 0, 2, 0, 3]); assert_eq!([&[1], &[2, 3]].connect_vec(&0), vec![1, 0, 2, 3]); assert_eq!([&[1], &[2], &[3]].connect_vec(&0), vec![1, 0, 2, 0, 3]); @@ -1808,13 +1663,13 @@ mod tests { #[test] fn test_move_iterator() { - let xs = box [1u,2,3,4,5]; + let xs = vec![1u,2,3,4,5]; assert_eq!(xs.move_iter().fold(0, |a: uint, b: uint| 10*a + b), 12345); } #[test] fn test_move_rev_iterator() { - let xs = box [1u,2,3,4,5]; + let xs = vec![1u,2,3,4,5]; assert_eq!(xs.move_iter().rev().fold(0, |a: uint, b: uint| 10*a + b), 54321); } @@ -1927,19 +1782,19 @@ mod tests { #[test] fn test_move_from() { let mut a = [1,2,3,4,5]; - let b = box [6,7,8]; + let b = vec![6,7,8]; assert_eq!(a.move_from(b, 0, 3), 3); assert!(a == [6,7,8,4,5]); let mut a = [7,2,8,1]; - let b = box [3,1,4,1,5,9]; + let b = vec![3,1,4,1,5,9]; assert_eq!(a.move_from(b, 0, 6), 4); assert!(a == [3,1,4,1]); let mut a = [1,2,3,4]; - let b = box [5,6,7,8,9,0]; + let b = vec![5,6,7,8,9,0]; assert_eq!(a.move_from(b, 2, 3), 1); assert!(a == [7,2,3,4]); let mut a = [1,2,3,4,5]; - let b = box [5,6,7,8,9,0]; + let b = vec![5,6,7,8,9,0]; assert_eq!(a.mut_slice(2,4).move_from(b,1,6), 2); assert!(a == [1,2,6,7,5]); } @@ -1972,11 +1827,11 @@ mod tests { assert_eq!(format!("{}", x.as_slice()), x_str); }) ) - let empty: ~[int] = box []; + let empty: Vec<int> = vec![]; test_show_vec!(empty, "[]".to_string()); - test_show_vec!(box [1], "[1]".to_string()); - test_show_vec!(box [1, 2, 3], "[1, 2, 3]".to_string()); - test_show_vec!(box [box [], box [1u], box [1u, 1u]], + test_show_vec!(vec![1], "[1]".to_string()); + test_show_vec!(vec![1, 2, 3], "[1, 2, 3]".to_string()); + test_show_vec!(vec![vec![], vec![1u], vec![1u, 1u]], "[[], [1], [1, 1]]".to_string()); let empty_mut: &mut [int] = &mut[]; @@ -1997,7 +1852,6 @@ mod tests { ); t!(&[int]); - t!(~[int]); t!(Vec<int>); } @@ -2393,13 +2247,6 @@ mod bench { } #[bench] - fn zero_1kb_fixed_repeat(b: &mut Bencher) { - b.iter(|| { - box [0u8, ..1024] - }); - } - - #[bench] fn zero_1kb_loop_set(b: &mut Bencher) { b.iter(|| { let mut v: Vec<uint> = Vec::with_capacity(1024); diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs index 49d8775dd9c..83601be83de 100644 --- a/src/libcollections/str.rs +++ b/src/libcollections/str.rs @@ -707,7 +707,7 @@ pub mod raw { use str::StrAllocating; unsafe { - let a = ~[65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8]; + let a = vec![65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8]; let b = a.as_ptr(); let c = from_buf_len(b, 3u); assert_eq!(c, "AAA".to_string()); @@ -1124,7 +1124,7 @@ mod tests { assert!(half_a_million_letter_a() == unsafe {raw::slice_bytes(letters.as_slice(), 0u, - 500000)}.to_owned()); + 500000)}.to_string()); } #[test] @@ -1219,7 +1219,7 @@ mod tests { assert_eq!("", data.slice(3, 3)); assert_eq!("华", data.slice(30, 33)); - fn a_million_letter_X() -> String { + fn a_million_letter_x() -> String { let mut i = 0; let mut rs = String::new(); while i < 100000 { @@ -1228,7 +1228,7 @@ mod tests { } rs } - fn half_a_million_letter_X() -> String { + fn half_a_million_letter_x() -> String { let mut i = 0; let mut rs = String::new(); while i < 100000 { @@ -1237,9 +1237,9 @@ mod tests { } rs } - let letters = a_million_letter_X(); - assert!(half_a_million_letter_X() == - letters.as_slice().slice(0u, 3u * 500000u).to_owned()); + let letters = a_million_letter_x(); + assert!(half_a_million_letter_x() == + letters.as_slice().slice(0u, 3u * 500000u).to_string()); } #[test] @@ -1464,7 +1464,7 @@ mod tests { #[test] fn test_raw_from_c_str() { unsafe { - let a = box [65, 65, 65, 65, 65, 65, 65, 0]; + let a = vec![65, 65, 65, 65, 65, 65, 65, 0]; let b = a.as_ptr(); let c = raw::from_c_str(b); assert_eq!(c, "AAAAAAA".to_string()); @@ -1682,7 +1682,7 @@ mod tests { #[test] fn test_char_at() { let s = "ศไทย中华Việt Nam"; - let v = box ['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m']; + let v = vec!['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m']; let mut pos = 0; for ch in v.iter() { assert!(s.char_at(pos) == *ch); @@ -1693,7 +1693,7 @@ mod tests { #[test] fn test_char_at_reverse() { let s = "ศไทย中华Việt Nam"; - let v = box ['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m']; + let v = vec!['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m']; let mut pos = s.len(); for ch in v.iter().rev() { assert!(s.char_at_reverse(pos) == *ch); @@ -1756,7 +1756,7 @@ mod tests { #[test] fn test_iterator() { let s = "ศไทย中华Việt Nam"; - let v = box ['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m']; + let v = ['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m']; let mut pos = 0; let mut it = s.chars(); @@ -1771,7 +1771,7 @@ mod tests { #[test] fn test_rev_iterator() { let s = "ศไทย中华Việt Nam"; - let v = box ['m', 'a', 'N', ' ', 't', 'ệ','i','V','华','中','ย','ท','ไ','ศ']; + let v = ['m', 'a', 'N', ' ', 't', 'ệ','i','V','华','中','ย','ท','ไ','ศ']; let mut pos = 0; let mut it = s.chars().rev(); diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index dbef73efc47..acb7b2c2704 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -13,7 +13,6 @@ use core::prelude::*; use alloc::heap::{allocate, reallocate, deallocate}; -use RawVec = core::raw::Vec; use core::raw::Slice; use core::cmp::max; use core::default::Default; @@ -25,7 +24,7 @@ use core::ptr; use core::uint; use {Collection, Mutable}; -use slice::{MutableOrdVector, OwnedVector, MutableVectorAllocating}; +use slice::{MutableOrdVector, MutableVectorAllocating, CloneableVector}; use slice::{Items, MutItems}; /// An owned, growable vector. @@ -387,6 +386,11 @@ impl<T: PartialOrd> PartialOrd for Vec<T> { impl<T: Eq> Eq for Vec<T> {} +impl<T: PartialEq, V: Vector<T>> Equiv<V> for Vec<T> { + #[inline] + fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() } +} + impl<T: Ord> Ord for Vec<T> { #[inline] fn cmp(&self, other: &Vec<T>) -> Ordering { @@ -401,6 +405,11 @@ impl<T> Collection for Vec<T> { } } +impl<T: Clone> CloneableVector<T> for Vec<T> { + fn to_owned(&self) -> Vec<T> { self.clone() } + fn into_owned(self) -> Vec<T> { self } +} + // FIXME: #13996: need a way to mark the return value as `noalias` #[inline(never)] unsafe fn alloc_or_realloc<T>(ptr: *mut T, size: uint, old_size: uint) -> *mut T { @@ -1511,52 +1520,6 @@ pub fn unzip<T, U, V: Iterator<(T, U)>>(mut iter: V) -> (Vec<T>, Vec<U>) { (ts, us) } -/// Mechanism to convert from a `Vec<T>` to a `[T]`. -/// -/// In a post-DST world this will be used to convert to any `Ptr<[T]>`. -/// -/// This could be implemented on more types than just pointers to vectors, but -/// the recommended approach for those types is to implement `FromIterator`. -// FIXME(#12938): Update doc comment when DST lands -pub trait FromVec<T> { - /// Convert a `Vec<T>` into the receiver type. - fn from_vec(v: Vec<T>) -> Self; -} - -impl<T> FromVec<T> for ~[T] { - fn from_vec(mut v: Vec<T>) -> ~[T] { - let len = v.len(); - let data_size = len.checked_mul(&mem::size_of::<T>()); - let data_size = data_size.expect("overflow in from_vec()"); - let size = mem::size_of::<RawVec<()>>().checked_add(&data_size); - let size = size.expect("overflow in from_vec()"); - - // In a post-DST world, we can attempt to reuse the Vec allocation by calling - // shrink_to_fit() on it. That may involve a reallocation+memcpy, but that's no - // different than what we're doing manually here. - - let vp = v.as_mut_ptr(); - - unsafe { - let ret = allocate(size, 8) as *mut RawVec<()>; - - let a_size = mem::size_of::<T>(); - let a_size = if a_size == 0 {1} else {a_size}; - (*ret).fill = len * a_size; - (*ret).alloc = len * a_size; - - ptr::copy_nonoverlapping_memory(&mut (*ret).data as *mut _ as *mut u8, - vp as *u8, data_size); - - // we've transferred ownership of the contents from v, but we can't drop it - // as it still needs to free its own allocation. - v.set_len(0); - - mem::transmute(ret) - } - } -} - /// Unsafe operations pub mod raw { use super::Vec; @@ -1580,8 +1543,7 @@ pub mod raw { mod tests { use std::prelude::*; use std::mem::size_of; - use std::kinds::marker; - use super::{unzip, raw, FromVec, Vec}; + use super::{unzip, raw, Vec}; #[test] fn test_small_vec_struct() { @@ -1830,7 +1792,7 @@ mod tests { assert_eq!(b, vec![1, 2, 3]); // Test on-heap copy-from-buf. - let c = box [1, 2, 3, 4, 5]; + let c = vec![1, 2, 3, 4, 5]; let ptr = c.as_ptr(); let d = raw::from_buf(ptr, 5u); assert_eq!(d, vec![1, 2, 3, 4, 5]); @@ -1838,32 +1800,6 @@ mod tests { } #[test] - fn test_from_vec() { - let a = vec![1u, 2, 3]; - let b: ~[uint] = FromVec::from_vec(a); - assert_eq!(b.as_slice(), &[1u, 2, 3]); - - let a = vec![]; - let b: ~[u8] = FromVec::from_vec(a); - assert_eq!(b.as_slice(), &[]); - - let a = vec!["one".to_string(), "two".to_string()]; - let b: ~[String] = FromVec::from_vec(a); - assert_eq!(b.as_slice(), &["one".to_string(), "two".to_string()]); - - struct Foo { - x: uint, - nocopy: marker::NoCopy - } - - let a = vec![Foo{x: 42, nocopy: marker::NoCopy}, Foo{x: 84, nocopy: marker::NoCopy}]; - let b: ~[Foo] = FromVec::from_vec(a); - assert_eq!(b.len(), 2); - assert_eq!(b[0].x, 42); - assert_eq!(b[1].x, 84); - } - - #[test] fn test_vec_truncate_drop() { static mut drops: uint = 0; struct Elem(int); |
