]> git.lizzy.rs Git - rust.git/commitdiff
Associated types support for deriving::generic::TraitDef
authorDzmitry Malyshau <kvarkus@gmail.com>
Sun, 25 Jan 2015 05:29:24 +0000 (00:29 -0500)
committerDzmitry Malyshau <kvarkus@gmail.com>
Sun, 25 Jan 2015 05:54:30 +0000 (00:54 -0500)
14 files changed:
src/libsyntax/ext/deriving/bounds.rs
src/libsyntax/ext/deriving/clone.rs
src/libsyntax/ext/deriving/cmp/eq.rs
src/libsyntax/ext/deriving/cmp/ord.rs
src/libsyntax/ext/deriving/cmp/totaleq.rs
src/libsyntax/ext/deriving/cmp/totalord.rs
src/libsyntax/ext/deriving/decodable.rs
src/libsyntax/ext/deriving/default.rs
src/libsyntax/ext/deriving/encodable.rs
src/libsyntax/ext/deriving/generic/mod.rs
src/libsyntax/ext/deriving/hash.rs
src/libsyntax/ext/deriving/primitive.rs
src/libsyntax/ext/deriving/rand.rs
src/libsyntax/ext/deriving/show.rs

index 8ac7e57bb818b3b901ad75a962f2efc63e744ee5..9c85d3d28a7a0039d5b34ade26d4d773a2f11f67 100644 (file)
@@ -51,7 +51,8 @@ pub fn expand_deriving_bound<F>(cx: &mut ExtCtxt,
         path: Path::new(vec!("std", "marker", name)),
         additional_bounds: Vec::new(),
         generics: LifetimeBounds::empty(),
-        methods: vec!()
+        methods: Vec::new(),
+        associated_types: Vec::new(),
     };
 
     trait_def.expand(cx, mitem, item, push)
index 6498e8d2d587a611c987f54aaaad89df214bfd35..e1dcc887778231d1b8f557b59caa4d424faafc9e 100644 (file)
@@ -44,7 +44,8 @@ pub fn expand_deriving_clone<F>(cx: &mut ExtCtxt,
                     cs_clone("Clone", c, s, sub)
                 }),
             }
-        )
+        ),
+        associated_types: Vec::new(),
     };
 
     trait_def.expand(cx, mitem, item, push)
index c550c26c745727ddb166b3704d68220070551219..00971b96e9288759ec9dcdaea150112d278fc772 100644 (file)
@@ -88,7 +88,8 @@ macro_rules! md {
         methods: vec!(
             md!("eq", cs_eq),
             md!("ne", cs_ne)
-        )
+        ),
+        associated_types: Vec::new(),
     };
     trait_def.expand(cx, mitem, item, push)
 }
index 9f1850145b6c592526e8c76e0cee5dfb1e2b9a71..1f92f8d7b372a3ac92738b41fcfda4de05ac94e8 100644 (file)
@@ -78,7 +78,8 @@ macro_rules! md {
             md!("le", true, true),
             md!("gt", false, false),
             md!("ge", false, true)
-        ]
+        ],
+        associated_types: Vec::new(),
     };
     trait_def.expand(cx, mitem, item, push)
 }
index 9a2af6a3e0bee29bd939f45b90ef795f14d3ad81..0429db3643bb1a335279fb04c181668906bd04e2 100644 (file)
@@ -61,7 +61,8 @@ fn cs_total_eq_assert(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<
                     cs_total_eq_assert(a, b, c)
                 })
             }
-        )
+        ),
+        associated_types: Vec::new(),
     };
     trait_def.expand(cx, mitem, item, push)
 }
index 29d327142a6cf4785949574fa233abc175b2700d..e90c1aa6fcee8254a18eea0a04520f8db653380b 100644 (file)
@@ -45,7 +45,8 @@ pub fn expand_deriving_totalord<F>(cx: &mut ExtCtxt,
                     cs_cmp(a, b, c)
                 }),
             }
-        )
+        ),
+        associated_types: Vec::new(),
     };
 
     trait_def.expand(cx, mitem, item, push)
index e458bd58e8b6a8cb51a1865f7bdc033f565d9330..9552dff941d273d1a55ac99e8c96091d186dd7b1 100644 (file)
@@ -79,7 +79,9 @@ fn expand_deriving_decodable_imp<F>(cx: &mut ExtCtxt,
                 combine_substructure: combine_substructure(box |a, b, c| {
                     decodable_substructure(a, b, c, krate)
                 }),
-            })
+            }
+        ),
+        associated_types: Vec::new(),
     };
 
     trait_def.expand(cx, mitem, item, push)
index f8fdd8575bd67dce5246129daa8c631d277ab422..27f3c46c48fceaf3edb4f6ccac496773ac2fc00f 100644 (file)
@@ -43,7 +43,9 @@ pub fn expand_deriving_default<F>(cx: &mut ExtCtxt,
                 combine_substructure: combine_substructure(box |a, b, c| {
                     default_substructure(a, b, c)
                 })
-            })
+            }
+        ),
+        associated_types: Vec::new(),
     };
     trait_def.expand(cx, mitem, item, push)
 }
