Associated types support for deriving::generic::TraitDef

This commit is contained in:
Dzmitry Malyshau 2015-01-25 00:29:24 -05:00
parent 4be79d6acd
commit e5632157b1
14 changed files with 55 additions and 16 deletions

View 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)

View 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)

View 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)
}

View 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)
}

View 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)
}

View 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)

View 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)

View 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)
}

View 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)

View 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,

View 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);

View 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)

View 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)
}

View 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)
}