Skip to content

Commit 0dd222b

Browse files
committed
Allow records with system header fields to implement ByteRepr
1 parent ed3c7bf commit 0dd222b

14 files changed

Lines changed: 1046 additions & 725 deletions

‎cpp2rust/converter/converter_lib.cpp‎

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

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

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,36 @@ use std::io::prelude::*;
66
use std::io::{Read, Seek, Write};
77
use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
9+
#[repr(C)]
910
#[derive(Default)]
1011
pub struct NonCopy {
11-
pub data: Value<Vec<i32>>,
12-
pub tag: Value<i32>,
12+
pub data: Vec<i32>,
13+
pub tag: i32,
1314
}
1415
impl Clone for NonCopy {
1516
fn clone(&self) -> Self {
1617
let mut this = Self {
17-
data: Rc::new(RefCell::new((*self.data.borrow()).clone())),
18-
tag: Rc::new(RefCell::new((*self.tag.borrow()))),
18+
data: (self.data).clone(),
19+
tag: self.tag,
1920
};
2021
this
2122
}
2223
}
23-
impl ByteRepr for NonCopy {}
24+
impl ByteRepr for NonCopy {
25+
fn byte_size() -> usize {
26+
32
27+
}
28+
fn to_bytes(&self, buf: &mut [u8]) {
29+
self.data.to_bytes(&mut buf[0..24]);
30+
self.tag.to_bytes(&mut buf[24..28]);
31+
}
32+
fn from_bytes(buf: &[u8]) -> Self {
33+
Self {
34+
data: <Vec<i32>>::from_bytes(&buf[0..24]),
35+
tag: <i32>::from_bytes(&buf[24..28]),
36+
}
37+
}
38+
}
2439
pub fn main() {
2540
std::process::exit(main_0());
2641
}
@@ -30,17 +45,21 @@ fn main_0() -> i32 {
3045
.map(|_| <NonCopy>::default())
3146
.collect::<Box<[NonCopy]>>(),
3247
));
33-
(*(*arr.borrow())[(0) as usize].tag.borrow_mut()) = 7;
34-
(*(*arr.borrow())[(1) as usize].data.borrow_mut()).push(42);
35-
assert!(((*(*arr.borrow())[(0) as usize].tag.borrow()) == 7));
36-
assert!(((*(*arr.borrow())[(1) as usize].data.borrow()).len() == 1_usize));
48+
(*arr.borrow_mut())[(0) as usize].tag = 7;
49+
(*arr.borrow_mut())[(1) as usize].data.push(42);
50+
assert!(((*arr.borrow())[(0) as usize].tag == 7));
51+
assert!(((*arr.borrow())[(1) as usize].data.len() == 1_usize));
3752
assert!(
38-
((((*arr.borrow())[(1) as usize].data.as_pointer() as Ptr<i32>)
53+
((((arr.as_pointer() as Ptr<NonCopy>).offset(1).field_ptr(
54+
0,
55+
|__v: &NonCopy| &__v.data[..],
56+
|__v: &mut NonCopy| &mut __v.data[..]
57+
) as Ptr<i32>)
3958
.offset(0_usize)
4059
.read())
4160
== 42)
4261
);
43-
assert!(((*(*arr.borrow())[(2) as usize].tag.borrow()) == 0));
44-
assert!(((*(*arr.borrow())[(2) as usize].data.borrow()).len() == 0_usize));
62+
assert!(((*arr.borrow())[(2) as usize].tag == 0));
63+
assert!(((*arr.borrow())[(2) as usize].data.len() == 0_usize));
4564
return 0;
4665
}

0 commit comments

Comments
 (0)