@@ -469,6 +469,42 @@ void ConverterRefCount::AddDefaultTrait(const clang::RecordDecl *decl) {
469469}
470470
471471void ConverterRefCount::AddDefaultTraitForUnion (const clang::RecordDecl *decl) {
472+ auto name = GetRecordName (decl);
473+ StrCat (std::format (" impl Default for {}" , name));
474+ PushBrace impl_brace (*this );
475+ StrCat (" fn default() -> Self" );
476+ PushBrace fn_brace (*this );
477+ StrCat (std::format (" {} {{ __store: libcc2rs::UnionStore::new({}) }}" , name,
478+ ctx_.getASTRecordLayout (decl).getSize ().getQuantity ()));
479+ }
480+
481+ void ConverterRefCount::EmitRustUnion (clang::RecordDecl *decl) {
482+ auto name = GetRecordName (decl);
483+
484+ auto attrs = GetStructAttributes (decl);
485+ Mapper::SetDerives (ctx_.getCanonicalTagType (decl),
486+ std::vector<std::string>(attrs.begin (), attrs.end ()));
487+ StrCat (" #[derive(" );
488+ for (auto *attr : attrs) {
489+ StrCat (attr, ' ,' );
490+ }
491+ StrCat (" )]" );
492+
493+ StrCat (
494+ std::format (" pub struct {} {{ __store: libcc2rs::UnionStore, }}" , name));
495+
496+ StrCat (std::format (" impl {}" , name));
497+ {
498+ PushBrace impl_brace (*this );
499+ for (auto *field : decl->fields ()) {
500+ StrCat (std::format (
501+ " pub fn {}(&self) -> Ptr<{}> {{ self.__store.pod(0) }}" ,
502+ GetNamedDeclAsString (field), Mapper::Map (field->getType ())));
503+ }
504+ }
505+
506+ AddDefaultTrait (decl);
507+ AddByteReprTrait (decl);
472508}
473509
474510void ConverterRefCount::AddDropTrait (const clang::CXXRecordDecl *decl) {
@@ -1789,6 +1825,11 @@ std::vector<const char *>
17891825ConverterRefCount::GetStructAttributes (const clang::RecordDecl *decl) {
17901826 std::vector<const char *> attrs;
17911827
1828+ if (decl->isUnion ()) {
1829+ attrs.emplace_back (" Clone" );
1830+ return attrs;
1831+ }
1832+
17921833 if (RecordDerivesDefault (decl)) {
17931834 attrs.emplace_back (" Default" );
17941835 }
0 commit comments