From 08f912792cbd69c2b7fe04f1d993516a8e77c373 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 30 Jun 2026 17:22:58 +0100 Subject: [PATCH] Add panic'ing ByteRepr impl for Ptr and AnyPtr --- cpp2rust/converter/converter_lib.cpp | 3 + libcc2rs/src/rc.rs | 1 + .../out/refcount/opaque_forward_decl.rs | 16 +- tests/unit/out/refcount/10_struct.rs | 32 +++- tests/unit/out/refcount/addr_of_global.rs | 14 +- tests/unit/out/refcount/bst.rs | 18 +- tests/unit/out/refcount/c_struct.rs | 16 +- tests/unit/out/refcount/complex_function.rs | 28 ++- tests/unit/out/refcount/default.rs | 22 ++- tests/unit/out/refcount/default_in_statics.rs | 72 +++++++- tests/unit/out/refcount/doubly_linked_list.rs | 18 +- tests/unit/out/refcount/enum_int_interop.rs | 18 +- tests/unit/out/refcount/enum_int_interop_c.rs | 18 +- tests/unit/out/refcount/exprs.rs | 16 +- tests/unit/out/refcount/fn_ptr_cast.rs | 14 +- tests/unit/out/refcount/fn_ptr_struct.rs | 18 +- tests/unit/out/refcount/fn_ptr_vtable.rs | 22 ++- tests/unit/out/refcount/global_pointers.rs | 16 +- tests/unit/out/refcount/huffman.rs | 20 ++- tests/unit/out/refcount/linked_list.rs | 16 +- tests/unit/out/refcount/new_bst.rs | 18 +- .../unit/out/refcount/opaque_forward_decl.rs | 16 +- .../unit/out/refcount/opaque_then_defined.rs | 32 +++- tests/unit/out/refcount/pointer_array.rs | 14 +- tests/unit/out/refcount/push_emplace_back.rs | 16 +- .../out/refcount/union_tagged_many_arms.rs | 156 +++++++++++++++++ .../unit/out/refcount/union_tagged_simple.rs | 123 +++++++++++++ .../out/refcount/union_tagged_struct_arms.rs | 18 +- .../refcount/union_void_ptr_sized_deref.rs | 163 ++++++++++++++++++ .../out/refcount/va_arg_non_primitive_ptrs.rs | 16 +- tests/unit/union_tagged_many_arms.c | 2 +- tests/unit/union_tagged_simple.c | 2 +- tests/unit/union_void_ptr_sized_deref.c | 2 +- 33 files changed, 943 insertions(+), 33 deletions(-) create mode 100644 tests/unit/out/refcount/union_tagged_many_arms.rs create mode 100644 tests/unit/out/refcount/union_tagged_simple.rs create mode 100644 tests/unit/out/refcount/union_void_ptr_sized_deref.rs diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 2eafb1c1..1b837cd7 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -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()); } diff --git a/libcc2rs/src/rc.rs b/libcc2rs/src/rc.rs index ac09c80c..4def6ec2 100644 --- a/libcc2rs/src/rc.rs +++ b/libcc2rs/src/rc.rs @@ -1256,6 +1256,7 @@ impl AsPointerDyn for Rc> { } impl ByteRepr for Ptr {} +impl ByteRepr for AnyPtr {} #[cfg(test)] mod tests { diff --git a/tests/multi-file/opaque_forward_decl/out/refcount/opaque_forward_decl.rs b/tests/multi-file/opaque_forward_decl/out/refcount/opaque_forward_decl.rs index 064f1131..77d2b14b 100644 --- a/tests/multi-file/opaque_forward_decl/out/refcount/opaque_forward_decl.rs +++ b/tests/multi-file/opaque_forward_decl/out/refcount/opaque_forward_decl.rs @@ -11,7 +11,21 @@ pub struct container { pub p: Value>, pub x: Value, } -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(>::from_bytes(&buf[0..8]))), + x: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + } + } +} pub fn main() { std::process::exit(main_0()); } diff --git a/tests/unit/out/refcount/10_struct.rs b/tests/unit/out/refcount/10_struct.rs index 2bc298dc..52b05821 100644 --- a/tests/unit/out/refcount/10_struct.rs +++ b/tests/unit/out/refcount/10_struct.rs @@ -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(::from_bytes(&buf[0..4]))), + next: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + } + } +} #[derive(Default)] pub struct Graph { pub V: Value, @@ -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(::from_bytes(&buf[0..4]))), + adj: Rc::new(RefCell::new(>>::from_bytes(&buf[8..16]))), + } + } +} pub fn main() { std::process::exit(main_0()); } diff --git a/tests/unit/out/refcount/addr_of_global.rs b/tests/unit/out/refcount/addr_of_global.rs index 0d03ca3b..e9ee44e9 100644 --- a/tests/unit/out/refcount/addr_of_global.rs +++ b/tests/unit/out/refcount/addr_of_global.rs @@ -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(>::from_bytes(&buf[0..8]))), + } + } +} thread_local!( pub static alpha_0: Value = Rc::new(RefCell::new(Inner { value: Rc::new(RefCell::new(1)), diff --git a/tests/unit/out/refcount/bst.rs b/tests/unit/out/refcount/bst.rs index 30a78b50..cb241d9f 100644 --- a/tests/unit/out/refcount/bst.rs +++ b/tests/unit/out/refcount/bst.rs @@ -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(>::from_bytes(&buf[0..8]))), + right: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + value: Rc::new(RefCell::new(::from_bytes(&buf[16..20]))), + } + } +} pub fn find_0(node: Ptr, value: i32) -> Ptr { let node: Value> = Rc::new(RefCell::new(node)); let value: Value = Rc::new(RefCell::new(value)); diff --git a/tests/unit/out/refcount/c_struct.rs b/tests/unit/out/refcount/c_struct.rs index 1214d4a9..d6921e87 100644 --- a/tests/unit/out/refcount/c_struct.rs +++ b/tests/unit/out/refcount/c_struct.rs @@ -51,7 +51,21 @@ pub struct Node { pub value: Value, pub next: Value>, } -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(::from_bytes(&buf[0..4]))), + next: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + } + } +} #[derive(Clone, Copy, PartialEq, Debug, Default)] enum Color { #[default] diff --git a/tests/unit/out/refcount/complex_function.rs b/tests/unit/out/refcount/complex_function.rs index 737edc12..9d6ba64e 100644 --- a/tests/unit/out/refcount/complex_function.rs +++ b/tests/unit/out/refcount/complex_function.rs @@ -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(>::from_bytes(&buf[0..8]))), + } + } +} #[derive(Default)] pub struct X4 { pub v: Value, @@ -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(::from_bytes(&buf[0..8]))), + } + } +} pub fn main() { std::process::exit(main_0()); } diff --git a/tests/unit/out/refcount/default.rs b/tests/unit/out/refcount/default.rs index 3f2389c5..0a4447a7 100644 --- a/tests/unit/out/refcount/default.rs +++ b/tests/unit/out/refcount/default.rs @@ -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(>::from_bytes(&buf[0..8]))), + x2: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + x3: Rc::new(RefCell::new(]>>::from_bytes(&buf[16..56]))), + x4: Rc::new(RefCell::new(]>>::from_bytes(&buf[56..136]))), + x5: Rc::new(RefCell::new(::from_bytes(&buf[136..140]))), + } + } +} pub fn main() { std::process::exit(main_0()); } diff --git a/tests/unit/out/refcount/default_in_statics.rs b/tests/unit/out/refcount/default_in_statics.rs index 7936846d..7636013a 100644 --- a/tests/unit/out/refcount/default_in_statics.rs +++ b/tests/unit/out/refcount/default_in_statics.rs @@ -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(::from_bytes(&buf[0..4]))), + name: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + } + } +} #[derive()] pub struct Outer { pub p1: Value>, @@ -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(>::from_bytes(&buf[0..8]))), + p2: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + arr: Rc::new(RefCell::new(]>>::from_bytes(&buf[16..40]))), + cp: Rc::new(RefCell::new(>::from_bytes(&buf[40..48]))), + pp: Rc::new(RefCell::new(>>::from_bytes(&buf[48..56]))), + inner: Rc::new(RefCell::new(::from_bytes(&buf[56..72]))), + x: Rc::new(RefCell::new(::from_bytes(&buf[72..76]))), + fn_: Rc::new(RefCell::new( i32>>::from_bytes( + &buf[80..88], + ))), + } + } +} #[derive()] pub struct Foo { pub s1: Value>, @@ -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(>::from_bytes(&buf[0..8]))), + s2: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + fn1: Rc::new(RefCell::new( i32>>::from_bytes( + &buf[16..24], + ))), + fn2: Rc::new(RefCell::new( i32>>::from_bytes( + &buf[24..32], + ))), + n: Rc::new(RefCell::new(::from_bytes(&buf[32..36]))), + } + } +} thread_local!( pub static static_fn_0: Value i32>> = Rc::new(RefCell::new(FnPtr::null())); ); diff --git a/tests/unit/out/refcount/doubly_linked_list.rs b/tests/unit/out/refcount/doubly_linked_list.rs index c8a671d4..e584674d 100644 --- a/tests/unit/out/refcount/doubly_linked_list.rs +++ b/tests/unit/out/refcount/doubly_linked_list.rs @@ -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(::from_bytes(&buf[0..4]))), + next: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + prev: Rc::new(RefCell::new(>::from_bytes(&buf[16..24]))), + } + } +} pub fn Find_0(head: Ptr, idx: i32) -> Ptr { let head: Value> = Rc::new(RefCell::new(head)); let idx: Value = Rc::new(RefCell::new(idx)); diff --git a/tests/unit/out/refcount/enum_int_interop.rs b/tests/unit/out/refcount/enum_int_interop.rs index fea2807d..e8c088e5 100644 --- a/tests/unit/out/refcount/enum_int_interop.rs +++ b/tests/unit/out/refcount/enum_int_interop.rs @@ -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(>::from_bytes(&buf[0..8]))), + color: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + opt: Rc::new(RefCell::new(