Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ bool TypeImplementsByteRepr(clang::QualType qt) {
if (qt->isIntegerType() || qt->isFloatingType() || qt->isEnumeralType()) {
return true;
}
if (qt->isPointerType()) {
return true;
}
if (const auto *arr = qt->getAsArrayTypeUnsafe()) {
return TypeImplementsByteRepr(arr->getElementType());
}
Expand Down
1 change: 1 addition & 0 deletions libcc2rs/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,7 @@ impl<T: ?Sized> AsPointerDyn<T> for Rc<RefCell<T>> {
}

impl<T: 'static> ByteRepr for Ptr<T> {}
impl ByteRepr for AnyPtr {}

#[cfg(test)]
mod tests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,21 @@ pub struct container {
pub p: Value<Ptr<opaque>>,
pub x: Value<i32>,
}
impl ByteRepr for container {}
impl ByteRepr for container {
fn byte_size() -> usize {
16
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.p.borrow()).to_bytes(&mut buf[0..8]);
(*self.x.borrow()).to_bytes(&mut buf[8..12]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
p: Rc::new(RefCell::new(<Ptr<opaque>>::from_bytes(&buf[0..8]))),
x: Rc::new(RefCell::new(<i32>::from_bytes(&buf[8..12]))),
}
}
}
pub fn main() {
std::process::exit(main_0());
}
Expand Down
32 changes: 30 additions & 2 deletions tests/unit/out/refcount/10_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,21 @@ impl Clone for GraphNode {
this
}
}
impl ByteRepr for GraphNode {}
impl ByteRepr for GraphNode {
fn byte_size() -> usize {
16
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.dst.borrow()).to_bytes(&mut buf[0..4]);
(*self.next.borrow()).to_bytes(&mut buf[8..16]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
dst: Rc::new(RefCell::new(<u32>::from_bytes(&buf[0..4]))),
next: Rc::new(RefCell::new(<Ptr<GraphNode>>::from_bytes(&buf[8..16]))),
}
}
}
#[derive(Default)]
pub struct Graph {
pub V: Value<u32>,
Expand Down Expand Up @@ -59,7 +73,21 @@ impl Clone for Graph {
this
}
}
impl ByteRepr for Graph {}
impl ByteRepr for Graph {
fn byte_size() -> usize {
16
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.V.borrow()).to_bytes(&mut buf[0..4]);
(*self.adj.borrow()).to_bytes(&mut buf[8..16]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
V: Rc::new(RefCell::new(<u32>::from_bytes(&buf[0..4]))),
adj: Rc::new(RefCell::new(<Ptr<Ptr<GraphNode>>>::from_bytes(&buf[8..16]))),
}
}
}
pub fn main() {
std::process::exit(main_0());
}
Expand Down
14 changes: 13 additions & 1 deletion tests/unit/out/refcount/addr_of_global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,19 @@ impl Clone for Outer {
this
}
}
impl ByteRepr for Outer {}
impl ByteRepr for Outer {
fn byte_size() -> usize {
8
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.p.borrow()).to_bytes(&mut buf[0..8]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
p: Rc::new(RefCell::new(<Ptr<Inner>>::from_bytes(&buf[0..8]))),
}
}
}
thread_local!(
pub static alpha_0: Value<Inner> = Rc::new(RefCell::new(Inner {
value: Rc::new(RefCell::new(1)),
Expand Down
18 changes: 17 additions & 1 deletion tests/unit/out/refcount/bst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,23 @@ impl Clone for node_t {
this
}
}
impl ByteRepr for node_t {}
impl ByteRepr for node_t {
fn byte_size() -> usize {
24
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.left.borrow()).to_bytes(&mut buf[0..8]);
(*self.right.borrow()).to_bytes(&mut buf[8..16]);
(*self.value.borrow()).to_bytes(&mut buf[16..20]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
left: Rc::new(RefCell::new(<Ptr<node_t>>::from_bytes(&buf[0..8]))),
right: Rc::new(RefCell::new(<Ptr<node_t>>::from_bytes(&buf[8..16]))),
value: Rc::new(RefCell::new(<i32>::from_bytes(&buf[16..20]))),
}
}
}
pub fn find_0(node: Ptr<node_t>, value: i32) -> Ptr<node_t> {
let node: Value<Ptr<node_t>> = Rc::new(RefCell::new(node));
let value: Value<i32> = Rc::new(RefCell::new(value));
Expand Down
16 changes: 15 additions & 1 deletion tests/unit/out/refcount/c_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,21 @@ pub struct Node {
pub value: Value<i32>,
pub next: Value<Ptr<Node>>,
}
impl ByteRepr for Node {}
impl ByteRepr for Node {
fn byte_size() -> usize {
16
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.value.borrow()).to_bytes(&mut buf[0..4]);
(*self.next.borrow()).to_bytes(&mut buf[8..16]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
value: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
next: Rc::new(RefCell::new(<Ptr<Node>>::from_bytes(&buf[8..16]))),
}
}
}
#[derive(Clone, Copy, PartialEq, Debug, Default)]
enum Color {
#[default]
Expand Down
28 changes: 26 additions & 2 deletions tests/unit/out/refcount/complex_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,19 @@ impl Clone for X3 {
this
}
}
impl ByteRepr for X3 {}
impl ByteRepr for X3 {
fn byte_size() -> usize {
8
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.v.borrow()).to_bytes(&mut buf[0..8]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
v: Rc::new(RefCell::new(<Ptr<X2>>::from_bytes(&buf[0..8]))),
}
}
}
#[derive(Default)]
pub struct X4 {
pub v: Value<X3>,
Expand All @@ -95,7 +107,19 @@ impl Clone for X4 {
this
}
}
impl ByteRepr for X4 {}
impl ByteRepr for X4 {
fn byte_size() -> usize {
8
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.v.borrow()).to_bytes(&mut buf[0..8]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
v: Rc::new(RefCell::new(<X3>::from_bytes(&buf[0..8]))),
}
}
}
pub fn main() {
std::process::exit(main_0());
}
Expand Down
22 changes: 21 additions & 1 deletion tests/unit/out/refcount/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,27 @@ impl Default for Pointers {
}
}
}
impl ByteRepr for Pointers {}
impl ByteRepr for Pointers {
fn byte_size() -> usize {
144
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.x1.borrow()).to_bytes(&mut buf[0..8]);
(*self.x2.borrow()).to_bytes(&mut buf[8..16]);
(*self.x3.borrow()).to_bytes(&mut buf[16..56]);
(*self.x4.borrow()).to_bytes(&mut buf[56..136]);
(*self.x5.borrow()).to_bytes(&mut buf[136..140]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
x1: Rc::new(RefCell::new(<Ptr<i32>>::from_bytes(&buf[0..8]))),
x2: Rc::new(RefCell::new(<Ptr<i32>>::from_bytes(&buf[8..16]))),
x3: Rc::new(RefCell::new(<Box<[Ptr<i32>]>>::from_bytes(&buf[16..56]))),
x4: Rc::new(RefCell::new(<Box<[Ptr<i32>]>>::from_bytes(&buf[56..136]))),
x5: Rc::new(RefCell::new(<i32>::from_bytes(&buf[136..140]))),
}
}
}
pub fn main() {
std::process::exit(main_0());
}
Expand Down
72 changes: 69 additions & 3 deletions tests/unit/out/refcount/default_in_statics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,21 @@ impl Clone for Inner {
this
}
}
impl ByteRepr for Inner {}
impl ByteRepr for Inner {
fn byte_size() -> usize {
16
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.v.borrow()).to_bytes(&mut buf[0..4]);
(*self.name.borrow()).to_bytes(&mut buf[8..16]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
v: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
name: Rc::new(RefCell::new(<Ptr<u8>>::from_bytes(&buf[8..16]))),
}
}
}
#[derive()]
pub struct Outer {
pub p1: Value<Ptr<i32>>,
Expand Down Expand Up @@ -65,7 +79,35 @@ impl Default for Outer {
}
}
}
impl ByteRepr for Outer {}
impl ByteRepr for Outer {
fn byte_size() -> usize {
88
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.p1.borrow()).to_bytes(&mut buf[0..8]);
(*self.p2.borrow()).to_bytes(&mut buf[8..16]);
(*self.arr.borrow()).to_bytes(&mut buf[16..40]);
(*self.cp.borrow()).to_bytes(&mut buf[40..48]);
(*self.pp.borrow()).to_bytes(&mut buf[48..56]);
(*self.inner.borrow()).to_bytes(&mut buf[56..72]);
(*self.x.borrow()).to_bytes(&mut buf[72..76]);
(*self.fn_.borrow()).to_bytes(&mut buf[80..88]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
p1: Rc::new(RefCell::new(<Ptr<i32>>::from_bytes(&buf[0..8]))),
p2: Rc::new(RefCell::new(<Ptr<i32>>::from_bytes(&buf[8..16]))),
arr: Rc::new(RefCell::new(<Box<[Ptr<i32>]>>::from_bytes(&buf[16..40]))),
cp: Rc::new(RefCell::new(<Ptr<u8>>::from_bytes(&buf[40..48]))),
pp: Rc::new(RefCell::new(<Ptr<Ptr<i32>>>::from_bytes(&buf[48..56]))),
inner: Rc::new(RefCell::new(<Inner>::from_bytes(&buf[56..72]))),
x: Rc::new(RefCell::new(<i32>::from_bytes(&buf[72..76]))),
fn_: Rc::new(RefCell::new(<FnPtr<fn(i32) -> i32>>::from_bytes(
&buf[80..88],
))),
}
}
}
#[derive()]
pub struct Foo {
pub s1: Value<Ptr<u8>>,
Expand Down Expand Up @@ -97,7 +139,31 @@ impl Default for Foo {
}
}
}
impl ByteRepr for Foo {}
impl ByteRepr for Foo {
fn byte_size() -> usize {
40
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.s1.borrow()).to_bytes(&mut buf[0..8]);
(*self.s2.borrow()).to_bytes(&mut buf[8..16]);
(*self.fn1.borrow()).to_bytes(&mut buf[16..24]);
(*self.fn2.borrow()).to_bytes(&mut buf[24..32]);
(*self.n.borrow()).to_bytes(&mut buf[32..36]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
s1: Rc::new(RefCell::new(<Ptr<u8>>::from_bytes(&buf[0..8]))),
s2: Rc::new(RefCell::new(<Ptr<u8>>::from_bytes(&buf[8..16]))),
fn1: Rc::new(RefCell::new(<FnPtr<fn(i32) -> i32>>::from_bytes(
&buf[16..24],
))),
fn2: Rc::new(RefCell::new(<FnPtr<fn(i32) -> i32>>::from_bytes(
&buf[24..32],
))),
n: Rc::new(RefCell::new(<i32>::from_bytes(&buf[32..36]))),
}
}
}
thread_local!(
pub static static_fn_0: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(FnPtr::null()));
);
Expand Down
18 changes: 17 additions & 1 deletion tests/unit/out/refcount/doubly_linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,23 @@ impl Clone for Node {
this
}
}
impl ByteRepr for Node {}
impl ByteRepr for Node {
fn byte_size() -> usize {
24
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.val.borrow()).to_bytes(&mut buf[0..4]);
(*self.next.borrow()).to_bytes(&mut buf[8..16]);
(*self.prev.borrow()).to_bytes(&mut buf[16..24]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
val: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
next: Rc::new(RefCell::new(<Ptr<Node>>::from_bytes(&buf[8..16]))),
prev: Rc::new(RefCell::new(<Ptr<Node>>::from_bytes(&buf[16..24]))),
}
}
}
pub fn Find_0(head: Ptr<Node>, idx: i32) -> Ptr<Node> {
let head: Value<Ptr<Node>> = Rc::new(RefCell::new(head));
let idx: Value<i32> = Rc::new(RefCell::new(idx));
Expand Down
18 changes: 17 additions & 1 deletion tests/unit/out/refcount/enum_int_interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,23 @@ impl Clone for Entry {
this
}
}
impl ByteRepr for Entry {}
impl ByteRepr for Entry {
fn byte_size() -> usize {
16
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.name.borrow()).to_bytes(&mut buf[0..8]);
(*self.color.borrow()).to_bytes(&mut buf[8..12]);
(*self.opt.borrow()).to_bytes(&mut buf[12..16]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
name: Rc::new(RefCell::new(<Ptr<u8>>::from_bytes(&buf[0..8]))),
color: Rc::new(RefCell::new(<Color>::from_bytes(&buf[8..12]))),
opt: Rc::new(RefCell::new(<Option>::from_bytes(&buf[12..16]))),
}
}
}
thread_local!(
pub static global_color_0: Value<Color> = Rc::new(RefCell::new(Color::GREEN));
);
Expand Down
18 changes: 17 additions & 1 deletion tests/unit/out/refcount/enum_int_interop_c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,23 @@ pub struct Entry {
pub color: Value<Color>,
pub opt: Value<Option>,
}
impl ByteRepr for Entry {}
impl ByteRepr for Entry {
fn byte_size() -> usize {
16
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.name.borrow()).to_bytes(&mut buf[0..8]);
(*self.color.borrow()).to_bytes(&mut buf[8..12]);
(*self.opt.borrow()).to_bytes(&mut buf[12..16]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
name: Rc::new(RefCell::new(<Ptr<u8>>::from_bytes(&buf[0..8]))),
color: Rc::new(RefCell::new(<Color>::from_bytes(&buf[8..12]))),
opt: Rc::new(RefCell::new(<Option>::from_bytes(&buf[12..16]))),
}
}
}
thread_local!(
pub static global_color_0: Value<Color> = Rc::new(RefCell::new(Color::GREEN));
);
Expand Down
Loading
Loading