Skip to content

Commit 93e6a85

Browse files
committed
Use Box::from and evit avoid extra std::format's
1 parent 1e10aa4 commit 93e6a85

5 files changed

Lines changed: 13 additions & 16 deletions

File tree

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -445,14 +445,12 @@ void ConverterRefCount::AddCloneTrait(const clang::RecordDecl *decl) {
445445
auto record_name = GetRecordName(decl);
446446

447447
if (decl->isUnion()) {
448-
StrCat(std::format("impl Clone for {}", record_name));
448+
StrCat("impl Clone for", record_name);
449449
PushBrace impl_brace(*this);
450450
StrCat("fn clone(&self) -> Self");
451451
PushBrace fn_brace(*this);
452-
StrCat(
453-
std::format("{} {{ __bytes: "
454-
"Rc::new(RefCell::new(self.__bytes.borrow().clone())) }}",
455-
record_name));
452+
StrCat(record_name,
453+
"{ __bytes: Rc::new(RefCell::new(self.__bytes.borrow().clone())) }");
456454
return;
457455
}
458456

@@ -483,14 +481,13 @@ void ConverterRefCount::AddDefaultTrait(const clang::RecordDecl *decl) {
483481

484482
void ConverterRefCount::AddDefaultTraitForUnion(const clang::RecordDecl *decl) {
485483
auto name = GetRecordName(decl);
486-
StrCat(std::format("impl Default for {}", name));
484+
StrCat("impl Default for", name);
487485
PushBrace impl_brace(*this);
488486
StrCat("fn default() -> Self");
489487
PushBrace fn_brace(*this);
490-
StrCat(std::format("{} {{ __bytes: Rc::new(RefCell::new(vec![0u8; "
491-
"{}].into_boxed_slice())) }}",
492-
name,
493-
ctx_.getASTRecordLayout(decl).getSize().getQuantity()));
488+
StrCat(std::format(
489+
"{} {{ __bytes: Rc::new(RefCell::new(Box::from([0u8; {}]))) }}", name,
490+
ctx_.getASTRecordLayout(decl).getSize().getQuantity()));
494491
}
495492

496493
void ConverterRefCount::EmitRustUnion(clang::RecordDecl *decl) {
@@ -502,7 +499,7 @@ void ConverterRefCount::EmitRustUnion(clang::RecordDecl *decl) {
502499

503500
StrCat(std::format("pub struct {} {{ __bytes: Value<Box<[u8]>> }}", name));
504501

505-
StrCat(std::format("impl {}", name));
502+
StrCat("impl", name);
506503
{
507504
PushBrace impl_brace(*this);
508505
for (auto *field : decl->fields()) {

tests/unit/out/refcount/tag_vs_identifier_collision.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl Clone for point {
8787
impl Default for point {
8888
fn default() -> Self {
8989
point {
90-
__bytes: Rc::new(RefCell::new(vec![0u8; 4].into_boxed_slice())),
90+
__bytes: Rc::new(RefCell::new(Box::from([0u8; 4]))),
9191
}
9292
}
9393
}
@@ -113,7 +113,7 @@ impl Clone for slot_union {
113113
impl Default for slot_union {
114114
fn default() -> Self {
115115
slot_union {
116-
__bytes: Rc::new(RefCell::new(vec![0u8; 4].into_boxed_slice())),
116+
__bytes: Rc::new(RefCell::new(Box::from([0u8; 4]))),
117117
}
118118
}
119119
}

tests/unit/out/refcount/union_basic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl Clone for basic {
2727
impl Default for basic {
2828
fn default() -> Self {
2929
basic {
30-
__bytes: Rc::new(RefCell::new(vec![0u8; 4].into_boxed_slice())),
30+
__bytes: Rc::new(RefCell::new(Box::from([0u8; 4]))),
3131
}
3232
}
3333
}

tests/unit/out/refcount/union_pointer_pun_writethrough.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn main_0() -> i32 {
3232
impl Default for anon_0 {
3333
fn default() -> Self {
3434
anon_0 {
35-
__bytes: Rc::new(RefCell::new(vec![0u8; 8].into_boxed_slice())),
35+
__bytes: Rc::new(RefCell::new(Box::from([0u8; 8]))),
3636
}
3737
}
3838
}

tests/unit/out/refcount/union_tagged_struct_arms.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl Clone for anon_0 {
112112
impl Default for anon_0 {
113113
fn default() -> Self {
114114
anon_0 {
115-
__bytes: Rc::new(RefCell::new(vec![0u8; 40].into_boxed_slice())),
115+
__bytes: Rc::new(RefCell::new(Box::from([0u8; 40]))),
116116
}
117117
}
118118
}

0 commit comments

Comments
 (0)