Skip to content

Rewrite reader::docs to return an iterator. #25596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions src/librbml/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,20 +397,43 @@ pub mod reader {
}
}

pub fn docs<F>(d: Doc, mut it: F) -> bool where
F: FnMut(usize, Doc) -> bool,
{
let mut pos = d.start;
while pos < d.end {
let elt_tag = try_or!(tag_at(d.data, pos), false);
let elt_size = try_or!(tag_len_at(d.data, elt_tag), false);
pos = elt_size.next + elt_size.val;
let doc = Doc { data: d.data, start: elt_size.next, end: pos };
if !it(elt_tag.val, doc) {
return false;
pub fn docs<'a>(d: Doc<'a>) -> DocsIterator<'a> {
DocsIterator {
d: d
}
}

pub struct DocsIterator<'a> {
d: Doc<'a>,
}

impl<'a> Iterator for DocsIterator<'a> {
type Item = (usize, Doc<'a>);

fn next(&mut self) -> Option<(usize, Doc<'a>)> {
if self.d.start >= self.d.end {
return None;
}

let elt_tag = try_or!(tag_at(self.d.data, self.d.start), {
self.d.start = self.d.end;
None
});
let elt_size = try_or!(tag_len_at(self.d.data, elt_tag), {
self.d.start = self.d.end;
None
});

let end = elt_size.next + elt_size.val;
let doc = Doc {
data: self.d.data,
start: elt_size.next,
end: end,
};

self.d.start = end;
return Some((elt_tag.val, doc));
}
return true;
}

pub fn tagged_docs<F>(d: Doc, tg: usize, mut it: F) -> bool where
Expand Down
17 changes: 5 additions & 12 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,25 +275,18 @@ fn enum_variant_ids(item: rbml::Doc, cdata: Cmd) -> Vec<ast::DefId> {

fn item_path(item_doc: rbml::Doc) -> Vec<ast_map::PathElem> {
let path_doc = reader::get_doc(item_doc, tag_path);

let len_doc = reader::get_doc(path_doc, tag_path_len);
let len = reader::doc_as_u32(len_doc) as usize;

let mut result = Vec::with_capacity(len);
reader::docs(path_doc, |tag, elt_doc| {
reader::docs(path_doc).filter_map(|(tag, elt_doc)| {
if tag == tag_path_elem_mod {
let s = elt_doc.as_str_slice();
result.push(ast_map::PathMod(token::intern(s)));
Some(ast_map::PathMod(token::intern(s)))
} else if tag == tag_path_elem_name {
let s = elt_doc.as_str_slice();
result.push(ast_map::PathName(token::intern(s)));
Some(ast_map::PathName(token::intern(s)))
} else {
// ignore tag_path_len element
None
}
true
});

result
}).collect()
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this comment stay for now? Otherwise it's somewhat unclear what's being ignored here.

fn item_name(intr: &IdentInterner, item: rbml::Doc) -> ast::Name {
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/middle/astencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1706,7 +1706,7 @@ impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> {
fn decode_side_tables(dcx: &DecodeContext,
ast_doc: rbml::Doc) {
let tbl_doc = ast_doc.get(c::tag_table as usize);
reader::docs(tbl_doc, |tag, entry_doc| {
for (tag, entry_doc) in reader::docs(tbl_doc) {
let mut entry_dsr = reader::Decoder::new(entry_doc);
let id0: ast::NodeId = Decodable::decode(&mut entry_dsr).unwrap();
let id = dcx.tr_id(id0);
Expand Down Expand Up @@ -1815,8 +1815,7 @@ fn decode_side_tables(dcx: &DecodeContext,
}

debug!(">< Side table doc loaded");
true
});
}
}

// ______________________________________________________________________
Expand Down