diff options
| author | bors <bors@rust-lang.org> | 2018-08-14 15:09:37 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-08-14 15:09:37 +0000 |
| commit | 23f09bbed4ef12c5f9db198c22f50b608ea6c6d5 (patch) | |
| tree | 547c1cea1a62ace01f8cef6c8120612596208083 /src/libsyntax/util | |
| parent | f45f52532a394d2d607fc5693364ad820049376d (diff) | |
| parent | e5e6375352636360add297c1f5a1f37ce71506e9 (diff) | |
| download | rust-23f09bbed4ef12c5f9db198c22f50b608ea6c6d5.tar.gz rust-23f09bbed4ef12c5f9db198c22f50b608ea6c6d5.zip | |
Auto merge of #53085 - ljedrz:cleanup_syntax_structures, r=ljedrz
Move SmallVector and ThinVec out of libsyntax - move `libsyntax::util::SmallVector` tests to `librustc_data_structures::small_vec` - remove `libsyntax::util::SmallVector` - move `libsyntax::util::thin_vec` to `librustc_data_structures::thin_vec` Other than moving these data structures where they belong it allows modules using `SmallVector<T>` (`SmallVec<[T; 1]>`) to specify their own length (e.g. 8 or 32) independently from `libsyntax`.
Diffstat (limited to 'src/libsyntax/util')
| -rw-r--r-- | src/libsyntax/util/move_map.rs | 5 | ||||
| -rw-r--r-- | src/libsyntax/util/small_vector.rs | 81 | ||||
| -rw-r--r-- | src/libsyntax/util/thin_vec.rs | 59 |
3 files changed, 2 insertions, 143 deletions
diff --git a/src/libsyntax/util/move_map.rs b/src/libsyntax/util/move_map.rs index 8cc37afa354..eb2c5a2458c 100644 --- a/src/libsyntax/util/move_map.rs +++ b/src/libsyntax/util/move_map.rs @@ -9,8 +9,7 @@ // except according to those terms. use std::ptr; - -use util::small_vector::SmallVector; +use OneVector; pub trait MoveMap<T>: Sized { fn move_map<F>(self, mut f: F) -> Self where F: FnMut(T) -> T { @@ -78,7 +77,7 @@ impl<T> MoveMap<T> for ::ptr::P<[T]> { } } -impl<T> MoveMap<T> for SmallVector<T> { +impl<T> MoveMap<T> for OneVector<T> { fn move_flat_map<F, I>(mut self, mut f: F) -> Self where F: FnMut(T) -> I, I: IntoIterator<Item=T> diff --git a/src/libsyntax/util/small_vector.rs b/src/libsyntax/util/small_vector.rs deleted file mode 100644 index 31e675836fc..00000000000 --- a/src/libsyntax/util/small_vector.rs +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright 2013-2014 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 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use rustc_data_structures::small_vec::SmallVec; - -pub type SmallVector<T> = SmallVec<[T; 1]>; - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_len() { - let v: SmallVector<isize> = SmallVector::new(); - assert_eq!(0, v.len()); - - assert_eq!(1, SmallVector::one(1).len()); - assert_eq!(5, SmallVector::many(vec![1, 2, 3, 4, 5]).len()); - } - - #[test] - fn test_push_get() { - let mut v = SmallVector::new(); - v.push(1); - assert_eq!(1, v.len()); - assert_eq!(1, v[0]); - v.push(2); - assert_eq!(2, v.len()); - assert_eq!(2, v[1]); - v.push(3); - assert_eq!(3, v.len()); - assert_eq!(3, v[2]); - } - - #[test] - fn test_from_iter() { - let v: SmallVector<isize> = (vec![1, 2, 3]).into_iter().collect(); - assert_eq!(3, v.len()); - assert_eq!(1, v[0]); - assert_eq!(2, v[1]); - assert_eq!(3, v[2]); - } - - #[test] - fn test_move_iter() { - let v = SmallVector::new(); - let v: Vec<isize> = v.into_iter().collect(); - assert_eq!(v, Vec::new()); - - let v = SmallVector::one(1); - assert_eq!(v.into_iter().collect::<Vec<_>>(), [1]); - - let v = SmallVector::many(vec![1, 2, 3]); - assert_eq!(v.into_iter().collect::<Vec<_>>(), [1, 2, 3]); - } - - #[test] - #[should_panic] - fn test_expect_one_zero() { - let _: isize = SmallVector::new().expect_one(""); - } - - #[test] - #[should_panic] - fn test_expect_one_many() { - SmallVector::many(vec![1, 2]).expect_one(""); - } - - #[test] - fn test_expect_one_one() { - assert_eq!(1, SmallVector::one(1).expect_one("")); - assert_eq!(1, SmallVector::many(vec![1]).expect_one("")); - } -} diff --git a/src/libsyntax/util/thin_vec.rs b/src/libsyntax/util/thin_vec.rs deleted file mode 100644 index 546686b46b8..00000000000 --- a/src/libsyntax/util/thin_vec.rs +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright 2016 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 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -/// A vector type optimized for cases where this size is usually 0 (c.f. `SmallVector`). -/// The `Option<Box<..>>` wrapping allows us to represent a zero sized vector with `None`, -/// which uses only a single (null) pointer. -#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] -pub struct ThinVec<T>(Option<Box<Vec<T>>>); - -impl<T> ThinVec<T> { - pub fn new() -> Self { - ThinVec(None) - } -} - -impl<T> From<Vec<T>> for ThinVec<T> { - fn from(vec: Vec<T>) -> Self { - if vec.is_empty() { - ThinVec(None) - } else { - ThinVec(Some(Box::new(vec))) - } - } -} - -impl<T> Into<Vec<T>> for ThinVec<T> { - fn into(self) -> Vec<T> { - match self { - ThinVec(None) => Vec::new(), - ThinVec(Some(vec)) => *vec, - } - } -} - -impl<T> ::std::ops::Deref for ThinVec<T> { - type Target = [T]; - fn deref(&self) -> &[T] { - match *self { - ThinVec(None) => &[], - ThinVec(Some(ref vec)) => vec, - } - } -} - -impl<T> Extend<T> for ThinVec<T> { - fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) { - match *self { - ThinVec(Some(ref mut vec)) => vec.extend(iter), - ThinVec(None) => *self = iter.into_iter().collect::<Vec<_>>().into(), - } - } -} |
