diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index c2bd9e18..cfc92689 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -3,6 +3,7 @@ #include "converter/models/converter_refcount.h" +#include #include #include @@ -497,10 +498,70 @@ void ConverterRefCount::AddDropTrait(const clang::CXXRecordDecl *decl) { StrCat("}"); } +static bool recordImplementsByteRepr(const clang::RecordDecl *decl) { + if (decl->isUnion()) { + return false; + } + + // ByteRepr is only supported for user-defined structs that contain ByteRepr + // fields. + for (auto *f : decl->fields()) { + auto qt = f->getType(); + if (qt->isEnumeralType()) { + return false; + } + if (!qt->isIntegerType() && !qt->isFloatingType()) { + return false; + } + } + + return true; +} + void ConverterRefCount::AddByteReprTrait(const clang::RecordDecl *decl) { auto struct_name = GetRecordName(decl); + + if (!recordImplementsByteRepr(decl)) { + StrCat(std::format("impl ByteRepr for {}", struct_name)); + PushBrace brace(*this); + return; + } + StrCat(std::format("impl ByteRepr for {}", struct_name)); - PushBrace brace(*this); + PushBrace impl_brace(*this); + + const auto &layout = ctx_.getASTRecordLayout(decl); + + StrCat("fn to_bytes(&self, buf: &mut [u8])"); + { + PushBrace fn_brace(*this); + unsigned idx = 0; + for (auto *field : decl->fields()) { + auto byte_off = layout.getFieldOffset(idx) / 8; + auto byte_size = ctx_.getTypeSize(field->getType()) / 8; + StrCat(std::format("(*self.{}.borrow()).to_bytes(&mut buf[{}..{}]);", + GetNamedDeclAsString(field), byte_off, + byte_off + byte_size)); + ++idx; + } + } + + StrCat("fn from_bytes(buf: &[u8]) -> Self"); + { + PushBrace fn_brace(*this); + StrCat("Self"); + PushBrace lit_brace(*this); + unsigned idx = 0; + for (auto *field : decl->fields()) { + auto byte_off = layout.getFieldOffset(idx) / 8; + auto byte_size = ctx_.getTypeSize(field->getType()) / 8; + StrCat(std::format( + "{}: Rc::new(RefCell::new(<{}>::from_bytes(&buf[{}..{}]))),", + GetNamedDeclAsString(field), Mapper::Map(field->getType()), byte_off, + byte_off + byte_size)); + ++idx; + } + } } std::string diff --git a/tests/unit/memcpy_struct_struct.cpp b/tests/unit/memcpy_struct_struct.cpp new file mode 100644 index 00000000..64059625 --- /dev/null +++ b/tests/unit/memcpy_struct_struct.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +#include + +struct Entry { + uint8_t bits; + uint16_t value; +}; + +int main(void) { + struct Entry table[8] = { + {1, 0x1111}, {2, 0x2222}, {3, 0x3333}, {4, 0x4444}, + {0, 0}, {0, 0}, {0, 0}, {0, 0}, + }; + size_t table_size = 4; + + memcpy(&table[table_size], &table[0], table_size * sizeof(table[0])); + + assert(table[4].bits == 1 && table[4].value == 0x1111); + assert(table[5].bits == 2 && table[5].value == 0x2222); + assert(table[6].bits == 3 && table[6].value == 0x3333); + assert(table[7].bits == 4 && table[7].value == 0x4444); + + return 0; +} diff --git a/tests/unit/out/refcount/addr_of_global.rs b/tests/unit/out/refcount/addr_of_global.rs index 90362073..943025df 100644 --- a/tests/unit/out/refcount/addr_of_global.rs +++ b/tests/unit/out/refcount/addr_of_global.rs @@ -18,7 +18,16 @@ impl Clone for Inner { this } } -impl ByteRepr for Inner {} +impl ByteRepr for Inner { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.value.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + value: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} #[derive(Default)] pub struct Outer { pub p: Value>, diff --git a/tests/unit/out/refcount/anonymous-struct.rs b/tests/unit/out/refcount/anonymous-struct.rs index 980aff17..60f64c58 100644 --- a/tests/unit/out/refcount/anonymous-struct.rs +++ b/tests/unit/out/refcount/anonymous-struct.rs @@ -20,7 +20,18 @@ impl Clone for Outer_Named { this } } -impl ByteRepr for Outer_Named {} +impl ByteRepr for Outer_Named { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.a.borrow()).to_bytes(&mut buf[0..4]); + (*self.b.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + b: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} #[derive(Default)] pub struct Outer_anon_0 { pub c: Value, @@ -35,7 +46,18 @@ impl Clone for Outer_anon_0 { this } } -impl ByteRepr for Outer_anon_0 {} +impl ByteRepr for Outer_anon_0 { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.c.borrow()).to_bytes(&mut buf[0..4]); + (*self.d.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + c: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + d: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} #[derive(Default)] pub struct Outer_anon_1 { pub g: Value, @@ -50,7 +72,18 @@ impl Clone for Outer_anon_1 { this } } -impl ByteRepr for Outer_anon_1 {} +impl ByteRepr for Outer_anon_1 { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.g.borrow()).to_bytes(&mut buf[0..4]); + (*self.h.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + g: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + h: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} #[derive(Default)] pub struct Outer_anon_2 { pub e: Value, @@ -65,7 +98,18 @@ impl Clone for Outer_anon_2 { this } } -impl ByteRepr for Outer_anon_2 {} +impl ByteRepr for Outer_anon_2 { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.e.borrow()).to_bytes(&mut buf[0..4]); + (*self.f.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + e: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + f: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} #[derive(Default)] pub struct Outer_anon_3_anon_0 { pub j: Value, @@ -78,7 +122,16 @@ impl Clone for Outer_anon_3_anon_0 { this } } -impl ByteRepr for Outer_anon_3_anon_0 {} +impl ByteRepr for Outer_anon_3_anon_0 { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.j.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + j: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} #[derive(Default)] pub struct Outer_anon_3_anon_1 { pub k: Value, @@ -91,7 +144,16 @@ impl Clone for Outer_anon_3_anon_1 { this } } -impl ByteRepr for Outer_anon_3_anon_1 {} +impl ByteRepr for Outer_anon_3_anon_1 { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.k.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + k: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} #[derive(Default)] pub struct Outer_anon_3 { pub i: Value, @@ -211,7 +273,18 @@ fn main_0() -> i32 { this } } - impl ByteRepr for anon_0 {}; + impl ByteRepr for anon_0 { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + (*self.z.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + z: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } + }; let s: Value = Rc::new(RefCell::new(::default())); (*(*s.borrow()).x.borrow_mut()) = 1; (*(*s.borrow()).z.borrow_mut()) = 2; diff --git a/tests/unit/out/refcount/anonymous-struct_c.rs b/tests/unit/out/refcount/anonymous-struct_c.rs index fe9e84ec..d682a2d6 100644 --- a/tests/unit/out/refcount/anonymous-struct_c.rs +++ b/tests/unit/out/refcount/anonymous-struct_c.rs @@ -11,35 +11,97 @@ pub struct Named { pub a: Value, pub b: Value, } -impl ByteRepr for Named {} +impl ByteRepr for Named { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.a.borrow()).to_bytes(&mut buf[0..4]); + (*self.b.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + b: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} #[derive(Default)] pub struct Outer_anon_0 { pub c: Value, pub d: Value, } -impl ByteRepr for Outer_anon_0 {} +impl ByteRepr for Outer_anon_0 { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.c.borrow()).to_bytes(&mut buf[0..4]); + (*self.d.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + c: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + d: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} #[derive(Default)] pub struct Outer_anon_1 { pub g: Value, pub h: Value, } -impl ByteRepr for Outer_anon_1 {} +impl ByteRepr for Outer_anon_1 { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.g.borrow()).to_bytes(&mut buf[0..4]); + (*self.h.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + g: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + h: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} #[derive(Default)] pub struct Outer_anon_2 { pub e: Value, pub f: Value, } -impl ByteRepr for Outer_anon_2 {} +impl ByteRepr for Outer_anon_2 { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.e.borrow()).to_bytes(&mut buf[0..4]); + (*self.f.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + e: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + f: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} #[derive(Default)] pub struct Outer_anon_3_anon_0 { pub j: Value, } -impl ByteRepr for Outer_anon_3_anon_0 {} +impl ByteRepr for Outer_anon_3_anon_0 { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.j.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + j: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} #[derive(Default)] pub struct Outer_anon_3_anon_1 { pub k: Value, } -impl ByteRepr for Outer_anon_3_anon_1 {} +impl ByteRepr for Outer_anon_3_anon_1 { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.k.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + k: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} #[derive(Default)] pub struct Outer_anon_3 { pub i: Value, @@ -113,7 +175,18 @@ fn main_0() -> i32 { pub x: Value, pub z: Value, } - impl ByteRepr for anon_0 {}; + impl ByteRepr for anon_0 { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + (*self.z.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + z: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } + }; let s: Value = >::default(); (*(*s.borrow()).x.borrow_mut()) = 1; (*(*s.borrow()).z.borrow_mut()) = 2; diff --git a/tests/unit/out/refcount/anonymous_enum.rs b/tests/unit/out/refcount/anonymous_enum.rs index fd913572..405402d6 100644 --- a/tests/unit/out/refcount/anonymous_enum.rs +++ b/tests/unit/out/refcount/anonymous_enum.rs @@ -48,7 +48,16 @@ impl Clone for S { this } } -impl ByteRepr for S {} +impl ByteRepr for S { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.a.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} #[derive(Clone, Copy, PartialEq, Debug, Default)] enum TdEnum { #[default] diff --git a/tests/unit/out/refcount/anonymous_enum_c.rs b/tests/unit/out/refcount/anonymous_enum_c.rs index 569a5f24..e28c7981 100644 --- a/tests/unit/out/refcount/anonymous_enum_c.rs +++ b/tests/unit/out/refcount/anonymous_enum_c.rs @@ -40,7 +40,16 @@ impl From for anon_enum_11 { pub struct S { pub a: Value, } -impl ByteRepr for S {} +impl ByteRepr for S { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.a.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} #[derive(Clone, Copy, PartialEq, Debug, Default)] enum TdEnum { #[default] diff --git a/tests/unit/out/refcount/bounded_struct_ptr.rs b/tests/unit/out/refcount/bounded_struct_ptr.rs index ec845b3b..477a71a6 100644 --- a/tests/unit/out/refcount/bounded_struct_ptr.rs +++ b/tests/unit/out/refcount/bounded_struct_ptr.rs @@ -20,7 +20,18 @@ impl Clone for Foo { this } } -impl ByteRepr for Foo {} +impl ByteRepr for Foo { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x1.borrow()).to_bytes(&mut buf[0..4]); + (*self.x2.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x1: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x2: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} pub fn main() { std::process::exit(main_0()); } diff --git a/tests/unit/out/refcount/c_struct.rs b/tests/unit/out/refcount/c_struct.rs index 37f19e18..3379e833 100644 --- a/tests/unit/out/refcount/c_struct.rs +++ b/tests/unit/out/refcount/c_struct.rs @@ -11,7 +11,18 @@ pub struct Point { pub x: Value, pub y: Value, } -impl ByteRepr for Point {} +impl ByteRepr for Point { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + (*self.y.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} #[derive(Default)] pub struct Line { pub start: Value, @@ -46,7 +57,18 @@ pub struct Inner { pub a: Value, pub b: Value, } -impl ByteRepr for Inner {} +impl ByteRepr for Inner { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.a.borrow()).to_bytes(&mut buf[0..4]); + (*self.b.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + b: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} #[derive(Default)] pub struct Container { pub inner: Value, diff --git a/tests/unit/out/refcount/class.rs b/tests/unit/out/refcount/class.rs index 97fea69c..82c14f3f 100644 --- a/tests/unit/out/refcount/class.rs +++ b/tests/unit/out/refcount/class.rs @@ -55,7 +55,18 @@ impl Clone for Pair { this } } -impl ByteRepr for Pair {} +impl ByteRepr for Pair { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.first.borrow()).to_bytes(&mut buf[0..4]); + (*self.second.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + first: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + second: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} #[derive(Default)] pub struct Route { pub path: Value, diff --git a/tests/unit/out/refcount/clone_vs_move.rs b/tests/unit/out/refcount/clone_vs_move.rs index bad09166..a6afa6ab 100644 --- a/tests/unit/out/refcount/clone_vs_move.rs +++ b/tests/unit/out/refcount/clone_vs_move.rs @@ -18,7 +18,16 @@ impl Clone for Bar { this } } -impl ByteRepr for Bar {} +impl ByteRepr for Bar { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.w.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + w: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} #[derive()] pub struct Foo { pub x: Value, diff --git a/tests/unit/out/refcount/complex_function.rs b/tests/unit/out/refcount/complex_function.rs index a10997f4..298858a4 100644 --- a/tests/unit/out/refcount/complex_function.rs +++ b/tests/unit/out/refcount/complex_function.rs @@ -29,7 +29,16 @@ impl Clone for X1 { this } } -impl ByteRepr for X1 {} +impl ByteRepr for X1 { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.v.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} #[derive(Default)] pub struct X2 { pub v: Ptr, diff --git a/tests/unit/out/refcount/exprs.rs b/tests/unit/out/refcount/exprs.rs index 825e4a4d..6a4a1646 100644 --- a/tests/unit/out/refcount/exprs.rs +++ b/tests/unit/out/refcount/exprs.rs @@ -18,7 +18,16 @@ impl Clone for X { this } } -impl ByteRepr for X {} +impl ByteRepr for X { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} #[derive(Default)] pub struct Y { pub x: Value, diff --git a/tests/unit/out/refcount/fft.rs b/tests/unit/out/refcount/fft.rs index 6d93ea2e..2ec8b4e9 100644 --- a/tests/unit/out/refcount/fft.rs +++ b/tests/unit/out/refcount/fft.rs @@ -20,7 +20,18 @@ impl Clone for Complex { this } } -impl ByteRepr for Complex {} +impl ByteRepr for Complex { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.re.borrow()).to_bytes(&mut buf[0..8]); + (*self.img.borrow()).to_bytes(&mut buf[8..16]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + re: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), + img: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + } + } +} pub fn Product_0(z1: Complex, z2: Complex) -> Complex { let z1: Value = Rc::new(RefCell::new(z1)); let z2: Value = Rc::new(RefCell::new(z2)); diff --git a/tests/unit/out/refcount/fn_ptr_stable_sort.rs b/tests/unit/out/refcount/fn_ptr_stable_sort.rs index 2d320e43..a6ac4c3a 100644 --- a/tests/unit/out/refcount/fn_ptr_stable_sort.rs +++ b/tests/unit/out/refcount/fn_ptr_stable_sort.rs @@ -20,7 +20,18 @@ impl Clone for Item { this } } -impl ByteRepr for Item {} +impl ByteRepr for Item { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.key.borrow()).to_bytes(&mut buf[0..4]); + (*self.value.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + key: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + value: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} pub fn Compare_0(a: Ptr, b: Ptr) -> bool { return { let _lhs = (*(*a.upgrade().deref()).key.borrow()); diff --git a/tests/unit/out/refcount/function_overloading.rs b/tests/unit/out/refcount/function_overloading.rs index a5aeccdf..00aa3599 100644 --- a/tests/unit/out/refcount/function_overloading.rs +++ b/tests/unit/out/refcount/function_overloading.rs @@ -62,7 +62,12 @@ impl Clone for Foo { this } } -impl ByteRepr for Foo {} +impl ByteRepr for Foo { + fn to_bytes(&self, buf: &mut [u8]) {} + fn from_bytes(buf: &[u8]) -> Self { + Self {} + } +} pub fn func_5(x: i32) -> i32 { let x: Value = Rc::new(RefCell::new(x)); return 1; diff --git a/tests/unit/out/refcount/global_without_initializer.rs b/tests/unit/out/refcount/global_without_initializer.rs index 08d8aa2b..b5c98330 100644 --- a/tests/unit/out/refcount/global_without_initializer.rs +++ b/tests/unit/out/refcount/global_without_initializer.rs @@ -18,7 +18,16 @@ impl Clone for S { this } } -impl ByteRepr for S {} +impl ByteRepr for S { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.a.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + a: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} thread_local!( pub static s: Value> = Rc::new(RefCell::new(Ptr::::null())); ); diff --git a/tests/unit/out/refcount/immutable-deref-on-func-call.rs b/tests/unit/out/refcount/immutable-deref-on-func-call.rs index 6533fef9..c282cc58 100644 --- a/tests/unit/out/refcount/immutable-deref-on-func-call.rs +++ b/tests/unit/out/refcount/immutable-deref-on-func-call.rs @@ -24,7 +24,16 @@ impl Clone for Item { this } } -impl ByteRepr for Item {} +impl ByteRepr for Item { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.value.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + value: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} pub fn main() { std::process::exit(main_0()); } diff --git a/tests/unit/out/refcount/init.rs b/tests/unit/out/refcount/init.rs index febbc569..f74601da 100644 --- a/tests/unit/out/refcount/init.rs +++ b/tests/unit/out/refcount/init.rs @@ -18,7 +18,16 @@ impl Clone for X { this } } -impl ByteRepr for X {} +impl ByteRepr for X { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} pub fn func_0() -> i32 { return 42; } diff --git a/tests/unit/out/refcount/kruskal.rs b/tests/unit/out/refcount/kruskal.rs index f33993ef..39b39ca4 100644 --- a/tests/unit/out/refcount/kruskal.rs +++ b/tests/unit/out/refcount/kruskal.rs @@ -22,7 +22,20 @@ impl Clone for Edge { this } } -impl ByteRepr for Edge {} +impl ByteRepr for Edge { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.u.borrow()).to_bytes(&mut buf[0..4]); + (*self.v.borrow()).to_bytes(&mut buf[4..8]); + (*self.weight.borrow()).to_bytes(&mut buf[8..16]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + u: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + weight: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + } + } +} pub fn partition_0(arr: Ptr>>>, start: i32, end: i32) -> i32 { let start: Value = Rc::new(RefCell::new(start)); let end: Value = Rc::new(RefCell::new(end)); diff --git a/tests/unit/out/refcount/memcpy_struct_struct.rs b/tests/unit/out/refcount/memcpy_struct_struct.rs new file mode 100644 index 00000000..0f7b4588 --- /dev/null +++ b/tests/unit/out/refcount/memcpy_struct_struct.rs @@ -0,0 +1,104 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +#[derive(Default)] +pub struct Entry { + pub bits: Value, + pub value: Value, +} +impl Clone for Entry { + fn clone(&self) -> Self { + let mut this = Self { + bits: Rc::new(RefCell::new((*self.bits.borrow()))), + value: Rc::new(RefCell::new((*self.value.borrow()))), + }; + this + } +} +impl ByteRepr for Entry { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.bits.borrow()).to_bytes(&mut buf[0..1]); + (*self.value.borrow()).to_bytes(&mut buf[2..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + bits: Rc::new(RefCell::new(::from_bytes(&buf[0..1]))), + value: Rc::new(RefCell::new(::from_bytes(&buf[2..4]))), + } + } +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let table: Value> = Rc::new(RefCell::new(Box::new([ + Entry { + bits: Rc::new(RefCell::new(1_u8)), + value: Rc::new(RefCell::new(4369_u16)), + }, + Entry { + bits: Rc::new(RefCell::new(2_u8)), + value: Rc::new(RefCell::new(8738_u16)), + }, + Entry { + bits: Rc::new(RefCell::new(3_u8)), + value: Rc::new(RefCell::new(13107_u16)), + }, + Entry { + bits: Rc::new(RefCell::new(4_u8)), + value: Rc::new(RefCell::new(17476_u16)), + }, + Entry { + bits: Rc::new(RefCell::new(0_u8)), + value: Rc::new(RefCell::new(0_u16)), + }, + Entry { + bits: Rc::new(RefCell::new(0_u8)), + value: Rc::new(RefCell::new(0_u16)), + }, + Entry { + bits: Rc::new(RefCell::new(0_u8)), + value: Rc::new(RefCell::new(0_u16)), + }, + Entry { + bits: Rc::new(RefCell::new(0_u8)), + value: Rc::new(RefCell::new(0_u16)), + }, + ]))); + let table_size: Value = Rc::new(RefCell::new(4_u64)); + { + (((table.as_pointer() as Ptr).offset((*table_size.borrow()) as isize)) + as Ptr) + .to_any() + .memcpy( + &(((table.as_pointer() as Ptr).offset(0 as isize)) as Ptr).to_any(), + (*table_size.borrow()).wrapping_mul(::std::mem::size_of::() as u64 as u64) + as usize, + ); + (((table.as_pointer() as Ptr).offset((*table_size.borrow()) as isize)) as Ptr) + .to_any() + .clone() + }; + assert!( + (((*(*table.borrow())[(4) as usize].bits.borrow()) as i32) == 1) + && (((*(*table.borrow())[(4) as usize].value.borrow()) as i32) == 4369) + ); + assert!( + (((*(*table.borrow())[(5) as usize].bits.borrow()) as i32) == 2) + && (((*(*table.borrow())[(5) as usize].value.borrow()) as i32) == 8738) + ); + assert!( + (((*(*table.borrow())[(6) as usize].bits.borrow()) as i32) == 3) + && (((*(*table.borrow())[(6) as usize].value.borrow()) as i32) == 13107) + ); + assert!( + (((*(*table.borrow())[(7) as usize].bits.borrow()) as i32) == 4) + && (((*(*table.borrow())[(7) as usize].value.borrow()) as i32) == 17476) + ); + return 0; +} diff --git a/tests/unit/out/refcount/nested_structs.rs b/tests/unit/out/refcount/nested_structs.rs index 6141301f..491cdf12 100644 --- a/tests/unit/out/refcount/nested_structs.rs +++ b/tests/unit/out/refcount/nested_structs.rs @@ -18,7 +18,16 @@ impl Clone for Level0_Level1_1_Level2_1_Level3_1 { this } } -impl ByteRepr for Level0_Level1_1_Level2_1_Level3_1 {} +impl ByteRepr for Level0_Level1_1_Level2_1_Level3_1 { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x1.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x1: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} #[derive(Default)] pub struct Level0_Level1_1_Level2_1_Level3_2 { pub x1: Value, @@ -33,7 +42,18 @@ impl Clone for Level0_Level1_1_Level2_1_Level3_2 { this } } -impl ByteRepr for Level0_Level1_1_Level2_1_Level3_2 {} +impl ByteRepr for Level0_Level1_1_Level2_1_Level3_2 { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x1.borrow()).to_bytes(&mut buf[0..4]); + (*self.x2.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x1: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x2: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} #[derive(Default)] pub struct Level0_Level1_1_Level2_1 { pub x1: Value, @@ -46,7 +66,16 @@ impl Clone for Level0_Level1_1_Level2_1 { this } } -impl ByteRepr for Level0_Level1_1_Level2_1 {} +impl ByteRepr for Level0_Level1_1_Level2_1 { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x1.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x1: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} #[derive(Default)] pub struct Level0_Level1_1 { pub x1: Value, @@ -59,7 +88,16 @@ impl Clone for Level0_Level1_1 { this } } -impl ByteRepr for Level0_Level1_1 {} +impl ByteRepr for Level0_Level1_1 { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x1.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x1: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} #[derive(Default)] pub struct Level0_Level1_2 { pub x1: Value, @@ -74,7 +112,18 @@ impl Clone for Level0_Level1_2 { this } } -impl ByteRepr for Level0_Level1_2 {} +impl ByteRepr for Level0_Level1_2 { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x1.borrow()).to_bytes(&mut buf[0..4]); + (*self.x2.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x1: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x2: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} #[derive(Default)] pub struct Level0 {} impl Clone for Level0 { @@ -83,7 +132,12 @@ impl Clone for Level0 { this } } -impl ByteRepr for Level0 {} +impl ByteRepr for Level0 { + fn to_bytes(&self, buf: &mut [u8]) {} + fn from_bytes(buf: &[u8]) -> Self { + Self {} + } +} pub fn main() { std::process::exit(main_0()); } diff --git a/tests/unit/out/refcount/new_struct.rs b/tests/unit/out/refcount/new_struct.rs index 93ba43c3..9885653c 100644 --- a/tests/unit/out/refcount/new_struct.rs +++ b/tests/unit/out/refcount/new_struct.rs @@ -20,7 +20,18 @@ impl Clone for Pair { this } } -impl ByteRepr for Pair {} +impl ByteRepr for Pair { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + (*self.y.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} pub fn main() { std::process::exit(main_0()); } diff --git a/tests/unit/out/refcount/operator_less_than.rs b/tests/unit/out/refcount/operator_less_than.rs index 8b1101bb..cbf3d70b 100644 --- a/tests/unit/out/refcount/operator_less_than.rs +++ b/tests/unit/out/refcount/operator_less_than.rs @@ -61,7 +61,18 @@ impl Clone for Pair { this } } -impl ByteRepr for Pair {} +impl ByteRepr for Pair { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + (*self.y.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} pub fn main() { std::process::exit(main_0()); } diff --git a/tests/unit/out/refcount/pod.rs b/tests/unit/out/refcount/pod.rs index 39c2ea91..1f095551 100644 --- a/tests/unit/out/refcount/pod.rs +++ b/tests/unit/out/refcount/pod.rs @@ -22,7 +22,20 @@ impl Clone for POD { this } } -impl ByteRepr for POD {} +impl ByteRepr for POD { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x1.borrow()).to_bytes(&mut buf[0..4]); + (*self.x2.borrow()).to_bytes(&mut buf[4..8]); + (*self.x3.borrow()).to_bytes(&mut buf[8..12]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x1: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x2: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + x3: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + } + } +} pub fn PODIncrement_0(pod: Ptr) { (*(*pod.upgrade().deref()).x1.borrow_mut()) += 1; (*(*pod.upgrade().deref()).x2.borrow_mut()) += 2; diff --git a/tests/unit/out/refcount/pointers.rs b/tests/unit/out/refcount/pointers.rs index 8a9f472d..e3e89828 100644 --- a/tests/unit/out/refcount/pointers.rs +++ b/tests/unit/out/refcount/pointers.rs @@ -34,7 +34,16 @@ impl Clone for Test { this } } -impl ByteRepr for Test {} +impl ByteRepr for Test { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} pub fn Update_0(t: Ptr) -> Ptr { let t: Value> = Rc::new(RefCell::new(t)); let x: Value = Rc::new(RefCell::new(1)); diff --git a/tests/unit/out/refcount/polymorphism.rs b/tests/unit/out/refcount/polymorphism.rs index 8dba9768..b1f4297f 100644 --- a/tests/unit/out/refcount/polymorphism.rs +++ b/tests/unit/out/refcount/polymorphism.rs @@ -22,7 +22,12 @@ impl Clone for Dog { this } } -impl ByteRepr for Dog {} +impl ByteRepr for Dog { + fn to_bytes(&self, buf: &mut [u8]) {} + fn from_bytes(buf: &[u8]) -> Self { + Self {} + } +} #[derive(Default)] pub struct Cat {} impl Cat { @@ -41,7 +46,12 @@ impl Clone for Cat { this } } -impl ByteRepr for Cat {} +impl ByteRepr for Cat { + fn to_bytes(&self, buf: &mut [u8]) {} + fn from_bytes(buf: &[u8]) -> Self { + Self {} + } +} pub fn main() { std::process::exit(main_0()); } diff --git a/tests/unit/out/refcount/push_emplace_back.rs b/tests/unit/out/refcount/push_emplace_back.rs index d1af0aaf..f55751ff 100644 --- a/tests/unit/out/refcount/push_emplace_back.rs +++ b/tests/unit/out/refcount/push_emplace_back.rs @@ -18,7 +18,16 @@ impl Clone for Chunk { this } } -impl ByteRepr for Chunk {} +impl ByteRepr for Chunk { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.data.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + data: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} #[derive(Default)] pub struct Writer { pub output: Value>>, diff --git a/tests/unit/out/refcount/random.rs b/tests/unit/out/refcount/random.rs index 8278e1fa..3b1057f8 100644 --- a/tests/unit/out/refcount/random.rs +++ b/tests/unit/out/refcount/random.rs @@ -81,7 +81,12 @@ impl Clone for X1 { this } } -impl ByteRepr for X1 {} +impl ByteRepr for X1 { + fn to_bytes(&self, buf: &mut [u8]) {} + fn from_bytes(buf: &[u8]) -> Self { + Self {} + } +} pub fn foo_1(x1: i32, x2: Ptr, x3: Ptr, p2: Ptr, p3: Ptr) { let x1: Value = Rc::new(RefCell::new(x1)); let x3: Value> = Rc::new(RefCell::new(x3)); diff --git a/tests/unit/out/refcount/reinterpret_cast_struct.rs b/tests/unit/out/refcount/reinterpret_cast_struct.rs index e2cf6f40..9e2370a1 100644 --- a/tests/unit/out/refcount/reinterpret_cast_struct.rs +++ b/tests/unit/out/refcount/reinterpret_cast_struct.rs @@ -20,7 +20,18 @@ impl Clone for Point { this } } -impl ByteRepr for Point {} +impl ByteRepr for Point { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + (*self.y.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} pub fn main() { std::process::exit(main_0()); } diff --git a/tests/unit/out/refcount/reinterpret_cast_struct_to_struct.rs b/tests/unit/out/refcount/reinterpret_cast_struct_to_struct.rs index 12eec8eb..0f065102 100644 --- a/tests/unit/out/refcount/reinterpret_cast_struct_to_struct.rs +++ b/tests/unit/out/refcount/reinterpret_cast_struct_to_struct.rs @@ -20,7 +20,18 @@ impl Clone for Point { this } } -impl ByteRepr for Point {} +impl ByteRepr for Point { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + (*self.y.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} #[derive(Default)] pub struct Pair { pub first: Value, @@ -35,7 +46,18 @@ impl Clone for Pair { this } } -impl ByteRepr for Pair {} +impl ByteRepr for Pair { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.first.borrow()).to_bytes(&mut buf[0..4]); + (*self.second.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + first: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + second: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} pub fn main() { std::process::exit(main_0()); } diff --git a/tests/unit/out/refcount/reserved_keywords.rs b/tests/unit/out/refcount/reserved_keywords.rs index b1a95864..a8b2ea80 100644 --- a/tests/unit/out/refcount/reserved_keywords.rs +++ b/tests/unit/out/refcount/reserved_keywords.rs @@ -92,7 +92,90 @@ impl Clone for S { this } } -impl ByteRepr for S {} +impl ByteRepr for S { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.as_.borrow()).to_bytes(&mut buf[0..4]); + (*self.async_.borrow()).to_bytes(&mut buf[4..8]); + (*self.await_.borrow()).to_bytes(&mut buf[8..12]); + (*self.crate_.borrow()).to_bytes(&mut buf[12..16]); + (*self.dyn_.borrow()).to_bytes(&mut buf[16..20]); + (*self.fn_.borrow()).to_bytes(&mut buf[20..24]); + (*self.impl_.borrow()).to_bytes(&mut buf[24..28]); + (*self.in_.borrow()).to_bytes(&mut buf[28..32]); + (*self.let_.borrow()).to_bytes(&mut buf[32..36]); + (*self.loop_.borrow()).to_bytes(&mut buf[36..40]); + (*self.match_.borrow()).to_bytes(&mut buf[40..44]); + (*self.mod_.borrow()).to_bytes(&mut buf[44..48]); + (*self.move_.borrow()).to_bytes(&mut buf[48..52]); + (*self.mut_.borrow()).to_bytes(&mut buf[52..56]); + (*self.pub_.borrow()).to_bytes(&mut buf[56..60]); + (*self.ref_.borrow()).to_bytes(&mut buf[60..64]); + (*self.self_.borrow()).to_bytes(&mut buf[64..68]); + (*self.Self_.borrow()).to_bytes(&mut buf[68..72]); + (*self.super_.borrow()).to_bytes(&mut buf[72..76]); + (*self.trait_.borrow()).to_bytes(&mut buf[76..80]); + (*self.type_.borrow()).to_bytes(&mut buf[80..84]); + (*self.unsafe_.borrow()).to_bytes(&mut buf[84..88]); + (*self.use_.borrow()).to_bytes(&mut buf[88..92]); + (*self.where_.borrow()).to_bytes(&mut buf[92..96]); + (*self.abstract_.borrow()).to_bytes(&mut buf[96..100]); + (*self.become_.borrow()).to_bytes(&mut buf[100..104]); + (*self.box_.borrow()).to_bytes(&mut buf[104..108]); + (*self.final_.borrow()).to_bytes(&mut buf[108..112]); + (*self.gen_.borrow()).to_bytes(&mut buf[112..116]); + (*self.macro_.borrow()).to_bytes(&mut buf[116..120]); + (*self.override_.borrow()).to_bytes(&mut buf[120..124]); + (*self.priv_.borrow()).to_bytes(&mut buf[124..128]); + (*self.unsized_.borrow()).to_bytes(&mut buf[128..132]); + (*self.yield_.borrow()).to_bytes(&mut buf[132..136]); + (*self.macro_rules_.borrow()).to_bytes(&mut buf[136..140]); + (*self.raw_.borrow()).to_bytes(&mut buf[140..144]); + (*self.safe_.borrow()).to_bytes(&mut buf[144..148]); + (*self.vec_.borrow()).to_bytes(&mut buf[148..152]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + as_: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + async_: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + await_: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + crate_: Rc::new(RefCell::new(::from_bytes(&buf[12..16]))), + dyn_: Rc::new(RefCell::new(::from_bytes(&buf[16..20]))), + fn_: Rc::new(RefCell::new(::from_bytes(&buf[20..24]))), + impl_: Rc::new(RefCell::new(::from_bytes(&buf[24..28]))), + in_: Rc::new(RefCell::new(::from_bytes(&buf[28..32]))), + let_: Rc::new(RefCell::new(::from_bytes(&buf[32..36]))), + loop_: Rc::new(RefCell::new(::from_bytes(&buf[36..40]))), + match_: Rc::new(RefCell::new(::from_bytes(&buf[40..44]))), + mod_: Rc::new(RefCell::new(::from_bytes(&buf[44..48]))), + move_: Rc::new(RefCell::new(::from_bytes(&buf[48..52]))), + mut_: Rc::new(RefCell::new(::from_bytes(&buf[52..56]))), + pub_: Rc::new(RefCell::new(::from_bytes(&buf[56..60]))), + ref_: Rc::new(RefCell::new(::from_bytes(&buf[60..64]))), + self_: Rc::new(RefCell::new(::from_bytes(&buf[64..68]))), + Self_: Rc::new(RefCell::new(::from_bytes(&buf[68..72]))), + super_: Rc::new(RefCell::new(::from_bytes(&buf[72..76]))), + trait_: Rc::new(RefCell::new(::from_bytes(&buf[76..80]))), + type_: Rc::new(RefCell::new(::from_bytes(&buf[80..84]))), + unsafe_: Rc::new(RefCell::new(::from_bytes(&buf[84..88]))), + use_: Rc::new(RefCell::new(::from_bytes(&buf[88..92]))), + where_: Rc::new(RefCell::new(::from_bytes(&buf[92..96]))), + abstract_: Rc::new(RefCell::new(::from_bytes(&buf[96..100]))), + become_: Rc::new(RefCell::new(::from_bytes(&buf[100..104]))), + box_: Rc::new(RefCell::new(::from_bytes(&buf[104..108]))), + final_: Rc::new(RefCell::new(::from_bytes(&buf[108..112]))), + gen_: Rc::new(RefCell::new(::from_bytes(&buf[112..116]))), + macro_: Rc::new(RefCell::new(::from_bytes(&buf[116..120]))), + override_: Rc::new(RefCell::new(::from_bytes(&buf[120..124]))), + priv_: Rc::new(RefCell::new(::from_bytes(&buf[124..128]))), + unsized_: Rc::new(RefCell::new(::from_bytes(&buf[128..132]))), + yield_: Rc::new(RefCell::new(::from_bytes(&buf[132..136]))), + macro_rules_: Rc::new(RefCell::new(::from_bytes(&buf[136..140]))), + raw_: Rc::new(RefCell::new(::from_bytes(&buf[140..144]))), + safe_: Rc::new(RefCell::new(::from_bytes(&buf[144..148]))), + vec_: Rc::new(RefCell::new(::from_bytes(&buf[148..152]))), + } + } +} pub fn foo_0( as_: i32, async_: i32, diff --git a/tests/unit/out/refcount/static_var_in_class.rs b/tests/unit/out/refcount/static_var_in_class.rs index 0acf1b02..2d0c197f 100644 --- a/tests/unit/out/refcount/static_var_in_class.rs +++ b/tests/unit/out/refcount/static_var_in_class.rs @@ -22,7 +22,12 @@ impl Clone for C { this } } -impl ByteRepr for C {} +impl ByteRepr for C { + fn to_bytes(&self, buf: &mut [u8]) {} + fn from_bytes(buf: &[u8]) -> Self { + Self {} + } +} thread_local!( pub static S_inner_const: Value = Rc::new(RefCell::new(2)); ); @@ -34,7 +39,12 @@ impl Clone for S { this } } -impl ByteRepr for S {} +impl ByteRepr for S { + fn to_bytes(&self, buf: &mut [u8]) {} + fn from_bytes(buf: &[u8]) -> Self { + Self {} + } +} pub fn main() { std::process::exit(main_0()); } diff --git a/tests/unit/out/refcount/struct_ctor.rs b/tests/unit/out/refcount/struct_ctor.rs index 2e575770..e0a9d434 100644 --- a/tests/unit/out/refcount/struct_ctor.rs +++ b/tests/unit/out/refcount/struct_ctor.rs @@ -39,7 +39,18 @@ impl Clone for StructWithCtor { this } } -impl ByteRepr for StructWithCtor {} +impl ByteRepr for StructWithCtor { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x1_.borrow()).to_bytes(&mut buf[0..4]); + (*self.x2_.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x1_: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + x2_: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} pub fn foo_0(x: Ptr) -> Ptr { return (x).clone(); } diff --git a/tests/unit/out/refcount/struct_ptr.rs b/tests/unit/out/refcount/struct_ptr.rs index 83eba0b7..4defc67a 100644 --- a/tests/unit/out/refcount/struct_ptr.rs +++ b/tests/unit/out/refcount/struct_ptr.rs @@ -18,7 +18,16 @@ impl Clone for XX { this } } -impl ByteRepr for XX {} +impl ByteRepr for XX { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} pub fn main() { std::process::exit(main_0()); } diff --git a/tests/unit/out/refcount/typedef-anon-struct.rs b/tests/unit/out/refcount/typedef-anon-struct.rs index c8faa988..9ad78e5d 100644 --- a/tests/unit/out/refcount/typedef-anon-struct.rs +++ b/tests/unit/out/refcount/typedef-anon-struct.rs @@ -20,7 +20,18 @@ impl Clone for Outer_RunInfo { this } } -impl ByteRepr for Outer_RunInfo {} +impl ByteRepr for Outer_RunInfo { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.block_idx.borrow()).to_bytes(&mut buf[0..4]); + (*self.num_extra_zero_runs.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + block_idx: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + num_extra_zero_runs: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} #[derive(Default)] pub struct Outer { pub runs: Value>, diff --git a/tests/unit/out/refcount/unique_ptr.rs b/tests/unit/out/refcount/unique_ptr.rs index b9228cf3..b51cdf4d 100644 --- a/tests/unit/out/refcount/unique_ptr.rs +++ b/tests/unit/out/refcount/unique_ptr.rs @@ -30,7 +30,18 @@ impl Clone for Pair { this } } -impl ByteRepr for Pair {} +impl ByteRepr for Pair { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + (*self.y.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} impl Pair { pub fn inc(&self, k: i32) { let k: Value = Rc::new(RefCell::new(k)); diff --git a/tests/unit/out/refcount/unique_ptr_nested.rs b/tests/unit/out/refcount/unique_ptr_nested.rs index 16a675c5..94cfb1d5 100644 --- a/tests/unit/out/refcount/unique_ptr_nested.rs +++ b/tests/unit/out/refcount/unique_ptr_nested.rs @@ -20,7 +20,18 @@ impl Clone for Inner { this } } -impl ByteRepr for Inner {} +impl ByteRepr for Inner { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + (*self.y.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} #[derive(Default)] pub struct Outer { pub inner: Value>>, diff --git a/tests/unit/out/refcount/unique_ptr_struct.rs b/tests/unit/out/refcount/unique_ptr_struct.rs index a409db4d..db2e7df6 100644 --- a/tests/unit/out/refcount/unique_ptr_struct.rs +++ b/tests/unit/out/refcount/unique_ptr_struct.rs @@ -20,7 +20,18 @@ impl Clone for Point { this } } -impl ByteRepr for Point {} +impl ByteRepr for Point { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + (*self.y.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} pub fn sum_0(p: Point) -> i32 { let p: Value = Rc::new(RefCell::new(p)); return ((*(*p.borrow()).x.borrow()) + (*(*p.borrow()).y.borrow())); diff --git a/tests/unit/out/refcount/va_arg_struct_ctx.rs b/tests/unit/out/refcount/va_arg_struct_ctx.rs index 8ffe4650..c74d7062 100644 --- a/tests/unit/out/refcount/va_arg_struct_ctx.rs +++ b/tests/unit/out/refcount/va_arg_struct_ctx.rs @@ -11,7 +11,18 @@ pub struct context { pub verbose: Value, pub last_error: Value, } -impl ByteRepr for context {} +impl ByteRepr for context { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.verbose.borrow()).to_bytes(&mut buf[0..4]); + (*self.last_error.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + verbose: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + last_error: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} pub fn set_error_0(ctx: Ptr, fmt: Ptr, __args: &[VaArg]) { let ctx: Value> = Rc::new(RefCell::new(ctx)); let fmt: Value> = Rc::new(RefCell::new(fmt)); diff --git a/tests/unit/out/refcount/void_cast.rs b/tests/unit/out/refcount/void_cast.rs index f8c55b9d..60dc763e 100644 --- a/tests/unit/out/refcount/void_cast.rs +++ b/tests/unit/out/refcount/void_cast.rs @@ -49,7 +49,16 @@ impl Clone for Holder { this } } -impl ByteRepr for Holder {} +impl ByteRepr for Holder { + fn to_bytes(&self, buf: &mut [u8]) { + (*self.field.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + field: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} pub fn main() { std::process::exit(main_0()); } diff --git a/tests/unit/out/unsafe/memcpy_struct_struct.rs b/tests/unit/out/unsafe/memcpy_struct_struct.rs new file mode 100644 index 00000000..01f42ce1 --- /dev/null +++ b/tests/unit/out/unsafe/memcpy_struct_struct.rs @@ -0,0 +1,84 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[repr(C)] +#[derive(Copy, Clone, Default)] +pub struct Entry { + pub bits: u8, + pub value: u16, +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut table: [Entry; 8] = [ + Entry { + bits: 1_u8, + value: 4369_u16, + }, + Entry { + bits: 2_u8, + value: 8738_u16, + }, + Entry { + bits: 3_u8, + value: 13107_u16, + }, + Entry { + bits: 4_u8, + value: 17476_u16, + }, + Entry { + bits: 0_u8, + value: 0_u16, + }, + Entry { + bits: 0_u8, + value: 0_u16, + }, + Entry { + bits: 0_u8, + value: 0_u16, + }, + Entry { + bits: 0_u8, + value: 0_u16, + }, + ]; + let mut table_size: u64 = 4_u64; + { + if (table_size).wrapping_mul(::std::mem::size_of::() as u64 as u64) != 0 { + ::std::ptr::copy_nonoverlapping( + ((&mut table[(0) as usize] as *mut Entry) as *const Entry as *const ::libc::c_void), + ((&mut table[(table_size) as usize] as *mut Entry) as *mut Entry + as *mut ::libc::c_void), + (table_size).wrapping_mul(::std::mem::size_of::() as u64 as u64) as usize, + ) + } + ((&mut table[(table_size) as usize] as *mut Entry) as *mut Entry as *mut ::libc::c_void) + }; + assert!( + ((table[(4) as usize].bits as i32) == (1)) + && ((table[(4) as usize].value as i32) == (4369)) + ); + assert!( + ((table[(5) as usize].bits as i32) == (2)) + && ((table[(5) as usize].value as i32) == (8738)) + ); + assert!( + ((table[(6) as usize].bits as i32) == (3)) + && ((table[(6) as usize].value as i32) == (13107)) + ); + assert!( + ((table[(7) as usize].bits as i32) == (4)) + && ((table[(7) as usize].value as i32) == (17476)) + ); + return 0; +} diff --git a/tests/unit/reinterpret_cast_struct.cpp b/tests/unit/reinterpret_cast_struct.cpp index 28318e57..d037ff28 100644 --- a/tests/unit/reinterpret_cast_struct.cpp +++ b/tests/unit/reinterpret_cast_struct.cpp @@ -1,7 +1,6 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -// panic: refcount #include #include