auto merge of #7204 : alexcrichton/rust/deriving-to-string, r=pcwalton
Closes #7180 and #7179. Before, the `deriving(ToStr)` attribute was essentially `fmt!("%?")`. This changes it to recursively invoke `to_str()` on fields instead of relying on `fmt!`-style things. This seems more natural to me and what should actually be expected.
This commit is contained in:
commit
fc83d82fec
3 changed files with 102 additions and 45 deletions
|
|
@ -10,6 +10,7 @@
|
|||
|
||||
use core::prelude::*;
|
||||
|
||||
use ast;
|
||||
use ast::{meta_item, item, expr};
|
||||
use codemap::span;
|
||||
use ext::base::ExtCtxt;
|
||||
|
|
@ -40,16 +41,68 @@ pub fn expand_deriving_to_str(cx: @ExtCtxt,
|
|||
trait_def.expand(cx, span, mitem, in_items)
|
||||
}
|
||||
|
||||
fn to_str_substructure(cx: @ExtCtxt, span: span, substr: &Substructure) -> @expr {
|
||||
match substr.self_args {
|
||||
[self_obj] => {
|
||||
let self_addr = cx.expr_addr_of(span, self_obj);
|
||||
cx.expr_call_global(span,
|
||||
~[cx.ident_of("std"),
|
||||
cx.ident_of("sys"),
|
||||
cx.ident_of("log_str")],
|
||||
~[self_addr])
|
||||
// It used to be the case that this deriving implementation invoked
|
||||
// std::sys::log_str, but this isn't sufficient because it doesn't invoke the
|
||||
// to_str() method on each field. Hence we mirror the logic of the log_str()
|
||||
// method, but with tweaks to call to_str() on sub-fields.
|
||||
fn to_str_substructure(cx: @ExtCtxt, span: span,
|
||||
substr: &Substructure) -> @expr {
|
||||
let to_str = cx.ident_of("to_str");
|
||||
|
||||
let doit = |start: &str, end: @str, name: ast::ident,
|
||||
fields: &[(Option<ast::ident>, @expr, ~[@expr])]| {
|
||||
if fields.len() == 0 {
|
||||
cx.expr_str_uniq(span, cx.str_of(name))
|
||||
} else {
|
||||
let buf = cx.ident_of("buf");
|
||||
let start = cx.str_of(name) + start;
|
||||
let init = cx.expr_str_uniq(span, start.to_managed());
|
||||
let mut stmts = ~[cx.stmt_let(span, true, buf, init)];
|
||||
let push_str = cx.ident_of("push_str");
|
||||
|
||||
let push = |s: @expr| {
|
||||
let ebuf = cx.expr_ident(span, buf);
|
||||
let call = cx.expr_method_call(span, ebuf, push_str, ~[s]);
|
||||
stmts.push(cx.stmt_expr(call));
|
||||
};
|
||||
|
||||
for fields.iter().enumerate().advance |(i, &(name, e, _))| {
|
||||
if i > 0 {
|
||||
push(cx.expr_str(span, @", "));
|
||||
}
|
||||
match name {
|
||||
None => {}
|
||||
Some(id) => {
|
||||
let name = cx.str_of(id) + ": ";
|
||||
push(cx.expr_str(span, name.to_managed()));
|
||||
}
|
||||
}
|
||||
push(cx.expr_method_call(span, e, to_str, ~[]));
|
||||
}
|
||||
push(cx.expr_str(span, end));
|
||||
|
||||
cx.expr_blk(cx.blk(span, stmts, Some(cx.expr_ident(span, buf))))
|
||||
}
|
||||
_ => cx.span_bug(span, "Invalid number of arguments in `deriving(ToStr)`")
|
||||
}
|
||||
};
|
||||
|
||||
return match *substr.fields {
|
||||
Struct(ref fields) => {
|
||||
if fields.len() == 0 || fields[0].n0_ref().is_none() {
|
||||
doit("(", @")", substr.type_ident, *fields)
|
||||
} else {
|
||||
doit("{", @"}", substr.type_ident, *fields)
|
||||
}
|
||||
}
|
||||
|
||||
EnumMatching(_, variant, ref fields) => {
|
||||
match variant.node.kind {
|
||||
ast::tuple_variant_kind(*) =>
|
||||
doit("(", @")", variant.node.name, *fields),
|
||||
ast::struct_variant_kind(*) =>
|
||||
doit("{", @"}", variant.node.name, *fields),
|
||||
}
|
||||
}
|
||||
|
||||
_ => cx.bug("expected Struct or EnumMatching in deriving(ToStr)")
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue