|
10 | 10 |
|
11 | 11 | use core::prelude::*;
|
12 | 12 |
|
| 13 | +use ast; |
13 | 14 | use ast::{meta_item, item, expr};
|
14 | 15 | use codemap::span;
|
15 | 16 | use ext::base::ExtCtxt;
|
@@ -40,16 +41,68 @@ pub fn expand_deriving_to_str(cx: @ExtCtxt,
|
40 | 41 | trait_def.expand(cx, span, mitem, in_items)
|
41 | 42 | }
|
42 | 43 |
|
43 |
| -fn to_str_substructure(cx: @ExtCtxt, span: span, substr: &Substructure) -> @expr { |
44 |
| - match substr.self_args { |
45 |
| - [self_obj] => { |
46 |
| - let self_addr = cx.expr_addr_of(span, self_obj); |
47 |
| - cx.expr_call_global(span, |
48 |
| - ~[cx.ident_of("std"), |
49 |
| - cx.ident_of("sys"), |
50 |
| - cx.ident_of("log_str")], |
51 |
| - ~[self_addr]) |
| 44 | +// It used to be the case that this deriving implementation invoked |
| 45 | +// std::sys::log_str, but this isn't sufficient because it doesn't invoke the |
| 46 | +// to_str() method on each field. Hence we mirror the logic of the log_str() |
| 47 | +// method, but with tweaks to call to_str() on sub-fields. |
| 48 | +fn to_str_substructure(cx: @ExtCtxt, span: span, |
| 49 | + substr: &Substructure) -> @expr { |
| 50 | + let to_str = cx.ident_of("to_str"); |
| 51 | + |
| 52 | + let doit = |start: &str, end: @str, name: ast::ident, |
| 53 | + fields: &[(Option<ast::ident>, @expr, ~[@expr])]| { |
| 54 | + if fields.len() == 0 { |
| 55 | + cx.expr_str_uniq(span, cx.str_of(name)) |
| 56 | + } else { |
| 57 | + let buf = cx.ident_of("buf"); |
| 58 | + let start = cx.str_of(name) + start; |
| 59 | + let init = cx.expr_str_uniq(span, start.to_managed()); |
| 60 | + let mut stmts = ~[cx.stmt_let(span, true, buf, init)]; |
| 61 | + let push_str = cx.ident_of("push_str"); |
| 62 | + |
| 63 | + let push = |s: @expr| { |
| 64 | + let ebuf = cx.expr_ident(span, buf); |
| 65 | + let call = cx.expr_method_call(span, ebuf, push_str, ~[s]); |
| 66 | + stmts.push(cx.stmt_expr(call)); |
| 67 | + }; |
| 68 | + |
| 69 | + for fields.iter().enumerate().advance |(i, &(name, e, _))| { |
| 70 | + if i > 0 { |
| 71 | + push(cx.expr_str(span, @", ")); |
| 72 | + } |
| 73 | + match name { |
| 74 | + None => {} |
| 75 | + Some(id) => { |
| 76 | + let name = cx.str_of(id) + ": "; |
| 77 | + push(cx.expr_str(span, name.to_managed())); |
| 78 | + } |
| 79 | + } |
| 80 | + push(cx.expr_method_call(span, e, to_str, ~[])); |
| 81 | + } |
| 82 | + push(cx.expr_str(span, end)); |
| 83 | +
|
| 84 | + cx.expr_blk(cx.blk(span, stmts, Some(cx.expr_ident(span, buf)))) |
| 85 | + } |
| 86 | + }; |
| 87 | +
|
| 88 | + return match *substr.fields { |
| 89 | + Struct(ref fields) => { |
| 90 | + if fields.len() == 0 || fields[0].n0_ref().is_none() { |
| 91 | + doit("(", @")", substr.type_ident, *fields) |
| 92 | + } else { |
| 93 | + doit("{", @"}", substr.type_ident, *fields) |
| 94 | + } |
52 | 95 | }
|
53 |
| - _ => cx.span_bug(span, "Invalid number of arguments in `deriving(ToStr)`") |
54 |
| - } |
| 96 | + |
| 97 | + EnumMatching(_, variant, ref fields) => { |
| 98 | + match variant.node.kind { |
| 99 | + ast::tuple_variant_kind(*) => |
| 100 | + doit("(", @")", variant.node.name, *fields), |
| 101 | + ast::struct_variant_kind(*) => |
| 102 | + doit("{", @"}", variant.node.name, *fields), |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + _ => cx.bug("expected Struct or EnumMatching in deriving(ToStr)") |
| 107 | + }; |
55 | 108 | }
|
0 commit comments