Skip to content

Commit 9f80a17

Browse files
committed
rustc: Add pprust::res_to_str
1 parent e44467a commit 9f80a17

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

src/comp/syntax/print/pprust.rs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,34 @@ fn test_fun_to_str() {
117117
assert fun_to_str(decl, "a", []) == "fn a()";
118118
}
119119

120+
fn res_to_str(decl: ast::fn_decl, name: ast::ident,
121+
params: [ast::ty_param]) -> str {
122+
let buffer = io::mk_mem_buffer();
123+
let s = rust_printer(io::mem_buffer_writer(buffer));
124+
print_res(s, decl, name, params);
125+
end(s); // Close the head box
126+
end(s); // Close the outer box
127+
eof(s.s);
128+
io::mem_buffer_str(buffer)
129+
}
130+
131+
#[test]
132+
fn test_res_to_str() {
133+
let decl: ast::fn_decl = {
134+
inputs: [{
135+
mode: ast::by_val,
136+
ty: @ast_util::respan(ast_util::dummy_sp(), ast::ty_bool),
137+
ident: "b",
138+
id: 0
139+
}],
140+
output: @ast_util::respan(ast_util::dummy_sp(), ast::ty_nil),
141+
purity: ast::impure_fn,
142+
cf: ast::return_val,
143+
constraints: []
144+
};
145+
assert res_to_str(decl, "a", []) == "resource a(b: bool)";
146+
}
147+
120148
fn block_to_str(blk: ast::blk) -> str {
121149
let buffer = io::mk_mem_buffer();
122150
let s = rust_printer(io::mem_buffer_writer(buffer));
@@ -498,20 +526,25 @@ fn print_item(s: ps, &&item: @ast::item) {
498526
bclose(s, item.span);
499527
}
500528
ast::item_res(decl, tps, body, dt_id, ct_id) {
501-
head(s, "resource");
502-
word(s.s, item.ident);
503-
print_type_params(s, tps);
504-
popen(s);
505-
word_space(s, decl.inputs[0].ident + ":");
506-
print_type(s, decl.inputs[0].ty);
507-
pclose(s);
508-
space(s.s);
529+
print_res(s, decl, item.ident, tps);
509530
print_block(s, body);
510531
}
511532
}
512533
s.ann.post(ann_node);
513534
}
514535

536+
fn print_res(s: ps, decl: ast::fn_decl, name: ast::ident,
537+
typarams: [ast::ty_param]) {
538+
head(s, "resource");
539+
word(s.s, name);
540+
print_type_params(s, typarams);
541+
popen(s);
542+
word_space(s, decl.inputs[0].ident + ":");
543+
print_type(s, decl.inputs[0].ty);
544+
pclose(s);
545+
space(s.s);
546+
}
547+
515548
fn print_variant(s: ps, v: ast::variant) {
516549
word(s.s, v.node.name);
517550
if vec::len(v.node.args) > 0u {

0 commit comments

Comments
 (0)