Skip to content

Commit fe88312

Browse files
committed
Merge branch 'master' into split-mapper
2 parents 4026a0e + 49c5f22 commit fe88312

4 files changed

Lines changed: 142 additions & 0 deletions

File tree

‎cpp2rust/converter/printer.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <cctype>
1313
#include <format>
1414
#include <regex>
15+
#include <string_view>
1516
#include <utility>
1617

1718
#include "converter/converter_lib.h"
@@ -109,6 +110,12 @@ std::string ToRustName(std::string name) {
109110
c = '_';
110111
}
111112
}
113+
114+
std::string_view stem(name);
115+
stem = stem.substr(0, stem.find_last_not_of('_') + 1);
116+
if (stem == "Ptr" || stem == "Value") {
117+
name += '_';
118+
}
112119
return name;
113120
}
114121

‎tests/unit/out/refcount/struct_ctor.rs‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,87 @@ impl ByteRepr for StructWithCtor {
5353
pub fn foo_0(x: Ptr<i32>) -> Ptr<i32> {
5454
return (x).clone();
5555
}
56+
#[derive(Default)]
57+
pub struct Value_ {
58+
pub v: Value<i32>,
59+
}
60+
impl Value_ {
61+
pub fn new(u: i32) -> Self {
62+
let u: Value<i32> = Rc::new(RefCell::new(u));
63+
let __this: Value<Value_> = Rc::new(RefCell::new(Self {
64+
v: Rc::new(RefCell::new((*u.borrow()))),
65+
}));
66+
let this: Ptr<Value_> = __this.as_pointer();
67+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
68+
}
69+
}
70+
impl Clone for Value_ {
71+
fn clone(&self) -> Self {
72+
let __this: Value<Value_> = Rc::new(RefCell::new(Self {
73+
v: Rc::new(RefCell::new((*self.v.borrow()))),
74+
}));
75+
let this: Ptr<Value_> = __this.as_pointer();
76+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
77+
}
78+
}
79+
impl ByteRepr for Value_ {
80+
fn byte_size() -> usize {
81+
4
82+
}
83+
fn to_bytes(&self, buf: &mut [u8]) {
84+
(*self.v.borrow()).to_bytes(&mut buf[0..4]);
85+
}
86+
fn from_bytes(buf: &[u8]) -> Self {
87+
Self {
88+
v: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
89+
}
90+
}
91+
}
92+
#[derive()]
93+
pub struct Ptr_ {
94+
pub v1: Value<Value_>,
95+
pub v2: Value<Value_>,
96+
}
97+
impl Ptr_ {
98+
pub fn new() -> Self {
99+
let __this: Value<Ptr_> = Rc::new(RefCell::new(Self {
100+
v1: Rc::new(RefCell::new(Value_::new({ 11 }))),
101+
v2: Rc::new(RefCell::new(Value_::new({ 22 }))),
102+
}));
103+
let this: Ptr<Ptr_> = __this.as_pointer();
104+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
105+
}
106+
}
107+
impl Clone for Ptr_ {
108+
fn clone(&self) -> Self {
109+
let __this: Value<Ptr_> = Rc::new(RefCell::new(Self {
110+
v1: Rc::new(RefCell::new((*self.v1.borrow()).clone())),
111+
v2: Rc::new(RefCell::new((*self.v2.borrow()).clone())),
112+
}));
113+
let this: Ptr<Ptr_> = __this.as_pointer();
114+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
115+
}
116+
}
117+
impl Default for Ptr_ {
118+
fn default() -> Self {
119+
{ Ptr_::new() }
120+
}
121+
}
122+
impl ByteRepr for Ptr_ {
123+
fn byte_size() -> usize {
124+
8
125+
}
126+
fn to_bytes(&self, buf: &mut [u8]) {
127+
(*self.v1.borrow()).to_bytes(&mut buf[0..4]);
128+
(*self.v2.borrow()).to_bytes(&mut buf[4..8]);
129+
}
130+
fn from_bytes(buf: &[u8]) -> Self {
131+
Self {
132+
v1: Rc::new(RefCell::new(<Value_>::from_bytes(&buf[0..4]))),
133+
v2: Rc::new(RefCell::new(<Value_>::from_bytes(&buf[4..8]))),
134+
}
135+
}
136+
}
56137
pub fn main() {
57138
__cpp2rust_init_globals();
58139
std::process::exit(main_0());
@@ -66,6 +147,9 @@ fn main_0() -> i32 {
66147
&& ((({ StructWithCtorImpl::x1(&struct_with_ctor.as_pointer(),) }).read()) == 2))
67148
&& ((({ StructWithCtorImpl::x2(&struct_with_ctor.as_pointer(),) }).read()) == 1)
68149
);
150+
let p: Value<Ptr_> = Rc::new(RefCell::new(Ptr_::new()));
151+
assert!(((*(*(*p.borrow()).v1.borrow()).v.borrow()) == 11));
152+
assert!(((*(*(*p.borrow()).v2.borrow()).v.borrow()) == 22));
69153
return 0;
70154
}
71155
pub trait StructWithCtorImpl {

‎tests/unit/out/unsafe/struct_ctor.rs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,37 @@ impl StructWithCtor {
2929
pub unsafe fn foo_0(x: *mut i32) -> *mut i32 {
3030
return x;
3131
}
32+
#[repr(C)]
33+
#[derive(Copy, Clone, Default)]
34+
pub struct Value_ {
35+
pub v: i32,
36+
}
37+
impl Value_ {
38+
pub unsafe fn new(mut u: i32) -> Self {
39+
let mut this = Self { v: u };
40+
this
41+
}
42+
}
43+
#[repr(C)]
44+
#[derive(Copy, Clone)]
45+
pub struct Ptr_ {
46+
pub v1: Value_,
47+
pub v2: Value_,
48+
}
49+
impl Ptr_ {
50+
pub unsafe fn new() -> Self {
51+
let mut this = Self {
52+
v1: Value_::new({ 11 }),
53+
v2: Value_::new({ 22 }),
54+
};
55+
this
56+
}
57+
}
58+
impl Default for Ptr_ {
59+
fn default() -> Self {
60+
unsafe { Ptr_::new() }
61+
}
62+
}
3263
pub fn main() {
3364
unsafe {
3465
__cpp2rust_init_globals();
@@ -43,6 +74,9 @@ unsafe fn main_0() -> i32 {
4374
&& ((*(unsafe { StructWithCtor::x1(&struct_with_ctor,) })) == (2)))
4475
&& ((*(unsafe { StructWithCtor::x2(&struct_with_ctor,) })) == (1))
4576
);
77+
let mut p: Ptr_ = Ptr_::new();
78+
assert!(((p.v1.v) == (11)));
79+
assert!(((p.v2.v) == (22)));
4680
return 0;
4781
}
4882
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/struct_ctor.cpp‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,27 @@ struct StructWithCtor {
1313
const int &x2() const { return x2_; }
1414
};
1515
int &foo(int &x) { return x; }
16+
17+
struct Value {
18+
int v;
19+
Value(int u) : v(u) {}
20+
};
21+
22+
struct Ptr {
23+
Value v1;
24+
Value v2;
25+
26+
Ptr() : v1(11), v2(22) {}
27+
};
28+
1629
int main() {
1730
StructWithCtor struct_with_ctor(1, 2);
1831
int x = 3;
1932
assert(foo(x) == 3 && struct_with_ctor.x1() == 2 &&
2033
struct_with_ctor.x2() == 1);
34+
35+
auto p = Ptr();
36+
assert(p.v1.v == 11);
37+
assert(p.v2.v == 22);
2138
return 0;
2239
}

0 commit comments

Comments
 (0)