Skip to content

Commit 5c99fd1

Browse files
authored
Allow records with system header fields to implement ByteRepr (#300)
1 parent 60550b0 commit 5c99fd1

17 files changed

Lines changed: 306 additions & 82 deletions

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,6 @@ bool TypeImplementsByteRepr(clang::QualType qt) {
208208
return TypeImplementsByteRepr(arr->getElementType());
209209
}
210210
if (const auto *rd = qt->getAsRecordDecl()) {
211-
if (rd->getASTContext().getSourceManager().isInSystemHeader(
212-
rd->getLocation())) {
213-
return false;
214-
}
215211
if (rd->isUnion()) {
216212
return true;
217213
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,21 @@ impl Clone for NonCopy {
2020
this
2121
}
2222
}
23-
impl ByteRepr for NonCopy {}
23+
impl ByteRepr for NonCopy {
24+
fn byte_size() -> usize {
25+
32
26+
}
27+
fn to_bytes(&self, buf: &mut [u8]) {
28+
(*self.data.borrow()).to_bytes(&mut buf[0..24]);
29+
(*self.tag.borrow()).to_bytes(&mut buf[24..28]);
30+
}
31+
fn from_bytes(buf: &[u8]) -> Self {
32+
Self {
33+
data: Rc::new(RefCell::new(<Vec<i32>>::from_bytes(&buf[0..24]))),
34+
tag: Rc::new(RefCell::new(<i32>::from_bytes(&buf[24..28]))),
35+
}
36+
}
37+
}
2438
pub fn main() {
2539
std::process::exit(main_0());
2640
}

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,19 @@ impl Clone for MyContainer_int_ {
4242
this
4343
}
4444
}
45-
impl ByteRepr for MyContainer_int_ {}
45+
impl ByteRepr for MyContainer_int_ {
46+
fn byte_size() -> usize {
47+
24
48+
}
49+
fn to_bytes(&self, buf: &mut [u8]) {
50+
(*self.vec_.borrow()).to_bytes(&mut buf[0..24]);
51+
}
52+
fn from_bytes(buf: &[u8]) -> Self {
53+
Self {
54+
vec_: Rc::new(RefCell::new(<Vec<i32>>::from_bytes(&buf[0..24]))),
55+
}
56+
}
57+
}
4658
#[derive(Default)]
4759
pub struct MyContainer_char_ {
4860
vec_: Value<Vec<u8>>,
@@ -79,7 +91,19 @@ impl Clone for MyContainer_char_ {
7991
this
8092
}
8193
}
82-
impl ByteRepr for MyContainer_char_ {}
94+
impl ByteRepr for MyContainer_char_ {
95+
fn byte_size() -> usize {
96+
24
97+
}
98+
fn to_bytes(&self, buf: &mut [u8]) {
99+
(*self.vec_.borrow()).to_bytes(&mut buf[0..24]);
100+
}
101+
fn from_bytes(buf: &[u8]) -> Self {
102+
Self {
103+
vec_: Rc::new(RefCell::new(<Vec<u8>>::from_bytes(&buf[0..24]))),
104+
}
105+
}
106+
}
83107
#[derive(Default)]
84108
pub struct MyContainer_float_ {
85109
vec_: Value<Vec<f32>>,
@@ -116,7 +140,19 @@ impl Clone for MyContainer_float_ {
116140
this
117141
}
118142
}
119-
impl ByteRepr for MyContainer_float_ {}
143+
impl ByteRepr for MyContainer_float_ {
144+
fn byte_size() -> usize {
145+
24
146+
}
147+
fn to_bytes(&self, buf: &mut [u8]) {
148+
(*self.vec_.borrow()).to_bytes(&mut buf[0..24]);
149+
}
150+
fn from_bytes(buf: &[u8]) -> Self {
151+
Self {
152+
vec_: Rc::new(RefCell::new(<Vec<f32>>::from_bytes(&buf[0..24]))),
153+
}
154+
}
155+
}
120156
pub fn main() {
121157
std::process::exit(main_0());
122158
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,21 @@ impl Clone for S {
2020
this
2121
}
2222
}
23-
impl ByteRepr for S {}
23+
impl ByteRepr for S {
24+
fn byte_size() -> usize {
25+
32
26+
}
27+
fn to_bytes(&self, buf: &mut [u8]) {
28+
(*self.v.borrow()).to_bytes(&mut buf[0..24]);
29+
(*self.a.borrow()).to_bytes(&mut buf[24..28]);
30+
}
31+
fn from_bytes(buf: &[u8]) -> Self {
32+
Self {
33+
v: Rc::new(RefCell::new(<Vec<i32>>::from_bytes(&buf[0..24]))),
34+
a: Rc::new(RefCell::new(<i32>::from_bytes(&buf[24..28]))),
35+
}
36+
}
37+
}
2438
pub fn main() {
2539
std::process::exit(main_0());
2640
}

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,31 @@ impl MinHeap {
218218
}
219219
}
220220
}
221-
impl ByteRepr for MinHeap {}
221+
impl ByteRepr for MinHeap {
222+
fn byte_size() -> usize {
223+
32
224+
}
225+
fn to_bytes(&self, buf: &mut [u8]) {
226+
(*self.size.borrow()).to_bytes(&mut buf[0..4]);
227+
(*self.capacity.borrow()).to_bytes(&mut buf[4..8]);
228+
(*self.arr.borrow()).to_bytes(&mut buf[8..16]);
229+
(*self.next.borrow()).to_bytes(&mut buf[16..20]);
230+
(*self.alloc.borrow()).to_bytes(&mut buf[24..32]);
231+
}
232+
fn from_bytes(buf: &[u8]) -> Self {
233+
Self {
234+
size: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
235+
capacity: Rc::new(RefCell::new(<i32>::from_bytes(&buf[4..8]))),
236+
arr: Rc::new(RefCell::new(
237+
<Option<Value<Box<[Ptr<MinHeapNode>]>>>>::from_bytes(&buf[8..16]),
238+
)),
239+
next: Rc::new(RefCell::new(<i32>::from_bytes(&buf[16..20]))),
240+
alloc: Rc::new(RefCell::new(
241+
<Option<Value<Box<[MinHeapNode]>>>>::from_bytes(&buf[24..32]),
242+
)),
243+
}
244+
}
245+
}
222246
pub fn AllocMinHeap_1(capacity: i32) -> Option<Value<MinHeap>> {
223247
let capacity: Value<i32> = Rc::new(RefCell::new(capacity));
224248
let minHeap: Value<Option<Value<MinHeap>>> =

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,19 @@ impl Clone for Holder {
1818
this
1919
}
2020
}
21-
impl ByteRepr for Holder {}
21+
impl ByteRepr for Holder {
22+
fn byte_size() -> usize {
23+
24
24+
}
25+
fn to_bytes(&self, buf: &mut [u8]) {
26+
(*self.v.borrow()).to_bytes(&mut buf[0..24]);
27+
}
28+
fn from_bytes(buf: &[u8]) -> Self {
29+
Self {
30+
v: Rc::new(RefCell::new(<Vec<i32>>::from_bytes(&buf[0..24]))),
31+
}
32+
}
33+
}
2234
pub fn write_through_0(p: Ptr<i32>) {
2335
let p: Value<Ptr<i32>> = Rc::new(RefCell::new(p));
2436
(*p.borrow()).write(42);

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,52 @@ impl DisjointSet {
280280
}
281281
}
282282
}
283-
impl ByteRepr for DisjointSet {}
283+
impl ByteRepr for DisjointSet {
284+
fn byte_size() -> usize {
285+
24
286+
}
287+
fn to_bytes(&self, buf: &mut [u8]) {
288+
(*self.rank.borrow()).to_bytes(&mut buf[0..8]);
289+
(*self.parent.borrow()).to_bytes(&mut buf[8..16]);
290+
(*self.n.borrow()).to_bytes(&mut buf[16..20]);
291+
}
292+
fn from_bytes(buf: &[u8]) -> Self {
293+
Self {
294+
rank: Rc::new(RefCell::new(<Option<Value<Box<[i32]>>>>::from_bytes(
295+
&buf[0..8],
296+
))),
297+
parent: Rc::new(RefCell::new(<Option<Value<Box<[i32]>>>>::from_bytes(
298+
&buf[8..16],
299+
))),
300+
n: Rc::new(RefCell::new(<i32>::from_bytes(&buf[16..20]))),
301+
}
302+
}
303+
}
284304
#[derive(Default)]
285305
pub struct Graph {
286306
pub edges: Value<Option<Value<Box<[Edge]>>>>,
287307
pub V: Value<i32>,
288308
pub E: Value<i32>,
289309
}
290-
impl ByteRepr for Graph {}
310+
impl ByteRepr for Graph {
311+
fn byte_size() -> usize {
312+
16
313+
}
314+
fn to_bytes(&self, buf: &mut [u8]) {
315+
(*self.edges.borrow()).to_bytes(&mut buf[0..8]);
316+
(*self.V.borrow()).to_bytes(&mut buf[8..12]);
317+
(*self.E.borrow()).to_bytes(&mut buf[12..16]);
318+
}
319+
fn from_bytes(buf: &[u8]) -> Self {
320+
Self {
321+
edges: Rc::new(RefCell::new(<Option<Value<Box<[Edge]>>>>::from_bytes(
322+
&buf[0..8],
323+
))),
324+
V: Rc::new(RefCell::new(<i32>::from_bytes(&buf[8..12]))),
325+
E: Rc::new(RefCell::new(<i32>::from_bytes(&buf[12..16]))),
326+
}
327+
}
328+
}
291329
pub fn MSTKruskal_2(graph: Ptr<Graph>) -> f64 {
292330
({
293331
let _arr: Ptr<Option<Value<Box<[Edge]>>>> = (*graph.upgrade().deref()).edges.as_pointer();

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,21 @@ impl Default for UserDefined {
3030
}
3131
}
3232
}
33-
impl ByteRepr for UserDefined {}
33+
impl ByteRepr for UserDefined {
34+
fn byte_size() -> usize {
35+
32
36+
}
37+
fn to_bytes(&self, buf: &mut [u8]) {
38+
(*self.a.borrow()).to_bytes(&mut buf[0..4]);
39+
(*self.v.borrow()).to_bytes(&mut buf[8..32]);
40+
}
41+
fn from_bytes(buf: &[u8]) -> Self {
42+
Self {
43+
a: Rc::new(RefCell::new(<Vec<i32>>::from_bytes(&buf[0..4]))),
44+
v: Rc::new(RefCell::new(<Vec<i32>>::from_bytes(&buf[8..32]))),
45+
}
46+
}
47+
}
3448
#[derive()]
3549
pub struct FieldIsLibcType {
3650
pub addr: Value<libcc2rs::Sockaddr>,
@@ -50,7 +64,19 @@ impl Default for FieldIsLibcType {
5064
}
5165
}
5266
}
53-
impl ByteRepr for FieldIsLibcType {}
67+
impl ByteRepr for FieldIsLibcType {
68+
fn byte_size() -> usize {
69+
16
70+
}
71+
fn to_bytes(&self, buf: &mut [u8]) {
72+
(*self.addr.borrow()).to_bytes(&mut buf[0..16]);
73+
}
74+
fn from_bytes(buf: &[u8]) -> Self {
75+
Self {
76+
addr: Rc::new(RefCell::new(<libcc2rs::Sockaddr>::from_bytes(&buf[0..16]))),
77+
}
78+
}
79+
}
5480
pub fn main() {
5581
std::process::exit(main_0());
5682
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,23 @@ impl Clone for JPEGData {
8484
this
8585
}
8686
}
87-
impl ByteRepr for JPEGData {}
87+
impl ByteRepr for JPEGData {
88+
fn byte_size() -> usize {
89+
48
90+
}
91+
fn to_bytes(&self, buf: &mut [u8]) {
92+
(*self.com_data.borrow()).to_bytes(&mut buf[0..24]);
93+
(*self.app_data.borrow()).to_bytes(&mut buf[24..48]);
94+
}
95+
fn from_bytes(buf: &[u8]) -> Self {
96+
Self {
97+
com_data: Rc::new(RefCell::new(<Vec<Value<Vec<u8>>>>::from_bytes(&buf[0..24]))),
98+
app_data: Rc::new(RefCell::new(<Vec<Value<Vec<u8>>>>::from_bytes(
99+
&buf[24..48],
100+
))),
101+
}
102+
}
103+
}
88104
pub fn push_param_0(dest: Ptr<Vec<Value<Vec<u8>>>>) {
89105
let dest: Value<Ptr<Vec<Value<Vec<u8>>>>> = Rc::new(RefCell::new(dest));
90106
((*dest.borrow()).to_strong().as_pointer() as Ptr<Vec<Value<Vec<u8>>>>).with_mut(

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

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,54 @@ use std::io::{Read, Seek, Write};
77
use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
99
#[derive()]
10-
pub struct WOFF2Params {
11-
pub extended_metadata: Value<Vec<u8>>,
12-
pub brotli_quality: Value<i32>,
13-
pub allow_transforms: Value<bool>,
10+
pub struct S {
11+
pub a: Value<i32>,
12+
pub b: Value<bool>,
1413
}
15-
impl WOFF2Params {
16-
pub fn WOFF2Params() -> Self {
14+
impl S {
15+
pub fn S() -> Self {
1716
let mut this = Self {
18-
extended_metadata: Rc::new(RefCell::new(
19-
Ptr::from_string_literal(b"")
20-
.to_c_string_iterator()
21-
.chain(std::iter::once(0))
22-
.collect::<Vec<u8>>(),
23-
)),
24-
brotli_quality: Rc::new(RefCell::new(11)),
25-
allow_transforms: Rc::new(RefCell::new(true)),
17+
a: Rc::new(RefCell::new(11)),
18+
b: Rc::new(RefCell::new(true)),
2619
};
2720
this
2821
}
2922
}
30-
impl Clone for WOFF2Params {
23+
impl Clone for S {
3124
fn clone(&self) -> Self {
3225
let mut this = Self {
33-
extended_metadata: Rc::new(RefCell::new((*self.extended_metadata.borrow()).clone())),
34-
brotli_quality: Rc::new(RefCell::new((*self.brotli_quality.borrow()))),
35-
allow_transforms: Rc::new(RefCell::new((*self.allow_transforms.borrow()))),
26+
a: Rc::new(RefCell::new((*self.a.borrow()))),
27+
b: Rc::new(RefCell::new((*self.b.borrow()))),
3628
};
3729
this
3830
}
3931
}
40-
impl Default for WOFF2Params {
32+
impl Default for S {
4133
fn default() -> Self {
42-
{ WOFF2Params::WOFF2Params() }
34+
{ S::S() }
35+
}
36+
}
37+
impl ByteRepr for S {
38+
fn byte_size() -> usize {
39+
8
40+
}
41+
fn to_bytes(&self, buf: &mut [u8]) {
42+
(*self.a.borrow()).to_bytes(&mut buf[0..4]);
43+
(*self.b.borrow()).to_bytes(&mut buf[4..5]);
44+
}
45+
fn from_bytes(buf: &[u8]) -> Self {
46+
Self {
47+
a: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
48+
b: Rc::new(RefCell::new(<bool>::from_bytes(&buf[4..5]))),
49+
}
4350
}
4451
}
45-
impl ByteRepr for WOFF2Params {}
4652
pub fn main() {
4753
std::process::exit(main_0());
4854
}
4955
fn main_0() -> i32 {
50-
let params: Value<WOFF2Params> = Rc::new(RefCell::new(WOFF2Params::WOFF2Params()));
51-
assert!((((*(*params.borrow()).extended_metadata.borrow()).len() - 1) == 0_usize));
52-
assert!(((*(*params.borrow()).brotli_quality.borrow()) == 11));
53-
assert!((((*(*params.borrow()).allow_transforms.borrow()) as i32) == (true as i32)));
56+
let s: Value<S> = Rc::new(RefCell::new(S::S()));
57+
assert!(((*(*s.borrow()).a.borrow()) == 11));
58+
assert!((((*(*s.borrow()).b.borrow()) as i32) == (true as i32)));
5459
return 0;
5560
}

0 commit comments

Comments
 (0)