Skip to content

Commit 2d49e8b

Browse files
authored
Add panic'ing ByteRepr impl for Ptr and AnyPtr (#215)
This just adds panic'ing implementations for Ptr and AnyPtr so that some tests move from no-compile to panic and adds the ByteRepr boilerplate where needed. It will make the PR with the real ByteRepr implementatoin for Ptr and AnyPtr smaller.
1 parent a41899c commit 2d49e8b

33 files changed

Lines changed: 943 additions & 33 deletions

cpp2rust/converter/converter_lib.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ bool TypeImplementsByteRepr(clang::QualType qt) {
204204
if (qt->isIntegerType() || qt->isFloatingType() || qt->isEnumeralType()) {
205205
return true;
206206
}
207+
if (qt->isPointerType()) {
208+
return true;
209+
}
207210
if (const auto *arr = qt->getAsArrayTypeUnsafe()) {
208211
return TypeImplementsByteRepr(arr->getElementType());
209212
}

libcc2rs/src/rc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,7 @@ impl<T: ?Sized> AsPointerDyn<T> for Rc<RefCell<T>> {
12561256
}
12571257

12581258
impl<T: 'static> ByteRepr for Ptr<T> {}
1259+
impl ByteRepr for AnyPtr {}
12591260

12601261
#[cfg(test)]
12611262
mod tests {

tests/multi-file/opaque_forward_decl/out/refcount/opaque_forward_decl.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,21 @@ pub struct container {
1111
pub p: Value<Ptr<opaque>>,
1212
pub x: Value<i32>,
1313
}
14-
impl ByteRepr for container {}
14+
impl ByteRepr for container {
15+
fn byte_size() -> usize {
16+
16
17+
}
18+
fn to_bytes(&self, buf: &mut [u8]) {
19+
(*self.p.borrow()).to_bytes(&mut buf[0..8]);
20+
(*self.x.borrow()).to_bytes(&mut buf[8..12]);
21+
}
22+
fn from_bytes(buf: &[u8]) -> Self {
23+
Self {
24+
p: Rc::new(RefCell::new(<Ptr<opaque>>::from_bytes(&buf[0..8]))),
25+
x: Rc::new(RefCell::new(<i32>::from_bytes(&buf[8..12]))),
26+
}
27+
}
28+
}
1529
pub fn main() {
1630
std::process::exit(main_0());
1731
}

tests/unit/out/refcount/10_struct.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,21 @@ impl Clone for GraphNode {
2020
this
2121
}
2222
}
23-
impl ByteRepr for GraphNode {}
23+
impl ByteRepr for GraphNode {
24+
fn byte_size() -> usize {
25+
16
26+
}
27+
fn to_bytes(&self, buf: &mut [u8]) {
28+
(*self.dst.borrow()).to_bytes(&mut buf[0..4]);
29+
(*self.next.borrow()).to_bytes(&mut buf[8..16]);
30+
}
31+
fn from_bytes(buf: &[u8]) -> Self {
32+
Self {
33+
dst: Rc::new(RefCell::new(<u32>::from_bytes(&buf[0..4]))),
34+
next: Rc::new(RefCell::new(<Ptr<GraphNode>>::from_bytes(&buf[8..16]))),
35+
}
36+
}
37+
}
2438
#[derive(Default)]
2539
pub struct Graph {
2640
pub V: Value<u32>,
@@ -59,7 +73,21 @@ impl Clone for Graph {
5973
this
6074
}
6175
}
62-
impl ByteRepr for Graph {}
76+
impl ByteRepr for Graph {
77+
fn byte_size() -> usize {
78+
16
79+
}
80+
fn to_bytes(&self, buf: &mut [u8]) {
81+
(*self.V.borrow()).to_bytes(&mut buf[0..4]);
82+
(*self.adj.borrow()).to_bytes(&mut buf[8..16]);
83+
}
84+
fn from_bytes(buf: &[u8]) -> Self {
85+
Self {
86+
V: Rc::new(RefCell::new(<u32>::from_bytes(&buf[0..4]))),
87+
adj: Rc::new(RefCell::new(<Ptr<Ptr<GraphNode>>>::from_bytes(&buf[8..16]))),
88+
}
89+
}
90+
}
6391
pub fn main() {
6492
std::process::exit(main_0());
6593
}

tests/unit/out/refcount/addr_of_global.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,19 @@ impl Clone for Outer {
4343
this
4444
}
4545
}
46-
impl ByteRepr for Outer {}
46+
impl ByteRepr for Outer {
47+
fn byte_size() -> usize {
48+
8
49+
}
50+
fn to_bytes(&self, buf: &mut [u8]) {
51+
(*self.p.borrow()).to_bytes(&mut buf[0..8]);
52+
}
53+
fn from_bytes(buf: &[u8]) -> Self {
54+
Self {
55+
p: Rc::new(RefCell::new(<Ptr<Inner>>::from_bytes(&buf[0..8]))),
56+
}
57+
}
58+
}
4759
thread_local!(
4860
pub static alpha_0: Value<Inner> = Rc::new(RefCell::new(Inner {
4961
value: Rc::new(RefCell::new(1)),

tests/unit/out/refcount/bst.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,23 @@ impl Clone for node_t {
2222
this
2323
}
2424
}
25-
impl ByteRepr for node_t {}
25+
impl ByteRepr for node_t {
26+
fn byte_size() -> usize {
27+
24
28+
}
29+
fn to_bytes(&self, buf: &mut [u8]) {
30+
(*self.left.borrow()).to_bytes(&mut buf[0..8]);
31+
(*self.right.borrow()).to_bytes(&mut buf[8..16]);
32+
(*self.value.borrow()).to_bytes(&mut buf[16..20]);
33+
}
34+
fn from_bytes(buf: &[u8]) -> Self {
35+
Self {
36+
left: Rc::new(RefCell::new(<Ptr<node_t>>::from_bytes(&buf[0..8]))),
37+
right: Rc::new(RefCell::new(<Ptr<node_t>>::from_bytes(&buf[8..16]))),
38+
value: Rc::new(RefCell::new(<i32>::from_bytes(&buf[16..20]))),
39+
}
40+
}
41+
}
2642
pub fn find_0(node: Ptr<node_t>, value: i32) -> Ptr<node_t> {
2743
let node: Value<Ptr<node_t>> = Rc::new(RefCell::new(node));
2844
let value: Value<i32> = Rc::new(RefCell::new(value));

tests/unit/out/refcount/c_struct.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,21 @@ pub struct Node {
5151
pub value: Value<i32>,
5252
pub next: Value<Ptr<Node>>,
5353
}
54-
impl ByteRepr for Node {}
54+
impl ByteRepr for Node {
55+
fn byte_size() -> usize {
56+
16
57+
}
58+
fn to_bytes(&self, buf: &mut [u8]) {
59+
(*self.value.borrow()).to_bytes(&mut buf[0..4]);
60+
(*self.next.borrow()).to_bytes(&mut buf[8..16]);
61+
}
62+
fn from_bytes(buf: &[u8]) -> Self {
63+
Self {
64+
value: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
65+
next: Rc::new(RefCell::new(<Ptr<Node>>::from_bytes(&buf[8..16]))),
66+
}
67+
}
68+
}
5569
#[derive(Clone, Copy, PartialEq, Debug, Default)]
5670
enum Color {
5771
#[default]

tests/unit/out/refcount/complex_function.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,19 @@ impl Clone for X3 {
7777
this
7878
}
7979
}
80-
impl ByteRepr for X3 {}
80+
impl ByteRepr for X3 {
81+
fn byte_size() -> usize {
82+
8
83+
}
84+
fn to_bytes(&self, buf: &mut [u8]) {
85+
(*self.v.borrow()).to_bytes(&mut buf[0..8]);
86+
}
87+
fn from_bytes(buf: &[u8]) -> Self {
88+
Self {
89+
v: Rc::new(RefCell::new(<Ptr<X2>>::from_bytes(&buf[0..8]))),
90+
}
91+
}
92+
}
8193
#[derive(Default)]
8294
pub struct X4 {
8395
pub v: Value<X3>,
@@ -95,7 +107,19 @@ impl Clone for X4 {
95107
this
96108
}
97109
}
98-
impl ByteRepr for X4 {}
110+
impl ByteRepr for X4 {
111+
fn byte_size() -> usize {
112+
8
113+
}
114+
fn to_bytes(&self, buf: &mut [u8]) {
115+
(*self.v.borrow()).to_bytes(&mut buf[0..8]);
116+
}
117+
fn from_bytes(buf: &[u8]) -> Self {
118+
Self {
119+
v: Rc::new(RefCell::new(<X3>::from_bytes(&buf[0..8]))),
120+
}
121+
}
122+
}
99123
pub fn main() {
100124
std::process::exit(main_0());
101125
}

tests/unit/out/refcount/default.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,27 @@ impl Default for Pointers {
4545
}
4646
}
4747
}
48-
impl ByteRepr for Pointers {}
48+
impl ByteRepr for Pointers {
49+
fn byte_size() -> usize {
50+
144
51+
}
52+
fn to_bytes(&self, buf: &mut [u8]) {
53+
(*self.x1.borrow()).to_bytes(&mut buf[0..8]);
54+
(*self.x2.borrow()).to_bytes(&mut buf[8..16]);
55+
(*self.x3.borrow()).to_bytes(&mut buf[16..56]);
56+
(*self.x4.borrow()).to_bytes(&mut buf[56..136]);
57+
(*self.x5.borrow()).to_bytes(&mut buf[136..140]);
58+
}
59+
fn from_bytes(buf: &[u8]) -> Self {
60+
Self {
61+
x1: Rc::new(RefCell::new(<Ptr<i32>>::from_bytes(&buf[0..8]))),
62+
x2: Rc::new(RefCell::new(<Ptr<i32>>::from_bytes(&buf[8..16]))),
63+
x3: Rc::new(RefCell::new(<Box<[Ptr<i32>]>>::from_bytes(&buf[16..56]))),
64+
x4: Rc::new(RefCell::new(<Box<[Ptr<i32>]>>::from_bytes(&buf[56..136]))),
65+
x5: Rc::new(RefCell::new(<i32>::from_bytes(&buf[136..140]))),
66+
}
67+
}
68+
}
4969
pub fn main() {
5070
std::process::exit(main_0());
5171
}

tests/unit/out/refcount/default_in_statics.rs

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,21 @@ impl Clone for Inner {
2020
this
2121
}
2222
}
23-
impl ByteRepr for Inner {}
23+
impl ByteRepr for Inner {
24+
fn byte_size() -> usize {
25+
16
26+
}
27+
fn to_bytes(&self, buf: &mut [u8]) {
28+
(*self.v.borrow()).to_bytes(&mut buf[0..4]);
29+
(*self.name.borrow()).to_bytes(&mut buf[8..16]);
30+
}
31+
fn from_bytes(buf: &[u8]) -> Self {
32+
Self {
33+
v: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
34+
name: Rc::new(RefCell::new(<Ptr<u8>>::from_bytes(&buf[8..16]))),
35+
}
36+
}
37+
}
2438
#[derive()]
2539
pub struct Outer {
2640
pub p1: Value<Ptr<i32>>,
@@ -65,7 +79,35 @@ impl Default for Outer {
6579
}
6680
}
6781
}
68-
impl ByteRepr for Outer {}
82+
impl ByteRepr for Outer {
83+
fn byte_size() -> usize {
84+
88
85+
}
86+
fn to_bytes(&self, buf: &mut [u8]) {
87+
(*self.p1.borrow()).to_bytes(&mut buf[0..8]);
88+
(*self.p2.borrow()).to_bytes(&mut buf[8..16]);
89+
(*self.arr.borrow()).to_bytes(&mut buf[16..40]);
90+
(*self.cp.borrow()).to_bytes(&mut buf[40..48]);
91+
(*self.pp.borrow()).to_bytes(&mut buf[48..56]);
92+
(*self.inner.borrow()).to_bytes(&mut buf[56..72]);
93+
(*self.x.borrow()).to_bytes(&mut buf[72..76]);
94+
(*self.fn_.borrow()).to_bytes(&mut buf[80..88]);
95+
}
96+
fn from_bytes(buf: &[u8]) -> Self {
97+
Self {
98+
p1: Rc::new(RefCell::new(<Ptr<i32>>::from_bytes(&buf[0..8]))),
99+
p2: Rc::new(RefCell::new(<Ptr<i32>>::from_bytes(&buf[8..16]))),
100+
arr: Rc::new(RefCell::new(<Box<[Ptr<i32>]>>::from_bytes(&buf[16..40]))),
101+
cp: Rc::new(RefCell::new(<Ptr<u8>>::from_bytes(&buf[40..48]))),
102+
pp: Rc::new(RefCell::new(<Ptr<Ptr<i32>>>::from_bytes(&buf[48..56]))),
103+
inner: Rc::new(RefCell::new(<Inner>::from_bytes(&buf[56..72]))),
104+
x: Rc::new(RefCell::new(<i32>::from_bytes(&buf[72..76]))),
105+
fn_: Rc::new(RefCell::new(<FnPtr<fn(i32) -> i32>>::from_bytes(
106+
&buf[80..88],
107+
))),
108+
}
109+
}
110+
}
69111
#[derive()]
70112
pub struct Foo {
71113
pub s1: Value<Ptr<u8>>,
@@ -97,7 +139,31 @@ impl Default for Foo {
97139
}
98140
}
99141
}
100-
impl ByteRepr for Foo {}
142+
impl ByteRepr for Foo {
143+
fn byte_size() -> usize {
144+
40
145+
}
146+
fn to_bytes(&self, buf: &mut [u8]) {
147+
(*self.s1.borrow()).to_bytes(&mut buf[0..8]);
148+
(*self.s2.borrow()).to_bytes(&mut buf[8..16]);
149+
(*self.fn1.borrow()).to_bytes(&mut buf[16..24]);
150+
(*self.fn2.borrow()).to_bytes(&mut buf[24..32]);
151+
(*self.n.borrow()).to_bytes(&mut buf[32..36]);
152+
}
153+
fn from_bytes(buf: &[u8]) -> Self {
154+
Self {
155+
s1: Rc::new(RefCell::new(<Ptr<u8>>::from_bytes(&buf[0..8]))),
156+
s2: Rc::new(RefCell::new(<Ptr<u8>>::from_bytes(&buf[8..16]))),
157+
fn1: Rc::new(RefCell::new(<FnPtr<fn(i32) -> i32>>::from_bytes(
158+
&buf[16..24],
159+
))),
160+
fn2: Rc::new(RefCell::new(<FnPtr<fn(i32) -> i32>>::from_bytes(
161+
&buf[24..32],
162+
))),
163+
n: Rc::new(RefCell::new(<i32>::from_bytes(&buf[32..36]))),
164+
}
165+
}
166+
}
101167
thread_local!(
102168
pub static static_fn_0: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(FnPtr::null()));
103169
);

0 commit comments

Comments
 (0)