summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2016-07-11 16:56:21 +1000
committerJohn Hodge <tpg@mutabah.net>2016-07-11 16:56:21 +1000
commitc9c9c91a42bf6202d9fe3834f952033455edd43c (patch)
tree03d93fdcdb2ce992652c98b36a0ebd240803fc18
parentf81506185270dee9e695ac427c25352a6956139b (diff)
downloadmrust-c9c9c91a42bf6202d9fe3834f952033455edd43c.tar.gz
Expand derive - Add Copy
-rw-r--r--src/expand/derive.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/expand/derive.cpp b/src/expand/derive.cpp
index ba4e0292..b1951ea8 100644
--- a/src/expand/derive.cpp
+++ b/src/expand/derive.cpp
@@ -610,6 +610,39 @@ public:
}
} g_derive_clone;
+class Deriver_Copy:
+ public Deriver
+{
+ AST::Path get_trait_path() const {
+ return AST::Path("", { AST::PathNode("marker", {}), AST::PathNode("Copy", {}) });
+ }
+
+ AST::Impl make_ret(Span sp, const AST::GenericParams& p, const TypeRef& type, AST::ExprNodeP node) const
+ {
+ const AST::Path trait_path = this->get_trait_path();
+
+ AST::GenericParams params = p;
+ for(const auto& typ : params.ty_params())
+ {
+ params.bounds().push_back( ::AST::GenericBound::make_IsTrait({ TypeRef(typ.name()), {}, trait_path }) );
+ }
+
+ AST::Impl rv( AST::ImplDef( sp, AST::MetaItems(), mv$(params), make_spanned(sp, trait_path), type ) );
+ return mv$(rv);
+ }
+
+public:
+ AST::Impl handle_item(Span sp, const AST::GenericParams& p, const TypeRef& type, const AST::Struct& str) const override
+ {
+ return this->make_ret(sp, p, type, nullptr);
+ }
+
+ AST::Impl handle_item(Span sp, const AST::GenericParams& p, const TypeRef& type, const AST::Enum& enm) const override
+ {
+ return this->make_ret(sp, p, type, nullptr);
+ }
+} g_derive_copy;
+
// --------------------------------------------------------------------
// Select and dispatch the correct derive() handler
// --------------------------------------------------------------------
@@ -623,6 +656,8 @@ static const Deriver* find_impl(const ::std::string& trait_name)
return &g_derive_eq;
else if( trait_name == "Clone" )
return &g_derive_clone;
+ else if( trait_name == "Copy" )
+ return &g_derive_copy;
else
return nullptr;
}