index 4c78a7b6a0cfefaf7cb6b7840cd5ac964e885e93..d0b2c2faf3719e2a3a70f921abd0fb0ee0970425 100644 (file)
@@ -155,7 +155,9 @@ fn expand_deriving_encodable_imp<F>(cx: &mut ExtCtxt,
                 combine_substructure: combine_substructure(box |a, b, c| {
                     encodable_substructure(a, b, c)
                 }),
-            })
+            }
+        ),
+        associated_types: Vec::new(),
     };
 
     trait_def.expand(cx, mitem, item, push)
index 272b0464010114d0c708b19b461d300ff691c871..99c87741c6b5b529da50b2c28a3e8d57ba5b657e 100644 (file)
@@ -228,6 +228,8 @@ pub struct TraitDef<'a> {
     pub generics: LifetimeBounds<'a>,
 
     pub methods: Vec<MethodDef<'a>>,
+
+    pub associated_types: Vec<(ast::Ident, Ty<'a>)>,
 }
 
 
@@ -387,6 +389,22 @@ fn create_derived_impl(&self,
                            methods: Vec<P<ast::Method>>) -> P<ast::Item> {
         let trait_path = self.path.to_path(cx, self.span, type_ident, generics);
 
+        // Transform associated types from `deriving::ty::Ty` into `ast::Typedef`
+        let associated_types = self.associated_types.iter().map(|&(ident, ref type_def)| {
+            P(ast::Typedef {
+                id: ast::DUMMY_NODE_ID,
+                span: self.span,
+                ident: ident,
+                vis: ast::Inherited,
+                attrs: Vec::new(),
+                typ: type_def.to_ty(cx,
+                    self.span,
+                    type_ident,
+                    generics
+                ),
+            })
+        });
+
         let Generics { mut lifetimes, ty_params, mut where_clause } =
             self.generics.to_generics(cx, self.span, type_ident, generics);
         let mut ty_params = ty_params.into_vec();
@@ -494,7 +512,11 @@ fn create_derived_impl(&self,
                           methods.into_iter()
                                  .map(|method| {
                                      ast::MethodImplItem(method)
-                                 }).collect()))
+                                 }).chain(
+                                     associated_types.map(|type_| {
+                                         ast::TypeImplItem(type_)
+                                     })
+                                 ).collect()))
     }
 
     fn expand_struct_def(&self,
index 8dac864c2ae3b689b9a89e38b3faa13f57c44b4d..e7f546b2691f2c7bb9d94c597f0649d43df536c6 100644 (file)
@@ -54,7 +54,8 @@ pub fn expand_deriving_hash<F>(cx: &mut ExtCtxt,
                     hash_substructure(a, b, c)
                 })
             }
-        )
+        ),
+        associated_types: Vec::new(),
     };
 
     hash_trait_def.expand(cx, mitem, item, push);
index c45fe1ceb2049bf0525cb2ccbde0aabbcb254b34..c694b054ba3c25cf273d88f19db0e0b5c6d73de1 100644 (file)
@@ -65,7 +65,9 @@ pub fn expand_deriving_from_primitive<F>(cx: &mut ExtCtxt,
                 combine_substructure: combine_substructure(box |c, s, sub| {
                     cs_from("u64", c, s, sub)
                 }),
-            })
+            }
+        ),
+        associated_types: Vec::new(),
     };
 
     trait_def.expand(cx, mitem, item, push)
index bb902d7059c87b8aeacd4bd5eaa76e51f37b48b1..9fd5091e194dcee568d8ee9a585f5e19138baaca 100644 (file)
@@ -49,7 +49,8 @@ pub fn expand_deriving_rand<F>(cx: &mut ExtCtxt,
                     rand_substructure(a, b, c)
                 })
             }
-        )
+        ),
+        associated_types: Vec::new(),
     };
     trait_def.expand(cx, mitem, item, push)
 }
index f5b5d4dda199c90ea1eeb4c014d5ab3ece96ac71..ec5941f58f3f02c1aaf49b01a8d3ceafd0970689 100644 (file)
@@ -35,10 +35,10 @@ pub fn expand_deriving_show<F>(cx: &mut ExtCtxt,
     let trait_def = TraitDef {
         span: span,
         attributes: Vec::new(),
-        path: Path::new(vec!("std", "fmt", "Debug")),
+        path: Path::new(vec!["std", "fmt", "Debug"]),
         additional_bounds: Vec::new(),
         generics: LifetimeBounds::empty(),
-        methods: vec!(
+        methods: vec![
             MethodDef {
                 name: "fmt",
                 generics: LifetimeBounds::empty(),
@@ -50,7 +50,8 @@ pub fn expand_deriving_show<F>(cx: &mut ExtCtxt,
                     show_substructure(a, b, c)
                 })
             }
-        )
+        ],
+        associated_types: Vec::new(),
     };
     trait_def.expand(cx, mitem, item, push)
 }