Skip to content

Commit d4f5762

Browse files
committed
rustdoc: Implement a parallel fold
1 parent 7599d2d commit d4f5762

File tree

3 files changed

+92
-27
lines changed

3 files changed

+92
-27
lines changed

src/rustdoc/fold.rs

Lines changed: 72 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export default_seq_fold_res;
1111
export default_seq_fold_iface;
1212
export default_seq_fold_impl;
1313
export default_seq_fold_type;
14+
export default_par_fold;
15+
export default_par_fold_mod;
1416

1517
enum fold<T> = t<T>;
1618

@@ -86,6 +88,22 @@ fn default_seq_fold<T:copy>(ctxt: T) -> fold<T> {
8688
)
8789
}
8890

91+
fn default_par_fold<T:send>(ctxt: T) -> fold<T> {
92+
mk_fold(
93+
ctxt,
94+
{|f, d| default_seq_fold_crate(f, d)},
95+
{|f, d| default_seq_fold_item(f, d)},
96+
{|f, d| default_par_fold_mod(f, d)},
97+
{|f, d| default_seq_fold_fn(f, d)},
98+
{|f, d| default_seq_fold_const(f, d)},
99+
{|f, d| default_seq_fold_enum(f, d)},
100+
{|f, d| default_seq_fold_res(f, d)},
101+
{|f, d| default_seq_fold_iface(f, d)},
102+
{|f, d| default_seq_fold_impl(f, d)},
103+
{|f, d| default_seq_fold_type(f, d)}
104+
)
105+
}
106+
89107
fn default_seq_fold_crate<T>(
90108
fold: fold<T>,
91109
doc: doc::cratedoc
@@ -109,37 +127,54 @@ fn default_seq_fold_mod<T>(
109127
{
110128
item: fold.fold_item(fold, doc.item),
111129
items: ~vec::map(*doc.items) {|itemtag|
112-
alt itemtag {
113-
doc::modtag(moddoc) {
114-
doc::modtag(fold.fold_mod(fold, moddoc))
115-
}
116-
doc::fntag(fndoc) {
117-
doc::fntag(fold.fold_fn(fold, fndoc))
118-
}
119-
doc::consttag(constdoc) {
120-
doc::consttag(fold.fold_const(fold, constdoc))
121-
}
122-
doc::enumtag(enumdoc) {
123-
doc::enumtag(fold.fold_enum(fold, enumdoc))
124-
}
125-
doc::restag(resdoc) {
126-
doc::restag(fold.fold_res(fold, resdoc))
127-
}
128-
doc::ifacetag(ifacedoc) {
129-
doc::ifacetag(fold.fold_iface(fold, ifacedoc))
130-
}
131-
doc::impltag(impldoc) {
132-
doc::impltag(fold.fold_impl(fold, impldoc))
133-
}
134-
doc::tytag(tydoc) {
135-
doc::tytag(fold.fold_type(fold, tydoc))
136-
}
137-
}
130+
fold_itemtag(fold, itemtag)
131+
}
132+
with doc
133+
}
134+
}
135+
136+
fn default_par_fold_mod<T:send>(
137+
fold: fold<T>,
138+
doc: doc::moddoc
139+
) -> doc::moddoc {
140+
{
141+
item: fold.fold_item(fold, doc.item),
142+
items: ~util::parmap(*doc.items) {|itemtag|
143+
fold_itemtag(fold, itemtag)
138144
}
139145
with doc
140146
}
141147
}
142148

149+
fn fold_itemtag<T>(fold: fold<T>, doc: doc::itemtag) -> doc::itemtag {
150+
alt doc {
151+
doc::modtag(moddoc) {
152+
doc::modtag(fold.fold_mod(fold, moddoc))
153+
}
154+
doc::fntag(fndoc) {
155+
doc::fntag(fold.fold_fn(fold, fndoc))
156+
}
157+
doc::consttag(constdoc) {
158+
doc::consttag(fold.fold_const(fold, constdoc))
159+
}
160+
doc::enumtag(enumdoc) {
161+
doc::enumtag(fold.fold_enum(fold, enumdoc))
162+
}
163+
doc::restag(resdoc) {
164+
doc::restag(fold.fold_res(fold, resdoc))
165+
}
166+
doc::ifacetag(ifacedoc) {
167+
doc::ifacetag(fold.fold_iface(fold, ifacedoc))
168+
}
169+
doc::impltag(impldoc) {
170+
doc::impltag(fold.fold_impl(fold, impldoc))
171+
}
172+
doc::tytag(tydoc) {
173+
doc::tytag(fold.fold_type(fold, tydoc))
174+
}
175+
}
176+
}
177+
143178
fn default_seq_fold_fn<T>(
144179
fold: fold<T>,
145180
doc: doc::fndoc
@@ -238,4 +273,14 @@ fn default_fold_should_produce_same_enums() {
238273
let fld = default_seq_fold(());
239274
let folded = fld.fold_crate(fld, doc);
240275
assert doc == folded;
241-
}
276+
}
277+
278+
#[test]
279+
fn default_parallel_fold_should_produce_same_doc() {
280+
let source = "mod a { fn b() { } mod c { fn d() { } } }";
281+
let ast = parse::from_str(source);
282+
let doc = extract::extract(ast, "");
283+
let fld = default_par_fold(());
284+
let folded = fld.fold_crate(fld, doc);
285+
assert doc == folded;
286+
}

src/rustdoc/rustdoc.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ mod sort_pass;
3434
mod sort_item_name_pass;
3535
mod sort_item_type_pass;
3636
mod reexport_pass;
37+
mod util;

src/rustdoc/util.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export parmap;
2+
3+
fn parmap<T:send, U:send>(v: [T], f: fn~(T) -> U) -> [U] {
4+
let futures = vec::map(v) {|elt|
5+
future::spawn {||
6+
f(elt)
7+
}
8+
};
9+
vec::map(futures) {|future|
10+
future::get(future)
11+
}
12+
}
13+
14+
#[test]
15+
fn test_parallel_map() {
16+
let i = [1, 2, 3, 4];
17+
let j = parmap(i) {|e| e + 1 };
18+
assert j == [2, 3, 4, 5];
19+
}

0 commit comments

Comments
 (0)