Skip to content

Commit 749cb19

Browse files
committed
Auto merge of #25596 - Ms2ger:rbml-docs, r=alexcrichton
This leads to more idiomatic code in the callers.
2 parents 6d718f2 + fb7c0b4 commit 749cb19

File tree

3 files changed

+42
-27
lines changed

3 files changed

+42
-27
lines changed

src/librbml/lib.rs

+35-12
Original file line numberDiff line numberDiff line change
@@ -397,20 +397,43 @@ pub mod reader {
397397
}
398398
}
399399

400-
pub fn docs<F>(d: Doc, mut it: F) -> bool where
401-
F: FnMut(usize, Doc) -> bool,
402-
{
403-
let mut pos = d.start;
404-
while pos < d.end {
405-
let elt_tag = try_or!(tag_at(d.data, pos), false);
406-
let elt_size = try_or!(tag_len_at(d.data, elt_tag), false);
407-
pos = elt_size.next + elt_size.val;
408-
let doc = Doc { data: d.data, start: elt_size.next, end: pos };
409-
if !it(elt_tag.val, doc) {
410-
return false;
400+
pub fn docs<'a>(d: Doc<'a>) -> DocsIterator<'a> {
401+
DocsIterator {
402+
d: d
403+
}
404+
}
405+
406+
pub struct DocsIterator<'a> {
407+
d: Doc<'a>,
408+
}
409+
410+
impl<'a> Iterator for DocsIterator<'a> {
411+
type Item = (usize, Doc<'a>);
412+
413+
fn next(&mut self) -> Option<(usize, Doc<'a>)> {
414+
if self.d.start >= self.d.end {
415+
return None;
411416
}
417+
418+
let elt_tag = try_or!(tag_at(self.d.data, self.d.start), {
419+
self.d.start = self.d.end;
420+
None
421+
});
422+
let elt_size = try_or!(tag_len_at(self.d.data, elt_tag), {
423+
self.d.start = self.d.end;
424+
None
425+
});
426+
427+
let end = elt_size.next + elt_size.val;
428+
let doc = Doc {
429+
data: self.d.data,
430+
start: elt_size.next,
431+
end: end,
432+
};
433+
434+
self.d.start = end;
435+
return Some((elt_tag.val, doc));
412436
}
413-
return true;
414437
}
415438

416439
pub fn tagged_docs<F>(d: Doc, tg: usize, mut it: F) -> bool where

src/librustc/metadata/decoder.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -275,25 +275,18 @@ fn enum_variant_ids(item: rbml::Doc, cdata: Cmd) -> Vec<ast::DefId> {
275275

276276
fn item_path(item_doc: rbml::Doc) -> Vec<ast_map::PathElem> {
277277
let path_doc = reader::get_doc(item_doc, tag_path);
278-
279-
let len_doc = reader::get_doc(path_doc, tag_path_len);
280-
let len = reader::doc_as_u32(len_doc) as usize;
281-
282-
let mut result = Vec::with_capacity(len);
283-
reader::docs(path_doc, |tag, elt_doc| {
278+
reader::docs(path_doc).filter_map(|(tag, elt_doc)| {
284279
if tag == tag_path_elem_mod {
285280
let s = elt_doc.as_str_slice();
286-
result.push(ast_map::PathMod(token::intern(s)));
281+
Some(ast_map::PathMod(token::intern(s)))
287282
} else if tag == tag_path_elem_name {
288283
let s = elt_doc.as_str_slice();
289-
result.push(ast_map::PathName(token::intern(s)));
284+
Some(ast_map::PathName(token::intern(s)))
290285
} else {
291286
// ignore tag_path_len element
287+
None
292288
}
293-
true
294-
});
295-
296-
result
289+
}).collect()
297290
}
298291

299292
fn item_name(intr: &IdentInterner, item: rbml::Doc) -> ast::Name {

src/librustc/middle/astencode.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1726,7 +1726,7 @@ impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> {
17261726
fn decode_side_tables(dcx: &DecodeContext,
17271727
ast_doc: rbml::Doc) {
17281728
let tbl_doc = ast_doc.get(c::tag_table as usize);
1729-
reader::docs(tbl_doc, |tag, entry_doc| {
1729+
for (tag, entry_doc) in reader::docs(tbl_doc) {
17301730
let mut entry_dsr = reader::Decoder::new(entry_doc);
17311731
let id0: ast::NodeId = Decodable::decode(&mut entry_dsr).unwrap();
17321732
let id = dcx.tr_id(id0);
@@ -1840,8 +1840,7 @@ fn decode_side_tables(dcx: &DecodeContext,
18401840
}
18411841

18421842
debug!(">< Side table doc loaded");
1843-
true
1844-
});
1843+
}
18451844
}
18461845

18471846
// ______________________________________________________________________

0 commit comments

Comments
 (0)