Skip to content

Commit 7235e7d

Browse files
committed
Move SmallVec and ThinVec out of libsyntax
1 parent 4b8089d commit 7235e7d

35 files changed

+245
-245
lines changed

src/Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -2140,6 +2140,7 @@ version = "0.0.0"
21402140
dependencies = [
21412141
"log 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
21422142
"rustc 0.0.0",
2143+
"rustc_data_structures 0.0.0",
21432144
"rustc_errors 0.0.0",
21442145
"rustc_target 0.0.0",
21452146
"syntax 0.0.0",

src/librustc/hir/lowering.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ use lint::builtin::{self, PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
5151
ELIDED_LIFETIMES_IN_PATHS};
5252
use middle::cstore::CrateStore;
5353
use rustc_data_structures::indexed_vec::IndexVec;
54+
use rustc_data_structures::small_vec::OneVector;
55+
use rustc_data_structures::thin_vec::ThinVec;
5456
use session::Session;
5557
use util::common::FN_OUTPUT_NAME;
5658
use util::nodemap::{DefIdMap, NodeMap};
@@ -71,7 +73,6 @@ use syntax::std_inject;
7173
use syntax::symbol::{keywords, Symbol};
7274
use syntax::tokenstream::{Delimited, TokenStream, TokenTree};
7375
use syntax::parse::token::Token;
74-
use syntax::util::small_vector::SmallVector;
7576
use syntax::visit::{self, Visitor};
7677
use syntax_pos::{Span, MultiSpan};
7778

@@ -3122,12 +3123,12 @@ impl<'a> LoweringContext<'a> {
31223123
&mut self,
31233124
decl: &FnDecl,
31243125
header: &FnHeader,
3125-
ids: &mut SmallVector<hir::ItemId>,
3126+
ids: &mut OneVector<hir::ItemId>,
31263127
) {
31273128
if let Some(id) = header.asyncness.opt_return_id() {
31283129
ids.push(hir::ItemId { id });
31293130
}
3130-
struct IdVisitor<'a> { ids: &'a mut SmallVector<hir::ItemId> }
3131+
struct IdVisitor<'a> { ids: &'a mut OneVector<hir::ItemId> }
31313132
impl<'a, 'b> Visitor<'a> for IdVisitor<'b> {
31323133
fn visit_ty(&mut self, ty: &'a Ty) {
31333134
match ty.node {
@@ -3160,36 +3161,36 @@ impl<'a> LoweringContext<'a> {
31603161
}
31613162
}
31623163

3163-
fn lower_item_id(&mut self, i: &Item) -> SmallVector<hir::ItemId> {
3164+
fn lower_item_id(&mut self, i: &Item) -> OneVector<hir::ItemId> {
31643165
match i.node {
31653166
ItemKind::Use(ref use_tree) => {
3166-
let mut vec = SmallVector::one(hir::ItemId { id: i.id });
3167+
let mut vec = OneVector::one(hir::ItemId { id: i.id });
31673168
self.lower_item_id_use_tree(use_tree, i.id, &mut vec);
31683169
vec
31693170
}
3170-
ItemKind::MacroDef(..) => SmallVector::new(),
3171+
ItemKind::MacroDef(..) => OneVector::new(),
31713172
ItemKind::Fn(ref decl, ref header, ..) => {
3172-
let mut ids = SmallVector::one(hir::ItemId { id: i.id });
3173+
let mut ids = OneVector::one(hir::ItemId { id: i.id });
31733174
self.lower_impl_trait_ids(decl, header, &mut ids);
31743175
ids
31753176
},
31763177
ItemKind::Impl(.., None, _, ref items) => {
3177-
let mut ids = SmallVector::one(hir::ItemId { id: i.id });
3178+
let mut ids = OneVector::one(hir::ItemId { id: i.id });
31783179
for item in items {
31793180
if let ImplItemKind::Method(ref sig, _) = item.node {
31803181
self.lower_impl_trait_ids(&sig.decl, &sig.header, &mut ids);
31813182
}
31823183
}
31833184
ids
31843185
},
3185-
_ => SmallVector::one(hir::ItemId { id: i.id }),
3186+
_ => OneVector::one(hir::ItemId { id: i.id }),
31863187
}
31873188
}
31883189

31893190
fn lower_item_id_use_tree(&mut self,
31903191
tree: &UseTree,
31913192
base_id: NodeId,
3192-
vec: &mut SmallVector<hir::ItemId>)
3193+
vec: &mut OneVector<hir::ItemId>)
31933194
{
31943195
match tree.kind {
31953196
UseTreeKind::Nested(ref nested_vec) => for &(ref nested, id) in nested_vec {
@@ -4281,8 +4282,8 @@ impl<'a> LoweringContext<'a> {
42814282
}
42824283
}
42834284

4284-
fn lower_stmt(&mut self, s: &Stmt) -> SmallVector<hir::Stmt> {
4285-
SmallVector::one(match s.node {
4285+
fn lower_stmt(&mut self, s: &Stmt) -> OneVector<hir::Stmt> {
4286+
OneVector::one(match s.node {
42864287
StmtKind::Local(ref l) => Spanned {
42874288
node: hir::StmtKind::Decl(
42884289
P(Spanned {

src/librustc/hir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ use syntax::ext::hygiene::SyntaxContext;
3333
use syntax::ptr::P;
3434
use syntax::symbol::{Symbol, keywords};
3535
use syntax::tokenstream::TokenStream;
36-
use syntax::util::ThinVec;
3736
use syntax::util::parser::ExprPrecedence;
3837
use ty::AdtKind;
3938
use ty::query::Providers;
4039

4140
use rustc_data_structures::indexed_vec;
4241
use rustc_data_structures::sync::{ParallelIterator, par_iter, Send, Sync, scope};
42+
use rustc_data_structures::thin_vec::ThinVec;
4343

4444
use serialize::{self, Encoder, Encodable, Decoder, Decodable};
4545
use std::collections::BTreeMap;

src/librustc_allocator/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ test = false
1010

1111
[dependencies]
1212
rustc = { path = "../librustc" }
13+
rustc_data_structures = { path = "../librustc_data_structures" }
1314
rustc_errors = { path = "../librustc_errors" }
1415
rustc_target = { path = "../librustc_target" }
1516
syntax = { path = "../libsyntax" }

src/librustc_allocator/expand.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
use rustc::middle::allocator::AllocatorKind;
12+
use rustc_data_structures::small_vec::OneVector;
1213
use rustc_errors;
1314
use syntax::{
1415
ast::{
@@ -28,8 +29,7 @@ use syntax::{
2829
fold::{self, Folder},
2930
parse::ParseSess,
3031
ptr::P,
31-
symbol::Symbol,
32-
util::small_vector::SmallVector,
32+
symbol::Symbol
3333
};
3434
use syntax_pos::Span;
3535

@@ -65,7 +65,7 @@ struct ExpandAllocatorDirectives<'a> {
6565
}
6666

6767
impl<'a> Folder for ExpandAllocatorDirectives<'a> {
68-
fn fold_item(&mut self, item: P<Item>) -> SmallVector<P<Item>> {
68+
fn fold_item(&mut self, item: P<Item>) -> OneVector<P<Item>> {
6969
debug!("in submodule {}", self.in_submod);
7070

7171
let name = if attr::contains_name(&item.attrs, "global_allocator") {
@@ -78,20 +78,20 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
7878
_ => {
7979
self.handler
8080
.span_err(item.span, "allocators must be statics");
81-
return SmallVector::one(item);
81+
return OneVector::one(item);
8282
}
8383
}
8484

8585
if self.in_submod > 0 {
8686
self.handler
8787
.span_err(item.span, "`global_allocator` cannot be used in submodules");
88-
return SmallVector::one(item);
88+
return OneVector::one(item);
8989
}
9090

9191
if self.found {
9292
self.handler
9393
.span_err(item.span, "cannot define more than one #[global_allocator]");
94-
return SmallVector::one(item);
94+
return OneVector::one(item);
9595
}
9696
self.found = true;
9797

@@ -152,7 +152,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
152152
let module = f.cx.monotonic_expander().fold_item(module).pop().unwrap();
153153

154154
// Return the item and new submodule
155-
let mut ret = SmallVector::with_capacity(2);
155+
let mut ret = OneVector::with_capacity(2);
156156
ret.push(item);
157157
ret.push(module);
158158

src/librustc_allocator/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#[macro_use] extern crate log;
1414
extern crate rustc;
15+
extern crate rustc_data_structures;
1516
extern crate rustc_errors;
1617
extern crate rustc_target;
1718
extern crate syntax;

src/librustc_data_structures/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pub mod sorted_map;
7575
pub mod stable_hasher;
7676
pub mod sync;
7777
pub mod tiny_list;
78+
pub mod thin_vec;
7879
pub mod transitive_relation;
7980
pub mod tuple_slice;
8081
pub use ena::unify;

src/librustc_data_structures/small_vec.rs

+65
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ use array_vec::Array;
2929

3030
pub struct SmallVec<A: Array>(AccumulateVec<A>);
3131

32+
pub type OneVector<T> = SmallVec<[T; 1]>;
33+
3234
impl<A> Clone for SmallVec<A>
3335
where A: Array,
3436
A::Element: Clone {
@@ -222,6 +224,69 @@ mod tests {
222224

223225
use super::*;
224226

227+
#[test]
228+
fn test_len() {
229+
let v: OneVector<isize> = OneVector::new();
230+
assert_eq!(0, v.len());
231+
232+
assert_eq!(1, OneVector::one(1).len());
233+
assert_eq!(5, OneVector::many(vec![1, 2, 3, 4, 5]).len());
234+
}
235+
236+
#[test]
237+
fn test_push_get() {
238+
let mut v = OneVector::new();
239+
v.push(1);
240+
assert_eq!(1, v.len());
241+
assert_eq!(1, v[0]);
242+
v.push(2);
243+
assert_eq!(2, v.len());
244+
assert_eq!(2, v[1]);
245+
v.push(3);
246+
assert_eq!(3, v.len());
247+
assert_eq!(3, v[2]);
248+
}
249+
250+
#[test]
251+
fn test_from_iter() {
252+
let v: OneVector<isize> = (vec![1, 2, 3]).into_iter().collect();
253+
assert_eq!(3, v.len());
254+
assert_eq!(1, v[0]);
255+
assert_eq!(2, v[1]);
256+
assert_eq!(3, v[2]);
257+
}
258+
259+
#[test]
260+
fn test_move_iter() {
261+
let v = OneVector::new();
262+
let v: Vec<isize> = v.into_iter().collect();
263+
assert_eq!(v, Vec::new());
264+
265+
let v = OneVector::one(1);
266+
assert_eq!(v.into_iter().collect::<Vec<_>>(), [1]);
267+
268+
let v = OneVector::many(vec![1, 2, 3]);
269+
assert_eq!(v.into_iter().collect::<Vec<_>>(), [1, 2, 3]);
270+
}
271+
272+
#[test]
273+
#[should_panic]
274+
fn test_expect_one_zero() {
275+
let _: isize = OneVector::new().expect_one("");
276+
}
277+
278+
#[test]
279+
#[should_panic]
280+
fn test_expect_one_many() {
281+
OneVector::many(vec![1, 2]).expect_one("");
282+
}
283+
284+
#[test]
285+
fn test_expect_one_one() {
286+
assert_eq!(1, OneVector::one(1).expect_one(""));
287+
assert_eq!(1, OneVector::many(vec![1]).expect_one(""));
288+
}
289+
225290
#[bench]
226291
fn fill_small_vec_1_10_with_cap(b: &mut Bencher) {
227292
b.iter(|| {

src/librustc_driver/pretty.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ use rustc::session::Session;
2424
use rustc::session::config::{Input, OutputFilenames};
2525
use rustc_borrowck as borrowck;
2626
use rustc_borrowck::graphviz as borrowck_dot;
27+
use rustc_data_structures::small_vec::OneVector;
28+
use rustc_data_structures::thin_vec::ThinVec;
2729
use rustc_metadata::cstore::CStore;
2830

2931
use rustc_mir::util::{write_mir_pretty, write_mir_graphviz};
@@ -33,8 +35,6 @@ use syntax::fold::{self, Folder};
3335
use syntax::print::{pprust};
3436
use syntax::print::pprust::PrintState;
3537
use syntax::ptr::P;
36-
use syntax::util::ThinVec;
37-
use syntax::util::small_vector::SmallVector;
3838
use syntax_pos::{self, FileName};
3939

4040
use graphviz as dot;
@@ -727,7 +727,7 @@ impl<'a> fold::Folder for ReplaceBodyWithLoop<'a> {
727727
self.run(is_const, |s| fold::noop_fold_item_kind(i, s))
728728
}
729729

730-
fn fold_trait_item(&mut self, i: ast::TraitItem) -> SmallVector<ast::TraitItem> {
730+
fn fold_trait_item(&mut self, i: ast::TraitItem) -> OneVector<ast::TraitItem> {
731731
let is_const = match i.node {
732732
ast::TraitItemKind::Const(..) => true,
733733
ast::TraitItemKind::Method(ast::MethodSig { ref decl, ref header, .. }, _) =>
@@ -737,7 +737,7 @@ impl<'a> fold::Folder for ReplaceBodyWithLoop<'a> {
737737
self.run(is_const, |s| fold::noop_fold_trait_item(i, s))
738738
}
739739

740-
fn fold_impl_item(&mut self, i: ast::ImplItem) -> SmallVector<ast::ImplItem> {
740+
fn fold_impl_item(&mut self, i: ast::ImplItem) -> OneVector<ast::ImplItem> {
741741
let is_const = match i.node {
742742
ast::ImplItemKind::Const(..) => true,
743743
ast::ImplItemKind::Method(ast::MethodSig { ref decl, ref header, .. }, _) =>
@@ -785,7 +785,7 @@ impl<'a> fold::Folder for ReplaceBodyWithLoop<'a> {
785785
node: ast::ExprKind::Loop(P(empty_block), None),
786786
id: self.sess.next_node_id(),
787787
span: syntax_pos::DUMMY_SP,
788-
attrs: ast::ThinVec::new(),
788+
attrs: ThinVec::new(),
789789
});
790790

791791
let loop_stmt = ast::Stmt {

src/libsyntax/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
pub use self::UnsafeSource::*;
1414
pub use self::GenericArgs::*;
1515
pub use symbol::{Ident, Symbol as Name};
16-
pub use util::ThinVec;
1716
pub use util::parser::ExprPrecedence;
1817

1918
use syntax_pos::{Span, DUMMY_SP};
@@ -25,6 +24,7 @@ use ptr::P;
2524
use rustc_data_structures::indexed_vec;
2625
use rustc_data_structures::indexed_vec::Idx;
2726
use symbol::{Symbol, keywords};
27+
use ThinVec;
2828
use tokenstream::{ThinTokenStream, TokenStream};
2929

3030
use serialize::{self, Encoder, Decoder};

src/libsyntax/attr/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ use parse::{self, ParseSess, PResult};
3333
use parse::token::{self, Token};
3434
use ptr::P;
3535
use symbol::Symbol;
36+
use ThinVec;
3637
use tokenstream::{TokenStream, TokenTree, Delimited};
37-
use util::ThinVec;
3838
use GLOBALS;
3939

4040
use std::iter;

src/libsyntax/config.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ use ast;
1515
use codemap::Spanned;
1616
use edition::Edition;
1717
use parse::{token, ParseSess};
18+
use OneVector;
1819

1920
use ptr::P;
20-
use util::small_vector::SmallVector;
2121

2222
/// A folder that strips out items that do not belong in the current configuration.
2323
pub struct StripUnconfigured<'a> {
@@ -319,22 +319,22 @@ impl<'a> fold::Folder for StripUnconfigured<'a> {
319319
Some(P(fold::noop_fold_expr(expr, self)))
320320
}
321321

322-
fn fold_stmt(&mut self, stmt: ast::Stmt) -> SmallVector<ast::Stmt> {
322+
fn fold_stmt(&mut self, stmt: ast::Stmt) -> OneVector<ast::Stmt> {
323323
match self.configure_stmt(stmt) {
324324
Some(stmt) => fold::noop_fold_stmt(stmt, self),
325-
None => return SmallVector::new(),
325+
None => return OneVector::new(),
326326
}
327327
}
328328

329-
fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
329+
fn fold_item(&mut self, item: P<ast::Item>) -> OneVector<P<ast::Item>> {
330330
fold::noop_fold_item(configure!(self, item), self)
331331
}
332332

333-
fn fold_impl_item(&mut self, item: ast::ImplItem) -> SmallVector<ast::ImplItem> {
333+
fn fold_impl_item(&mut self, item: ast::ImplItem) -> OneVector<ast::ImplItem> {
334334
fold::noop_fold_impl_item(configure!(self, item), self)
335335
}
336336

337-
fn fold_trait_item(&mut self, item: ast::TraitItem) -> SmallVector<ast::TraitItem> {
337+
fn fold_trait_item(&mut self, item: ast::TraitItem) -> OneVector<ast::TraitItem> {
338338
fold::noop_fold_trait_item(configure!(self, item), self)
339339
}
340340

0 commit comments

Comments
 (0)