]> git.lizzy.rs Git - rust.git/blobdiff - src/libsyntax/ext/deriving/hash.rs
auto merge of #13704 : edwardw/rust/doc-hidden, r=alexcrichton
[rust.git] / src / libsyntax / ext / deriving / hash.rs
index 0b9158547ed83abd3f4e8f3d61685ff8b104c19d..d367ae61e0b8f3e86d62e417821d0607620110eb 100644 (file)
@@ -8,6 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+use ast;
 use ast::{MetaItem, Item, Expr, MutMutable};
 use codemap::Span;
 use ext::base::ExtCtxt;
@@ -21,36 +22,41 @@ pub fn expand_deriving_hash(cx: &mut ExtCtxt,
                             item: @Item,
                             push: |@Item|) {
 
-    let allow_default_type_param_usage = cx.attribute(
-        span,
-        cx.meta_list(
-            span,
-            InternedString::new("allow"),
-            ~[cx.meta_word(span, InternedString::new("default_type_param_usage"))]));
-
+    let (path, generics, args) = if cx.ecfg.deriving_hash_type_parameter {
+        (Path::new_(vec!("std", "hash", "Hash"), None,
+                    vec!(~Literal(Path::new_local("__S"))), true),
+         LifetimeBounds {
+             lifetimes: Vec::new(),
+             bounds: vec!(("__S", ast::StaticSize, vec!(Path::new(vec!("std", "io", "Writer"))))),
+         },
+         Path::new_local("__S"))
+    } else {
+        (Path::new(vec!("std", "hash", "Hash")),
+         LifetimeBounds::empty(),
+         Path::new(vec!("std", "hash", "sip", "SipState")))
+    };
+    let inline = cx.meta_word(span, InternedString::new("inline"));
+    let attrs = vec!(cx.attribute(span, inline));
     let hash_trait_def = TraitDef {
         span: span,
-        attributes: ~[allow_default_type_param_usage],
-        path: Path::new_(~["std", "hash", "Hash"], None,
-                         ~[~Literal(Path::new_local("__H"))], true),
-        additional_bounds: ~[],
-        generics: LifetimeBounds {
-            lifetimes: ~[],
-            bounds: ~[("__H", ~[Path::new(~["std", "io", "Writer"])])],
-        },
-        methods: ~[
+        attributes: Vec::new(),
+        path: path,
+        additional_bounds: Vec::new(),
+        generics: generics,
+        methods: vec!(
             MethodDef {
                 name: "hash",
                 generics: LifetimeBounds::empty(),
                 explicit_self: borrowed_explicit_self(),
-                args: ~[Ptr(~Literal(Path::new_local("__H")),
-                            Borrowed(None, MutMutable))],
+                args: vec!(Ptr(~Literal(args), Borrowed(None, MutMutable))),
                 ret_ty: nil_ty(),
-                inline: true,
+                attributes: attrs,
                 const_nonmatching: false,
-                combine_substructure: hash_substructure
+                combine_substructure: combine_substructure(|a, b, c| {
+                    hash_substructure(a, b, c)
+                })
             }
-        ]
+        )
     };
 
     hash_trait_def.expand(cx, mitem, item, push);
@@ -63,10 +69,10 @@ fn hash_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure)
     };
     let hash_ident = substr.method_ident;
     let call_hash = |span, thing_expr| {
-        let expr = cx.expr_method_call(span, thing_expr, hash_ident, ~[state_expr]);
+        let expr = cx.expr_method_call(span, thing_expr, hash_ident, vec!(state_expr));
         cx.stmt_expr(expr)
     };
-    let mut stmts = ~[];
+    let mut stmts = Vec::new();
 
     let fields = match *substr.fields {
         Struct(ref fs) => fs,