@@ -664,6 +664,12 @@ bool Converter::RecordDerivesDefault(const clang::RecordDecl *decl) {
664664}
665665
666666bool Converter::RecordDerivesCopy (const clang::RecordDecl *decl) {
667+ auto *derives = Mapper::MappedDerives (ctx_.getCanonicalTagType (decl));
668+ return derives &&
669+ std::find (derives->begin (), derives->end (), " Copy" ) != derives->end ();
670+ }
671+
672+ bool Converter::RecordFieldsCopyable (const clang::RecordDecl *decl) {
667673 for (auto f : decl->fields ()) {
668674 // Records that contain std::vector, std::array, std::string or anything
669675 // that is translated to Vec<>, do not derive Copy
@@ -750,8 +756,11 @@ void Converter::EmitRustStructOrUnion(clang::RecordDecl *decl) {
750756 if (EmitsReprCForRecords ()) {
751757 StrCat (" #[repr(C)]" );
752758 }
759+ auto attrs = GetStructAttributes (decl);
760+ Mapper::SetDerives (ctx_.getCanonicalTagType (decl),
761+ std::vector<std::string>(attrs.begin (), attrs.end ()));
753762 StrCat (" #[derive(" );
754- for (auto *attr : GetStructAttributes (decl) ) {
763+ for (auto *attr : attrs ) {
755764 StrCat (attr, ' ,' );
756765 }
757766 StrCat (" )]" );
@@ -3101,6 +3110,8 @@ bool Converter::VisitEnumDecl(clang::EnumDecl *decl) {
31013110 return false ;
31023111 }
31033112 Mapper::AddRuleForUserDefinedType (decl);
3113+ Mapper::SetDerives (ctx_.getCanonicalTagType (decl),
3114+ {" Clone" , " Copy" , " PartialEq" , " Debug" , " Default" });
31043115 StrCat (" #[derive(Clone, Copy, PartialEq, Debug, Default)]" );
31053116 StrCat (std::format (" enum {}" , GetRecordName (decl)));
31063117 StrCat (' {' );
@@ -3480,7 +3491,7 @@ Converter::GetStructAttributes(const clang::RecordDecl *decl) {
34803491
34813492 std::vector<const char *> struct_attrs;
34823493
3483- if (RecordDerivesCopy (decl)) {
3494+ if (RecordFieldsCopyable (decl)) {
34843495 struct_attrs.emplace_back (" Copy" );
34853496 }
34863497
0 commit comments