Skip to content

Commit 1195708

Browse files
author
Jorge Aparicio
committed
librustc: use unboxed closures
1 parent 933e7b4 commit 1195708

37 files changed

+517
-358
lines changed

src/librustc/lint/context.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,11 @@ impl<'a, 'tcx> Context<'a, 'tcx> {
464464
/// Merge the lints specified by any lint attributes into the
465465
/// current lint context, call the provided function, then reset the
466466
/// lints in effect to their previous state.
467-
fn with_lint_attrs(&mut self,
468-
attrs: &[ast::Attribute],
469-
f: |&mut Context|) {
467+
fn with_lint_attrs<F>(&mut self,
468+
attrs: &[ast::Attribute],
469+
f: F) where
470+
F: FnOnce(&mut Context),
471+
{
470472
// Parse all of the lint attributes, and then add them all to the
471473
// current dictionary of lint information. Along the way, keep a history
472474
// of what we changed so we can roll everything back after invoking the
@@ -528,7 +530,9 @@ impl<'a, 'tcx> Context<'a, 'tcx> {
528530
}
529531
}
530532

531-
fn visit_ids(&mut self, f: |&mut ast_util::IdVisitor<Context>|) {
533+
fn visit_ids<F>(&mut self, f: F) where
534+
F: FnOnce(&mut ast_util::IdVisitor<Context>)
535+
{
532536
let mut v = ast_util::IdVisitor {
533537
operation: self,
534538
pass_through_items: false,

src/librustc/metadata/csearch.rs

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,22 @@ pub fn get_symbol(cstore: &cstore::CStore, def: ast::DefId) -> String {
4747
}
4848

4949
/// Iterates over all the language items in the given crate.
50-
pub fn each_lang_item(cstore: &cstore::CStore,
51-
cnum: ast::CrateNum,
52-
f: |ast::NodeId, uint| -> bool)
53-
-> bool {
50+
pub fn each_lang_item<F>(cstore: &cstore::CStore,
51+
cnum: ast::CrateNum,
52+
f: F)
53+
-> bool where
54+
F: FnMut(ast::NodeId, uint) -> bool,
55+
{
5456
let crate_data = cstore.get_crate_data(cnum);
5557
decoder::each_lang_item(&*crate_data, f)
5658
}
5759

5860
/// Iterates over each child of the given item.
59-
pub fn each_child_of_item(cstore: &cstore::CStore,
60-
def_id: ast::DefId,
61-
callback: |decoder::DefLike,
62-
ast::Name,
63-
ast::Visibility|) {
61+
pub fn each_child_of_item<F>(cstore: &cstore::CStore,
62+
def_id: ast::DefId,
63+
callback: F) where
64+
F: FnMut(decoder::DefLike, ast::Name, ast::Visibility),
65+
{
6466
let crate_data = cstore.get_crate_data(def_id.krate);
6567
let get_crate_data: decoder::GetCrateDataCb = |cnum| {
6668
cstore.get_crate_data(cnum)
@@ -73,11 +75,11 @@ pub fn each_child_of_item(cstore: &cstore::CStore,
7375
}
7476

7577
/// Iterates over each top-level crate item.
76-
pub fn each_top_level_item_of_crate(cstore: &cstore::CStore,
77-
cnum: ast::CrateNum,
78-
callback: |decoder::DefLike,
79-
ast::Name,
80-
ast::Visibility|) {
78+
pub fn each_top_level_item_of_crate<F>(cstore: &cstore::CStore,
79+
cnum: ast::CrateNum,
80+
callback: F) where
81+
F: FnMut(decoder::DefLike, ast::Name, ast::Visibility),
82+
{
8183
let crate_data = cstore.get_crate_data(cnum);
8284
let get_crate_data: decoder::GetCrateDataCb = |cnum| {
8385
cstore.get_crate_data(cnum)
@@ -195,9 +197,11 @@ pub fn get_methods_if_impl(cstore: &cstore::CStore,
195197
decoder::get_methods_if_impl(cstore.intr.clone(), &*cdata, def.node)
196198
}
197199

198-
pub fn get_item_attrs(cstore: &cstore::CStore,
199-
def_id: ast::DefId,
200-
f: |Vec<ast::Attribute>|) {
200+
pub fn get_item_attrs<F>(cstore: &cstore::CStore,
201+
def_id: ast::DefId,
202+
f: F) where
203+
F: FnOnce(Vec<ast::Attribute>),
204+
{
201205
let cdata = cstore.get_crate_data(def_id.krate);
202206
decoder::get_item_attrs(&*cdata, def_id.node, f)
203207
}
@@ -279,23 +283,29 @@ pub fn get_native_libraries(cstore: &cstore::CStore, crate_num: ast::CrateNum)
279283
decoder::get_native_libraries(&*cdata)
280284
}
281285

282-
pub fn each_impl(cstore: &cstore::CStore,
283-
crate_num: ast::CrateNum,
284-
callback: |ast::DefId|) {
286+
pub fn each_impl<F>(cstore: &cstore::CStore,
287+
crate_num: ast::CrateNum,
288+
callback: F) where
289+
F: FnMut(ast::DefId),
290+
{
285291
let cdata = cstore.get_crate_data(crate_num);
286292
decoder::each_impl(&*cdata, callback)
287293
}
288294

289-
pub fn each_implementation_for_type(cstore: &cstore::CStore,
290-
def_id: ast::DefId,
291-
callback: |ast::DefId|) {
295+
pub fn each_implementation_for_type<F>(cstore: &cstore::CStore,
296+
def_id: ast::DefId,
297+
callback: F) where
298+
F: FnMut(ast::DefId),
299+
{
292300
let cdata = cstore.get_crate_data(def_id.krate);
293301
decoder::each_implementation_for_type(&*cdata, def_id.node, callback)
294302
}
295303

296-
pub fn each_implementation_for_trait(cstore: &cstore::CStore,
297-
def_id: ast::DefId,
298-
callback: |ast::DefId|) {
304+
pub fn each_implementation_for_trait<F>(cstore: &cstore::CStore,
305+
def_id: ast::DefId,
306+
callback: F) where
307+
F: FnMut(ast::DefId),
308+
{
299309
let cdata = cstore.get_crate_data(def_id.krate);
300310
decoder::each_implementation_for_trait(&*cdata, def_id.node, callback)
301311
}

src/librustc/metadata/cstore.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,18 @@ impl CStore {
113113
self.metas.borrow_mut().insert(cnum, data);
114114
}
115115

116-
pub fn iter_crate_data(&self, i: |ast::CrateNum, &crate_metadata|) {
116+
pub fn iter_crate_data<I>(&self, mut i: I) where
117+
I: FnMut(ast::CrateNum, &crate_metadata),
118+
{
117119
for (&k, v) in self.metas.borrow().iter() {
118120
i(k, &**v);
119121
}
120122
}
121123

122124
/// Like `iter_crate_data`, but passes source paths (if available) as well.
123-
pub fn iter_crate_data_origins(&self, i: |ast::CrateNum,
124-
&crate_metadata,
125-
Option<CrateSource>|) {
125+
pub fn iter_crate_data_origins<I>(&self, mut i: I) where
126+
I: FnMut(ast::CrateNum, &crate_metadata, Option<CrateSource>),
127+
{
126128
for (&k, v) in self.metas.borrow().iter() {
127129
let origin = self.get_used_crate_source(k);
128130
origin.as_ref().map(|cs| { assert!(k == cs.cnum); });

src/librustc/metadata/decoder.rs

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ pub type Cmd<'a> = &'a crate_metadata;
6060
// what crate that's in and give us a def_id that makes sense for the current
6161
// build.
6262

63-
fn lookup_hash<'a>(d: rbml::Doc<'a>, eq_fn: |&[u8]| -> bool,
64-
hash: u64) -> Option<rbml::Doc<'a>> {
63+
fn lookup_hash<'a, F>(d: rbml::Doc<'a>, mut eq_fn: F, hash: u64) -> Option<rbml::Doc<'a>> where
64+
F: FnMut(&[u8]) -> bool,
65+
{
6566
let index = reader::get_doc(d, tag_index);
6667
let table = reader::get_doc(index, tag_index_table);
6768
let hash_pos = table.start + (hash % 256 * 4) as uint;
@@ -448,7 +449,9 @@ pub enum DefLike {
448449
impl Copy for DefLike {}
449450

450451
/// Iterates over the language items in the given crate.
451-
pub fn each_lang_item(cdata: Cmd, f: |ast::NodeId, uint| -> bool) -> bool {
452+
pub fn each_lang_item<F>(cdata: Cmd, mut f: F) -> bool where
453+
F: FnMut(ast::NodeId, uint) -> bool,
454+
{
452455
let root = rbml::Doc::new(cdata.data());
453456
let lang_items = reader::get_doc(root, tag_lang_items);
454457
reader::tagged_docs(lang_items, tag_lang_items_item, |item_doc| {
@@ -464,13 +467,13 @@ pub fn each_lang_item(cdata: Cmd, f: |ast::NodeId, uint| -> bool) -> bool {
464467

465468
pub type GetCrateDataCb<'a> = |ast::CrateNum|: 'a -> Rc<crate_metadata>;
466469

467-
fn each_child_of_item_or_crate(intr: Rc<IdentInterner>,
468-
cdata: Cmd,
469-
item_doc: rbml::Doc,
470-
get_crate_data: GetCrateDataCb,
471-
callback: |DefLike,
472-
ast::Name,
473-
ast::Visibility|) {
470+
fn each_child_of_item_or_crate<F>(intr: Rc<IdentInterner>,
471+
cdata: Cmd,
472+
item_doc: rbml::Doc,
473+
get_crate_data: GetCrateDataCb,
474+
mut callback: F) where
475+
F: FnMut(DefLike, ast::Name, ast::Visibility),
476+
{
474477
// Iterate over all children.
475478
let _ = reader::tagged_docs(item_doc, tag_mod_child, |child_info_doc| {
476479
let child_def_id = reader::with_doc_data(child_info_doc,
@@ -583,11 +586,13 @@ fn each_child_of_item_or_crate(intr: Rc<IdentInterner>,
583586
}
584587

585588
/// Iterates over each child of the given item.
586-
pub fn each_child_of_item(intr: Rc<IdentInterner>,
587-
cdata: Cmd,
588-
id: ast::NodeId,
589-
get_crate_data: GetCrateDataCb,
590-
callback: |DefLike, ast::Name, ast::Visibility|) {
589+
pub fn each_child_of_item<F>(intr: Rc<IdentInterner>,
590+
cdata: Cmd,
591+
id: ast::NodeId,
592+
get_crate_data: GetCrateDataCb,
593+
callback: F) where
594+
F: FnMut(DefLike, ast::Name, ast::Visibility),
595+
{
591596
// Find the item.
592597
let root_doc = rbml::Doc::new(cdata.data());
593598
let items = reader::get_doc(root_doc, tag_items);
@@ -604,12 +609,12 @@ pub fn each_child_of_item(intr: Rc<IdentInterner>,
604609
}
605610

606611
/// Iterates over all the top-level crate items.
607-
pub fn each_top_level_item_of_crate(intr: Rc<IdentInterner>,
608-
cdata: Cmd,
609-
get_crate_data: GetCrateDataCb,
610-
callback: |DefLike,
611-
ast::Name,
612-
ast::Visibility|) {
612+
pub fn each_top_level_item_of_crate<F>(intr: Rc<IdentInterner>,
613+
cdata: Cmd,
614+
get_crate_data: GetCrateDataCb,
615+
callback: F) where
616+
F: FnMut(DefLike, ast::Name, ast::Visibility),
617+
{
613618
let root_doc = rbml::Doc::new(cdata.data());
614619
let misc_info_doc = reader::get_doc(root_doc, tag_misc_info);
615620
let crate_items_doc = reader::get_doc(misc_info_doc,
@@ -982,9 +987,11 @@ pub fn get_tuple_struct_definition_if_ctor(cdata: Cmd,
982987
ret
983988
}
984989

985-
pub fn get_item_attrs(cdata: Cmd,
986-
orig_node_id: ast::NodeId,
987-
f: |Vec<ast::Attribute>|) {
990+
pub fn get_item_attrs<F>(cdata: Cmd,
991+
orig_node_id: ast::NodeId,
992+
f: F) where
993+
F: FnOnce(Vec<ast::Attribute>),
994+
{
988995
// The attributes for a tuple struct are attached to the definition, not the ctor;
989996
// we assume that someone passing in a tuple struct ctor is actually wanting to
990997
// look at the definition
@@ -1224,17 +1231,21 @@ pub fn translate_def_id(cdata: Cmd, did: ast::DefId) -> ast::DefId {
12241231
}
12251232
}
12261233

1227-
pub fn each_impl(cdata: Cmd, callback: |ast::DefId|) {
1234+
pub fn each_impl<F>(cdata: Cmd, mut callback: F) where
1235+
F: FnMut(ast::DefId),
1236+
{
12281237
let impls_doc = reader::get_doc(rbml::Doc::new(cdata.data()), tag_impls);
12291238
let _ = reader::tagged_docs(impls_doc, tag_impls_impl, |impl_doc| {
12301239
callback(item_def_id(impl_doc, cdata));
12311240
true
12321241
});
12331242
}
12341243

1235-
pub fn each_implementation_for_type(cdata: Cmd,
1244+
pub fn each_implementation_for_type<F>(cdata: Cmd,
12361245
id: ast::NodeId,
1237-
callback: |ast::DefId|) {
1246+
mut callback: F) where
1247+
F: FnMut(ast::DefId),
1248+
{
12381249
let item_doc = lookup_item(id, cdata.data());
12391250
reader::tagged_docs(item_doc,
12401251
tag_items_data_item_inherent_impl,
@@ -1245,9 +1256,11 @@ pub fn each_implementation_for_type(cdata: Cmd,
12451256
});
12461257
}
12471258

1248-
pub fn each_implementation_for_trait(cdata: Cmd,
1249-
id: ast::NodeId,
1250-
callback: |ast::DefId|) {
1259+
pub fn each_implementation_for_trait<F>(cdata: Cmd,
1260+
id: ast::NodeId,
1261+
mut callback: F) where
1262+
F: FnMut(ast::DefId),
1263+
{
12511264
let item_doc = lookup_item(id, cdata.data());
12521265

12531266
let _ = reader::tagged_docs(item_doc,

src/librustc/metadata/encoder.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,9 @@ fn encode_reexported_static_methods(ecx: &EncodeContext,
493493
/// top-level items that are sub-items of the given item. Specifically:
494494
///
495495
/// * For newtype structs, iterates through the node ID of the constructor.
496-
fn each_auxiliary_node_id(item: &ast::Item, callback: |NodeId| -> bool) -> bool {
496+
fn each_auxiliary_node_id<F>(item: &ast::Item, callback: F) -> bool where
497+
F: FnOnce(NodeId) -> bool,
498+
{
497499
let mut continue_ = true;
498500
match item.node {
499501
ast::ItemStruct(ref struct_def, _) => {
@@ -1579,8 +1581,10 @@ fn encode_info_for_items(ecx: &EncodeContext,
15791581

15801582
// Path and definition ID indexing
15811583

1582-
fn encode_index<T: Hash>(rbml_w: &mut Encoder, index: Vec<entry<T>>,
1583-
write_fn: |&mut SeekableMemWriter, &T|) {
1584+
fn encode_index<T, F>(rbml_w: &mut Encoder, index: Vec<entry<T>>, mut write_fn: F) where
1585+
F: FnMut(&mut SeekableMemWriter, &T),
1586+
T: Hash,
1587+
{
15841588
let mut buckets: Vec<Vec<entry<T>>> = Vec::from_fn(256, |_| Vec::new());
15851589
for elt in index.into_iter() {
15861590
let h = hash::hash(&elt.val) as uint;

src/librustc/metadata/filesearch.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ pub struct FileSearch<'a> {
4242
}
4343

4444
impl<'a> FileSearch<'a> {
45-
pub fn for_each_lib_search_path(&self, f: |&Path| -> FileMatch) {
45+
pub fn for_each_lib_search_path<F>(&self, mut f: F) where
46+
F: FnMut(&Path) -> FileMatch,
47+
{
4648
let mut visited_dirs = HashSet::new();
4749
let mut found = false;
4850

src/librustc/metadata/tydecode.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ fn next_byte(st: &mut PState) -> u8 {
8989
return b;
9090
}
9191

92-
fn scan<R>(st: &mut PState, is_last: |char| -> bool, op: |&[u8]| -> R) -> R {
92+
fn scan<R, F, G>(st: &mut PState, mut is_last: F, op: G) -> R where
93+
F: FnMut(char) -> bool,
94+
G: FnOnce(&[u8]) -> R,
95+
{
9396
let start_pos = st.pos;
9497
debug!("scan: '{}' (start)", st.data[st.pos] as char);
9598
while !is_last(st.data[st.pos] as char) {
@@ -110,7 +113,9 @@ pub fn parse_name(st: &mut PState, last: char) -> ast::Name {
110113
parse_name_(st, |a| is_last(last, a) )
111114
}
112115

113-
fn parse_name_(st: &mut PState, is_last: |char| -> bool) -> ast::Name {
116+
fn parse_name_<F>(st: &mut PState, is_last: F) -> ast::Name where
117+
F: FnMut(char) -> bool,
118+
{
114119
scan(st, is_last, |bytes| {
115120
token::intern(str::from_utf8(bytes).unwrap())
116121
})
@@ -234,9 +239,10 @@ fn parse_trait_store(st: &mut PState, conv: conv_did) -> ty::TraitStore {
234239
}
235240
}
236241

237-
fn parse_vec_per_param_space<'a, 'tcx, T>(st: &mut PState<'a, 'tcx>,
238-
f: |&mut PState<'a, 'tcx>| -> T)
239-
-> VecPerParamSpace<T>
242+
fn parse_vec_per_param_space<'a, 'tcx, T, F>(st: &mut PState<'a, 'tcx>,
243+
mut f: F)
244+
-> VecPerParamSpace<T> where
245+
F: FnMut(&mut PState<'a, 'tcx>) -> T,
240246
{
241247
let mut r = VecPerParamSpace::empty();
242248
for &space in subst::ParamSpace::all().iter() {
@@ -350,8 +356,9 @@ fn parse_scope(st: &mut PState) -> region::CodeExtent {
350356
}
351357
}
352358

353-
fn parse_opt<'a, 'tcx, T>(st: &mut PState<'a, 'tcx>, f: |&mut PState<'a, 'tcx>| -> T)
354-
-> Option<T> {
359+
fn parse_opt<'a, 'tcx, T, F>(st: &mut PState<'a, 'tcx>, f: F) -> Option<T> where
360+
F: FnOnce(&mut PState<'a, 'tcx>) -> T,
361+
{
355362
match next(st) {
356363
'n' => None,
357364
's' => Some(f(st)),

0 commit comments

Comments
 (0)