From 1a3e3576a7d9c943b803491ce1abee0116f914b8 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 30 Sep 2026 12:36:52 +0100 Subject: [PATCH 1/7] Update tests --- tests/unit/array_field_addr.c | 24 ++ tests/unit/container_field_addr.cpp | 42 +++ tests/unit/field_aliasing.c | 44 ++++ tests/unit/field_method_receiver.cpp | 51 ++++ tests/unit/field_pointers_malloced_struct.cpp | 42 +++ tests/unit/first_field_ptr_covers_struct.c | 32 +++ tests/unit/first_member_downcast.c | 29 +++ tests/unit/flex_array_member_sibling_write.c | 46 ++++ tests/unit/memset_struct_tail.c | 32 +++ tests/unit/out/refcount/array_field_addr.rs | 92 +++++++ .../unit/out/refcount/container_field_addr.rs | 166 ++++++++++++ tests/unit/out/refcount/field_aliasing.rs | 185 +++++++++++++ .../out/refcount/field_method_receiver.rs | 205 +++++++++++++++ .../field_pointers_malloced_struct.rs | 111 ++++++++ .../refcount/first_field_ptr_covers_struct.rs | 158 ++++++++++++ .../out/refcount/first_member_downcast.rs | 107 ++++++++ .../flex_array_member_sibling_write.rs | 219 ++++++++++++++++ tests/unit/out/refcount/memset_struct_tail.rs | 127 +++++++++ .../out/refcount/struct_2d_array_field.rs | 242 ++++++++++++++++++ .../refcount/struct_member_array_adjacency.rs | 58 +++++ tests/unit/out/unsafe/array_field_addr.rs | 68 +++++ tests/unit/out/unsafe/container_field_addr.rs | 83 ++++++ tests/unit/out/unsafe/field_aliasing.rs | 77 ++++++ .../unit/out/unsafe/field_method_receiver.rs | 98 +++++++ .../unsafe/field_pointers_malloced_struct.rs | 61 +++++ .../unsafe/first_field_ptr_covers_struct.rs | 89 +++++++ .../unit/out/unsafe/first_member_downcast.rs | 44 ++++ .../unsafe/flex_array_member_sibling_write.rs | 109 ++++++++ tests/unit/out/unsafe/memset_struct_tail.rs | 90 +++++++ .../unit/out/unsafe/struct_2d_array_field.rs | 125 +++++++++ .../unsafe/struct_member_array_adjacency.rs | 34 +++ tests/unit/struct_2d_array_field.c | 33 +++ tests/unit/struct_member_array_adjacency.c | 13 + 33 files changed, 2936 insertions(+) create mode 100644 tests/unit/array_field_addr.c create mode 100644 tests/unit/container_field_addr.cpp create mode 100644 tests/unit/field_aliasing.c create mode 100644 tests/unit/field_method_receiver.cpp create mode 100644 tests/unit/field_pointers_malloced_struct.cpp create mode 100644 tests/unit/first_field_ptr_covers_struct.c create mode 100644 tests/unit/first_member_downcast.c create mode 100644 tests/unit/flex_array_member_sibling_write.c create mode 100644 tests/unit/memset_struct_tail.c create mode 100644 tests/unit/out/refcount/array_field_addr.rs create mode 100644 tests/unit/out/refcount/container_field_addr.rs create mode 100644 tests/unit/out/refcount/field_aliasing.rs create mode 100644 tests/unit/out/refcount/field_method_receiver.rs create mode 100644 tests/unit/out/refcount/field_pointers_malloced_struct.rs create mode 100644 tests/unit/out/refcount/first_field_ptr_covers_struct.rs create mode 100644 tests/unit/out/refcount/first_member_downcast.rs create mode 100644 tests/unit/out/refcount/flex_array_member_sibling_write.rs create mode 100644 tests/unit/out/refcount/memset_struct_tail.rs create mode 100644 tests/unit/out/refcount/struct_2d_array_field.rs create mode 100644 tests/unit/out/refcount/struct_member_array_adjacency.rs create mode 100644 tests/unit/out/unsafe/array_field_addr.rs create mode 100644 tests/unit/out/unsafe/container_field_addr.rs create mode 100644 tests/unit/out/unsafe/field_aliasing.rs create mode 100644 tests/unit/out/unsafe/field_method_receiver.rs create mode 100644 tests/unit/out/unsafe/field_pointers_malloced_struct.rs create mode 100644 tests/unit/out/unsafe/first_field_ptr_covers_struct.rs create mode 100644 tests/unit/out/unsafe/first_member_downcast.rs create mode 100644 tests/unit/out/unsafe/flex_array_member_sibling_write.rs create mode 100644 tests/unit/out/unsafe/memset_struct_tail.rs create mode 100644 tests/unit/out/unsafe/struct_2d_array_field.rs create mode 100644 tests/unit/out/unsafe/struct_member_array_adjacency.rs create mode 100644 tests/unit/struct_2d_array_field.c create mode 100644 tests/unit/struct_member_array_adjacency.c diff --git a/tests/unit/array_field_addr.c b/tests/unit/array_field_addr.c new file mode 100644 index 000000000..e02202c51 --- /dev/null +++ b/tests/unit/array_field_addr.c @@ -0,0 +1,24 @@ +#include +#include +#include + +struct S { + int before; + unsigned char mask[4]; + int after; +}; + +int main(void) { + struct S *s = malloc(sizeof(struct S)); + assert(s != NULL); + s->before = 1; + memset(s->mask, 5, sizeof(s->mask)); + s->after = 2; + *(unsigned char *)&s->mask = 7; + unsigned char out[4]; + memcpy(out, &s->mask, sizeof(s->mask)); + assert(out[0] == 7 && out[3] == 5); + assert(s->before == 1 && s->after == 2); + free(s); + return 0; +} diff --git a/tests/unit/container_field_addr.cpp b/tests/unit/container_field_addr.cpp new file mode 100644 index 000000000..a5d26fc13 --- /dev/null +++ b/tests/unit/container_field_addr.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include + +struct S { + int tag; + std::vector v; + std::string s; + std::map m; +}; + +static void add(std::vector *v, int k) { v->push_back(k); } + +static void append(std::string *s, const char *t, std::size_t n) { + s->append(t, n); +} + +static void put(std::map *m, int k, int v) { (*m)[k] = v; } + +static void run(S *h) { + add(&h->v, h->tag); + append(&h->s, "ab", 2); + put(&h->m, h->tag, 2); + std::vector *pv = &h->v; + pv->push_back((int)pv->size()); + assert(h->v.size() == 2 && h->v[0] == 7 && h->v[1] == 1); + assert(h->s == "ab"); + assert(h->m[7] == 2); + assert(h->tag == 7); +} + +int main() { + S local; + local.tag = 7; + run(&local); + S *heap = new S(); + heap->tag = 7; + run(heap); + delete heap; + return 0; +} diff --git a/tests/unit/field_aliasing.c b/tests/unit/field_aliasing.c new file mode 100644 index 000000000..d9cedb187 --- /dev/null +++ b/tests/unit/field_aliasing.c @@ -0,0 +1,44 @@ +#include +#include + +struct In { + int x; + int y; +}; + +struct S { + struct In in; + int total; + int n; + int arr[4]; +}; + +struct Node { + int x; + struct Node *self; +}; + +int main(void) { + struct S *p = calloc(1, sizeof(struct S)); + assert(p != NULL); + struct S *q = p; + p->in.x = 1; + p->in.y = 2; + p->total = q->in.x + q->in.y; + assert(q->total == 3); + struct In *ip = &p->in; + ip->x = p->total + 1; + assert(q->in.x == 4 && q->in.y == 2); + p->arr[p->n] = p->total; + p->n += 1; + p->arr[p->n] = q->in.x; + assert(q->arr[0] == 3 && q->arr[1] == 4 && q->n == 1); + free(p); + + struct Node s; + s.x = 1; + s.self = &s; + s.self->x = s.x + 1; + assert(s.x == 2); + return 0; +} diff --git a/tests/unit/field_method_receiver.cpp b/tests/unit/field_method_receiver.cpp new file mode 100644 index 000000000..895feb96c --- /dev/null +++ b/tests/unit/field_method_receiver.cpp @@ -0,0 +1,51 @@ +#include +#include + +struct Counter { + int n; + int get() const { return n; } + void add(int k) { n += k; } + Counter *self() { return this; } + void take(Counter *other) { + n += other->n; + other->n = 0; + } +}; + +struct S { + int tag; + Counter c; + Counter arr[2]; + std::vector v; + void bump() { c.add(tag); } +}; + +static void run(S *o) { + o->c.add(2); + assert(o->c.get() == 2); + o->arr[1].add(5); + assert(o->arr[1].get() == 5); + o->c.self()->add(1); + assert(o->c.get() == 3); + assert(o->c.self() == &o->c); + o->arr[0].take(&o->arr[1]); + assert(o->arr[0].get() == 5 && o->arr[1].get() == 0); + o->c.take(&o->c); + assert(o->c.get() == 0); + o->bump(); + assert(o->c.get() == 1); + o->v.push_back(o->c.get()); + assert(o->v.size() == 1 && o->v[0] == 1); + assert(o->tag == 1); +} + +int main() { + S local{}; + local.tag = 1; + run(&local); + S *heap = new S{}; + heap->tag = 1; + run(heap); + delete heap; + return 0; +} diff --git a/tests/unit/field_pointers_malloced_struct.cpp b/tests/unit/field_pointers_malloced_struct.cpp new file mode 100644 index 000000000..f042aa41a --- /dev/null +++ b/tests/unit/field_pointers_malloced_struct.cpp @@ -0,0 +1,42 @@ +#include +#include + +struct S { + int a; + int b; + int c; +}; + +static int bump(S *s) { + s->b += 10; + return s->b; +} + +int main() { + S *s = static_cast(calloc(1, sizeof(S))); + assert(s != nullptr); + s->b = 1; + s->a = bump(s); + assert(s->a == 11); + assert(s->b == 11); + s->a = 1; + s->b = 2; + s->c = 0; + if (s->a < s->b && s->c++ == 0) { + s->a = 5; + } + assert(s->a == 5 && s->c == 1); + if (s->a < s->b && s->c++ == 0) { + s->a = 6; + } + assert(s->a == 5 && s->c == 1); + int x = s->a + (s->b = 3); + assert(x == 8 && s->b == 3); + int y = 0; + s->c = (y = 99); + assert(s->c == 99 && y == 99); + s->a += bump(s); + assert(s->a == 18 && s->b == 13 && s->c == 99); + free(s); + return 0; +} diff --git a/tests/unit/first_field_ptr_covers_struct.c b/tests/unit/first_field_ptr_covers_struct.c new file mode 100644 index 000000000..f06cd2419 --- /dev/null +++ b/tests/unit/first_field_ptr_covers_struct.c @@ -0,0 +1,32 @@ +#include +#include +#include + +struct In { + short a; + short b; +}; + +struct S { + int x; + struct In in; + int z; +}; + +int main(void) { + struct S src = {1, {2, 3}, 4}; + struct S *p = malloc(sizeof(struct S)); + assert(p != NULL); + memcpy(&p->x, &src, sizeof(*p)); + assert(p->x == 1 && p->in.a == 2 && p->in.b == 3 && p->z == 4); + struct In n = {5, 6}; + memcpy(&p->in, &n, sizeof(n)); + assert(p->x == 1 && p->in.a == 5 && p->in.b == 6 && p->z == 4); + unsigned char *bz = (unsigned char *)&p->z; + for (int i = 0; i < 4; i++) { + bz[i] = 1; + } + assert(p->z == 0x01010101 && p->in.b == 6); + free(p); + return 0; +} diff --git a/tests/unit/first_member_downcast.c b/tests/unit/first_member_downcast.c new file mode 100644 index 000000000..4e10ce475 --- /dev/null +++ b/tests/unit/first_member_downcast.c @@ -0,0 +1,29 @@ +#include +#include + +typedef struct base { + int kind; +} base; + +typedef struct derived { + base head; + size_t value; +} derived; + +int main(void) { + derived *d = malloc(sizeof(*d)); + assert(d != NULL); + d->head.kind = 3; + d->value = 7; + base *b = &d->head; + derived *back = (derived *)b; + assert(back == d); + assert(back->value == 7); + assert(back->head.kind == 3); + back->value = 8; + assert(d->value == 8); + b->kind = 4; + assert(d->head.kind == 4); + free(back); + return 0; +} diff --git a/tests/unit/flex_array_member_sibling_write.c b/tests/unit/flex_array_member_sibling_write.c new file mode 100644 index 000000000..7245cba65 --- /dev/null +++ b/tests/unit/flex_array_member_sibling_write.c @@ -0,0 +1,46 @@ +#include +#include +#include + +struct S { + int n; + char name[1]; +}; + +struct E { + int id; + int w; +}; + +struct T { + int n; + int cap; + struct E a[1]; +}; + +int main(void) { + struct S *s = calloc(1, sizeof(struct S) + 8); + assert(s != NULL); + memcpy(s->name, "abcdefg", 8); + s->n = 5; + assert(s->n == 5); + assert(strcmp(s->name, "abcdefg") == 0); + free(s); + + struct T *t = malloc(sizeof(struct T) + sizeof(struct E)); + assert(t != NULL); + t->n = 2; + t->cap = 2; + t->a[0].id = 10; + t->a[1].w = 20; + t->n = 3; + assert(t->a[0].id == 10 && t->a[1].w == 20); + struct E *tail = (struct E *)&t[1]; + assert(tail == &t->a[1]); + tail[0].id = 30; + t->cap = 4; + assert(t->a[1].id == 30 && t->a[1].w == 20); + assert(t->n == 3 && t->cap == 4); + free(t); + return 0; +} diff --git a/tests/unit/memset_struct_tail.c b/tests/unit/memset_struct_tail.c new file mode 100644 index 000000000..cb3a64914 --- /dev/null +++ b/tests/unit/memset_struct_tail.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +struct S { + int keep; + int a; + long b; + char c[5]; + int last; +}; + +int main(void) { + struct S *p = malloc(sizeof(struct S)); + assert(p != NULL); + memset(p, 0xff, sizeof(*p)); + p->keep = 7; + memset(&p->a, 0, sizeof(struct S) - offsetof(struct S, a)); + assert(p->keep == 7); + assert(p->a == 0 && p->b == 0 && p->c[4] == 0 && p->last == 0); + p->a = 1; + p->b = 2; + p->c[0] = 'x'; + p->last = 3; + memset(&p->b, 0, offsetof(struct S, last) - offsetof(struct S, b)); + assert(p->keep == 7 && p->a == 1); + assert(p->b == 0 && p->c[0] == 0); + assert(p->last == 3); + free(p); + return 0; +} diff --git a/tests/unit/out/refcount/array_field_addr.rs b/tests/unit/out/refcount/array_field_addr.rs new file mode 100644 index 000000000..59545dcce --- /dev/null +++ b/tests/unit/out/refcount/array_field_addr.rs @@ -0,0 +1,92 @@ +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(VaArg, FnPtrArg)] +pub struct S { + pub before: Value, + pub mask: Value>, + pub after: Value, +} +impl Clone for S { + fn clone(&self) -> Self { + Self { + before: Rc::new(RefCell::new((*self.before.borrow()).clone())), + mask: Rc::new(RefCell::new((*self.mask.borrow()).clone())), + after: Rc::new(RefCell::new((*self.after.borrow()).clone())), + } + } +} +impl Default for S { + fn default() -> Self { + S { + before: Rc::new(RefCell::new(0_i32)), + mask: Rc::new(RefCell::new((0..4).map(|_| 0_u8).collect::>())), + after: Rc::new(RefCell::new(0_i32)), + } + } +} +impl ByteRepr for S { + fn byte_size() -> usize { + 12 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.before.borrow()).to_bytes(&mut buf[0..4]); + (*self.mask.borrow()).to_bytes(&mut buf[4..8]); + (*self.after.borrow()).to_bytes(&mut buf[8..12]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + before: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + mask: Rc::new(RefCell::new(>::from_bytes(&buf[4..8]))), + after: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + } + } +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let s: Value> = Rc::new(RefCell::new( + libcc2rs::malloc_refcount(12usize).reinterpret_cast::(), + )); + assert!((((!((*s.borrow()).is_null())) as i32) != 0)); + (*(*(*s.borrow()).upgrade().deref()).before.borrow_mut()) = 1; + { + (((*(*s.borrow()).upgrade().deref()).mask.as_pointer() as Ptr) as Ptr) + .to_any() + .memset((5) as u8, ::std::mem::size_of::<[u8; 4]>() as usize); + (((*(*s.borrow()).upgrade().deref()).mask.as_pointer() as Ptr) as Ptr).to_any() + }; + (*(*(*s.borrow()).upgrade().deref()).after.borrow_mut()) = 2; + ((*(*s.borrow()).upgrade().deref()).mask.as_pointer()) + .reinterpret_cast::() + .write(7_u8); + let out: Value> = Rc::new(RefCell::new((0..4).map(|_| 0_u8).collect::>())); + { + ((out.as_pointer() as Ptr) as Ptr).to_any().memcpy( + &(((*(*s.borrow()).upgrade().deref()).mask.as_pointer()) as Ptr).to_any(), + ::std::mem::size_of::<[u8; 4]>() as usize, + ); + ((out.as_pointer() as Ptr) as Ptr).to_any() + }; + assert!( + ((((((((*out.borrow())[(0) as usize] as i32) == 7) as i32) != 0) + && (((((*out.borrow())[(3) as usize] as i32) == 5) as i32) != 0)) as i32) + != 0) + ); + assert!( + (((((((*(*(*s.borrow()).upgrade().deref()).before.borrow()) == 1) as i32) != 0) + && ((((*(*(*s.borrow()).upgrade().deref()).after.borrow()) == 2) as i32) != 0)) + as i32) + != 0) + ); + libcc2rs::free_refcount((*s.borrow()).to_any()); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/container_field_addr.rs b/tests/unit/out/refcount/container_field_addr.rs new file mode 100644 index 000000000..be527e0f3 --- /dev/null +++ b/tests/unit/out/refcount/container_field_addr.rs @@ -0,0 +1,166 @@ +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(VaArg, FnPtrArg, Default)] +pub struct S { + pub tag: Value, + pub v: Value>, + pub s: Value>, + pub m: Value>>, +} +impl Clone for S { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + tag: Rc::new(RefCell::new((*self.tag.borrow()))), + v: Rc::new(RefCell::new((*self.v.borrow()).clone())), + s: Rc::new(RefCell::new((*self.s.borrow()).clone())), + m: Rc::new(RefCell::new( + (*self.m.borrow()) + .iter() + .map(|(k, v)| (k.clone(), Rc::new(RefCell::new(v.borrow().clone())))) + .collect(), + )), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl ByteRepr for S { + fn byte_size() -> usize { + 112 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.tag.borrow()).to_bytes(&mut buf[0..4]); + (*self.v.borrow()).to_bytes(&mut buf[8..32]); + (*self.s.borrow()).to_bytes(&mut buf[32..64]); + (*self.m.borrow()).to_bytes(&mut buf[64..112]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + tag: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + v: Rc::new(RefCell::new(>::from_bytes(&buf[8..32]))), + s: Rc::new(RefCell::new(>::from_bytes(&buf[32..64]))), + m: Rc::new(RefCell::new(>>::from_bytes( + &buf[64..112], + ))), + } + } +} +pub fn add_0(v: Ptr>, k: i32) { + let v: Value>> = Rc::new(RefCell::new(v)); + let k: Value = Rc::new(RefCell::new(k)); + { + let a0_clone = (*k.borrow()).clone(); + (*v.borrow()).with_mut(|__v: &mut Vec| __v.push(a0_clone)) + }; +} +pub fn append_1(s: Ptr>, t: Ptr, n: usize) { + let s: Value>> = Rc::new(RefCell::new(s)); + let t: Value> = Rc::new(RefCell::new(t)); + let n: Value = Rc::new(RefCell::new(n)); + { + ((*s.borrow()).clone() as Ptr>).with_mut(|__v: &mut Vec| { + __v.pop(); + __v.extend( + (*t.borrow()) + .clone() + .map(|c| c.read()) + .take((*n.borrow()) as usize), + ); + __v.push(0); + }); + ((*s.borrow()).clone() as Ptr>) + }; +} +pub fn put_2(m: Ptr>>, k: i32, v: i32) { + let m: Value>>> = Rc::new(RefCell::new(m)); + let k: Value = Rc::new(RefCell::new(k)); + let v: Value = Rc::new(RefCell::new(v)); + ((*m.borrow()).clone() as Ptr>>) + .with_mut(|__v: &mut BTreeMap>| { + __v.entry((*k.borrow())) + .or_insert_with(|| Rc::new(RefCell::new(::default()))) + .as_pointer() + }) + .write((*v.borrow())); +} +pub fn run_3(h: Ptr) { + let h: Value> = Rc::new(RefCell::new(h)); + ({ + let _v: Ptr> = ((*(*h.borrow()).upgrade().deref()).v.as_pointer()); + let _k: i32 = (*(*(*h.borrow()).upgrade().deref()).tag.borrow()); + add_0(_v, _k) + }); + ({ + append_1( + ((*(*h.borrow()).upgrade().deref()).s.as_pointer()), + Ptr::::from_string_literal(b"ab"), + 2_usize, + ) + }); + ({ + let _m: Ptr>> = + ((*(*h.borrow()).upgrade().deref()).m.as_pointer()); + let _k: i32 = (*(*(*h.borrow()).upgrade().deref()).tag.borrow()); + put_2(_m, _k, 2) + }); + let pv: Value>> = Rc::new(RefCell::new( + ((*(*h.borrow()).upgrade().deref()).v.as_pointer()), + )); + (*pv.borrow()).with_mut(|__v: &mut Vec| { + __v.push(((*(*pv.borrow()).upgrade().deref()).len() as i32)) + }); + assert!( + (((*(*(*h.borrow()).upgrade().deref()).v.borrow()).len() == 2_usize) + && ((((*(*h.borrow()).upgrade().deref()).v.as_pointer() as Ptr) + .offset(0_usize) + .read()) + == 7)) + && ((((*(*h.borrow()).upgrade().deref()).v.as_pointer() as Ptr) + .offset(1_usize) + .read()) + == 1) + ); + assert!( + (*(*(*h.borrow()).upgrade().deref()).s.borrow()) + .iter() + .copied() + .take( + (*(*(*h.borrow()).upgrade().deref()).s.borrow()) + .len() + .saturating_sub(1) + ) + .eq(Ptr::::from_string_literal(b"ab").to_c_string_iterator()) + ); + assert!( + ((((*(*h.borrow()).upgrade().deref()).m.as_pointer() as Ptr>>) + .with_mut(|__v: &mut BTreeMap>| { + __v.entry(7) + .or_insert_with(|| Rc::new(RefCell::new(::default()))) + .as_pointer() + }) + .read()) + == 2) + ); + assert!(((*(*(*h.borrow()).upgrade().deref()).tag.borrow()) == 7)); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let local: Value = Rc::new(RefCell::new(::default())); + (*(*local.borrow()).tag.borrow_mut()) = 7; + ({ run_3((local.as_pointer())) }); + let heap: Value> = Rc::new(RefCell::new(Ptr::alloc(::default()))); + (*(*(*heap.borrow()).upgrade().deref()).tag.borrow_mut()) = 7; + ({ run_3((*heap.borrow()).clone()) }); + (*heap.borrow()).delete(); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/field_aliasing.rs b/tests/unit/out/refcount/field_aliasing.rs new file mode 100644 index 000000000..bf083e991 --- /dev/null +++ b/tests/unit/out/refcount/field_aliasing.rs @@ -0,0 +1,185 @@ +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(VaArg, FnPtrArg, Default)] +pub struct In { + pub x: Value, + pub y: Value, +} +impl Clone for In { + fn clone(&self) -> Self { + Self { + x: Rc::new(RefCell::new((*self.x.borrow()).clone())), + y: Rc::new(RefCell::new((*self.y.borrow()).clone())), + } + } +} +impl ByteRepr for In { + fn byte_size() -> usize { + 8 + } + 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(VaArg, FnPtrArg)] +pub struct S { + pub in_: Value, + pub total: Value, + pub n: Value, + pub arr: Value>, +} +impl Clone for S { + fn clone(&self) -> Self { + Self { + in_: Rc::new(RefCell::new((*self.in_.borrow()).clone())), + total: Rc::new(RefCell::new((*self.total.borrow()).clone())), + n: Rc::new(RefCell::new((*self.n.borrow()).clone())), + arr: Rc::new(RefCell::new((*self.arr.borrow()).clone())), + } + } +} +impl Default for S { + fn default() -> Self { + S { + in_: >::default(), + total: Rc::new(RefCell::new(0_i32)), + n: Rc::new(RefCell::new(0_i32)), + arr: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), + } + } +} +impl ByteRepr for S { + fn byte_size() -> usize { + 32 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.in_.borrow()).to_bytes(&mut buf[0..8]); + (*self.total.borrow()).to_bytes(&mut buf[8..12]); + (*self.n.borrow()).to_bytes(&mut buf[12..16]); + (*self.arr.borrow()).to_bytes(&mut buf[16..32]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + in_: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), + total: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + n: Rc::new(RefCell::new(::from_bytes(&buf[12..16]))), + arr: Rc::new(RefCell::new(>::from_bytes(&buf[16..32]))), + } + } +} +#[derive(VaArg, FnPtrArg, Default)] +pub struct Node { + pub x: Value, + pub self_: Value>, +} +impl Clone for Node { + fn clone(&self) -> Self { + Self { + x: Rc::new(RefCell::new((*self.x.borrow()).clone())), + self_: Rc::new(RefCell::new((*self.self_.borrow()).clone())), + } + } +} +impl ByteRepr for Node { + fn byte_size() -> usize { + 16 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + (*self.self_.borrow()).to_bytes(&mut buf[8..16]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + self_: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + } + } +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let p: Value> = Rc::new(RefCell::new( + libcc2rs::calloc_refcount(1_usize, 32usize).reinterpret_cast::(), + )); + assert!((((!((*p.borrow()).is_null())) as i32) != 0)); + let q: Value> = Rc::new(RefCell::new((*p.borrow()).clone())); + (*(*(*(*p.borrow()).upgrade().deref()).in_.borrow()) + .x + .borrow_mut()) = 1; + (*(*(*(*p.borrow()).upgrade().deref()).in_.borrow()) + .y + .borrow_mut()) = 2; + (*(*(*p.borrow()).upgrade().deref()).total.borrow_mut()) = { + let _lhs = (*(*(*(*q.borrow()).upgrade().deref()).in_.borrow()) + .x + .borrow()); + _lhs + (*(*(*(*q.borrow()).upgrade().deref()).in_.borrow()) + .y + .borrow()) + }; + assert!(((((*(*(*q.borrow()).upgrade().deref()).total.borrow()) == 3) as i32) != 0)); + let ip: Value> = Rc::new(RefCell::new( + ((*(*p.borrow()).upgrade().deref()).in_.as_pointer()), + )); + (*(*(*ip.borrow()).upgrade().deref()).x.borrow_mut()) = + ((*(*(*p.borrow()).upgrade().deref()).total.borrow()) + 1); + assert!( + (((((((*(*(*(*q.borrow()).upgrade().deref()).in_.borrow()) + .x + .borrow()) + == 4) as i32) + != 0) + && ((((*(*(*(*q.borrow()).upgrade().deref()).in_.borrow()) + .y + .borrow()) + == 2) as i32) + != 0)) as i32) + != 0) + ); + let __rhs = (*(*(*p.borrow()).upgrade().deref()).total.borrow()); + (*(*(*p.borrow()).upgrade().deref()).arr.borrow_mut()) + [(*(*(*p.borrow()).upgrade().deref()).n.borrow()) as usize] = __rhs; + (*(*(*p.borrow()).upgrade().deref()).n.borrow_mut()) += 1; + (*(*(*p.borrow()).upgrade().deref()).arr.borrow_mut()) + [(*(*(*p.borrow()).upgrade().deref()).n.borrow()) as usize] = + (*(*(*(*q.borrow()).upgrade().deref()).in_.borrow()) + .x + .borrow()); + assert!( + ((((((((((*(*(*q.borrow()).upgrade().deref()).arr.borrow())[(0) as usize] == 3) as i32) + != 0) + && ((((*(*(*q.borrow()).upgrade().deref()).arr.borrow())[(1) as usize] == 4) as i32) + != 0)) as i32) + != 0) + && ((((*(*(*q.borrow()).upgrade().deref()).n.borrow()) == 1) as i32) != 0)) + as i32) + != 0) + ); + libcc2rs::free_refcount((*p.borrow()).to_any()); + let s: Value = >::default(); + (*(*s.borrow()).x.borrow_mut()) = 1; + let __rhs = (s.as_pointer()); + (*(*s.borrow()).self_.borrow_mut()) = __rhs; + let __rhs = ((*(*s.borrow()).x.borrow()) + 1); + (*(*(*(*s.borrow()).self_.borrow()).upgrade().deref()) + .x + .borrow_mut()) = __rhs; + assert!(((((*(*s.borrow()).x.borrow()) == 2) as i32) != 0)); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/field_method_receiver.rs b/tests/unit/out/refcount/field_method_receiver.rs new file mode 100644 index 000000000..f03717be2 --- /dev/null +++ b/tests/unit/out/refcount/field_method_receiver.rs @@ -0,0 +1,205 @@ +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(VaArg, FnPtrArg, Default)] +pub struct Counter { + pub n: Value, +} +impl Clone for Counter { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + n: Rc::new(RefCell::new((*self.n.borrow()))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl ByteRepr for Counter { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.n.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + n: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} +#[derive(VaArg, FnPtrArg)] +pub struct S { + pub tag: Value, + pub c: Value, + pub arr: Value>, + pub v: Value>, +} +impl Clone for S { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + tag: Rc::new(RefCell::new((*self.tag.borrow()))), + c: Rc::new(RefCell::new((*self.c.borrow()).clone())), + arr: Rc::new(RefCell::new(Box::new(std::array::from_fn::<_, 2, _>( + |__i: usize| ((*self.arr.borrow())[(__i) as usize]).clone(), + )))), + v: Rc::new(RefCell::new((*self.v.borrow()).clone())), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl Default for S { + fn default() -> Self { + S { + tag: Rc::new(RefCell::new(0_i32)), + c: >::default(), + arr: Rc::new(RefCell::new( + (0..2) + .map(|_| ::default()) + .collect::>(), + )), + v: Rc::new(RefCell::new(Default::default())), + } + } +} +impl ByteRepr for S { + fn byte_size() -> usize { + 40 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.tag.borrow()).to_bytes(&mut buf[0..4]); + (*self.c.borrow()).to_bytes(&mut buf[4..8]); + (*self.arr.borrow()).to_bytes(&mut buf[8..16]); + (*self.v.borrow()).to_bytes(&mut buf[16..40]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + tag: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + c: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + arr: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), + v: Rc::new(RefCell::new(>::from_bytes(&buf[16..40]))), + } + } +} +pub fn run_0(o: Ptr) { + let o: Value> = Rc::new(RefCell::new(o)); + ({ CounterImpl::add(&(*(*o.borrow()).upgrade().deref()).c.as_pointer(), 2) }); + assert!((({ CounterImpl::get(&(*(*o.borrow()).upgrade().deref()).c.as_pointer(),) }) == 2)); + ({ + CounterImpl::add( + &((*(*o.borrow()).upgrade().deref()).arr.as_pointer() as Ptr).offset(1), + 5, + ) + }); + assert!( + (({ + CounterImpl::get( + &((*(*o.borrow()).upgrade().deref()).arr.as_pointer() as Ptr).offset(1), + ) + }) == 5) + ); + ({ + CounterImpl::add( + &({ CounterImpl::self_(&(*(*o.borrow()).upgrade().deref()).c.as_pointer()) }), + 1, + ) + }); + assert!((({ CounterImpl::get(&(*(*o.borrow()).upgrade().deref()).c.as_pointer(),) }) == 3)); + assert!({ + let _lhs = ({ CounterImpl::self_(&(*(*o.borrow()).upgrade().deref()).c.as_pointer()) }); + _lhs == ((*(*o.borrow()).upgrade().deref()).c.as_pointer()) + }); + ({ + let _other: Ptr = + (((*(*o.borrow()).upgrade().deref()).arr.as_pointer() as Ptr).offset(1)); + CounterImpl::take( + &((*(*o.borrow()).upgrade().deref()).arr.as_pointer() as Ptr).offset(0), + _other, + ) + }); + assert!( + (({ + CounterImpl::get( + &((*(*o.borrow()).upgrade().deref()).arr.as_pointer() as Ptr).offset(0), + ) + }) == 5) + && (({ + CounterImpl::get( + &((*(*o.borrow()).upgrade().deref()).arr.as_pointer() as Ptr) + .offset(1), + ) + }) == 0) + ); + ({ + let _other: Ptr = ((*(*o.borrow()).upgrade().deref()).c.as_pointer()); + CounterImpl::take(&(*(*o.borrow()).upgrade().deref()).c.as_pointer(), _other) + }); + assert!((({ CounterImpl::get(&(*(*o.borrow()).upgrade().deref()).c.as_pointer(),) }) == 0)); + ({ SImpl::bump(&(*o.borrow())) }); + assert!((({ CounterImpl::get(&(*(*o.borrow()).upgrade().deref()).c.as_pointer(),) }) == 1)); + (*(*(*o.borrow()).upgrade().deref()).v.borrow_mut()) + .push(({ CounterImpl::get(&(*(*o.borrow()).upgrade().deref()).c.as_pointer()) })); + assert!( + ((*(*(*o.borrow()).upgrade().deref()).v.borrow()).len() == 1_usize) + && ((((*(*o.borrow()).upgrade().deref()).v.as_pointer() as Ptr) + .offset(0_usize) + .read()) + == 1) + ); + assert!(((*(*(*o.borrow()).upgrade().deref()).tag.borrow()) == 1)); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let local: Value = Rc::new(RefCell::new(::default())); + (*(*local.borrow()).tag.borrow_mut()) = 1; + ({ run_0((local.as_pointer())) }); + let heap: Value> = Rc::new(RefCell::new(Ptr::alloc(::default()))); + (*(*(*heap.borrow()).upgrade().deref()).tag.borrow_mut()) = 1; + ({ run_0((*heap.borrow()).clone()) }); + (*heap.borrow()).delete(); + return 0; +} +pub trait CounterImpl { + fn get(&self) -> i32; + fn add(&self, k: i32); + fn self_(&self) -> Ptr; + fn take(&self, other: Ptr); +} +impl CounterImpl for Ptr { + fn get(&self) -> i32 { + return (*(*(*self).upgrade().deref()).n.borrow()); + } + fn add(&self, k: i32) { + let k: Value = Rc::new(RefCell::new(k)); + (*(*(*self).upgrade().deref()).n.borrow_mut()) += (*k.borrow()); + } + fn self_(&self) -> Ptr { + return (*self).clone(); + } + fn take(&self, other: Ptr) { + let other: Value> = Rc::new(RefCell::new(other)); + let __rhs = (*(*(*other.borrow()).upgrade().deref()).n.borrow()); + (*(*(*self).upgrade().deref()).n.borrow_mut()) += __rhs; + (*(*(*other.borrow()).upgrade().deref()).n.borrow_mut()) = 0; + } +} +pub trait SImpl { + fn bump(&self); +} +impl SImpl for Ptr { + fn bump(&self) { + ({ + let _k: i32 = (*(*(*self).upgrade().deref()).tag.borrow()); + CounterImpl::add(&(*(*self).upgrade().deref()).c.as_pointer(), _k) + }); + } +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/field_pointers_malloced_struct.rs b/tests/unit/out/refcount/field_pointers_malloced_struct.rs new file mode 100644 index 000000000..575df1dfb --- /dev/null +++ b/tests/unit/out/refcount/field_pointers_malloced_struct.rs @@ -0,0 +1,111 @@ +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(VaArg, FnPtrArg, Default)] +pub struct S { + pub a: Value, + pub b: Value, + pub c: Value, +} +impl Clone for S { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + a: Rc::new(RefCell::new((*self.a.borrow()))), + b: Rc::new(RefCell::new((*self.b.borrow()))), + c: Rc::new(RefCell::new((*self.c.borrow()))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl ByteRepr for S { + fn byte_size() -> usize { + 12 + } + 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]); + (*self.c.borrow()).to_bytes(&mut buf[8..12]); + } + 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]))), + c: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + } + } +} +pub fn bump_0(s: Ptr) -> i32 { + let s: Value> = Rc::new(RefCell::new(s)); + (*(*(*s.borrow()).upgrade().deref()).b.borrow_mut()) += 10; + return (*(*(*s.borrow()).upgrade().deref()).b.borrow()); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let s: Value> = Rc::new(RefCell::new( + libcc2rs::calloc_refcount(1_usize, 12usize).reinterpret_cast::(), + )); + assert!(!((*s.borrow()).is_null())); + (*(*(*s.borrow()).upgrade().deref()).b.borrow_mut()) = 1; + let __rhs = ({ bump_0((*s.borrow()).clone()) }); + (*(*(*s.borrow()).upgrade().deref()).a.borrow_mut()) = __rhs; + assert!(((*(*(*s.borrow()).upgrade().deref()).a.borrow()) == 11)); + assert!(((*(*(*s.borrow()).upgrade().deref()).b.borrow()) == 11)); + (*(*(*s.borrow()).upgrade().deref()).a.borrow_mut()) = 1; + (*(*(*s.borrow()).upgrade().deref()).b.borrow_mut()) = 2; + (*(*(*s.borrow()).upgrade().deref()).c.borrow_mut()) = 0; + if ({ + let _lhs = (*(*(*s.borrow()).upgrade().deref()).a.borrow()); + _lhs < (*(*(*s.borrow()).upgrade().deref()).b.borrow()) + }) && ((*(*(*s.borrow()).upgrade().deref()).c.borrow_mut()).postfix_inc() == 0) + { + (*(*(*s.borrow()).upgrade().deref()).a.borrow_mut()) = 5; + } + assert!( + ((*(*(*s.borrow()).upgrade().deref()).a.borrow()) == 5) + && ((*(*(*s.borrow()).upgrade().deref()).c.borrow()) == 1) + ); + if ({ + let _lhs = (*(*(*s.borrow()).upgrade().deref()).a.borrow()); + _lhs < (*(*(*s.borrow()).upgrade().deref()).b.borrow()) + }) && ((*(*(*s.borrow()).upgrade().deref()).c.borrow_mut()).postfix_inc() == 0) + { + (*(*(*s.borrow()).upgrade().deref()).a.borrow_mut()) = 6; + } + assert!( + ((*(*(*s.borrow()).upgrade().deref()).a.borrow()) == 5) + && ((*(*(*s.borrow()).upgrade().deref()).c.borrow()) == 1) + ); + let x: Value = Rc::new(RefCell::new({ + let _lhs = (*(*(*s.borrow()).upgrade().deref()).a.borrow()); + _lhs + ({ + (*(*(*s.borrow()).upgrade().deref()).b.borrow_mut()) = 3; + (*(*(*s.borrow()).upgrade().deref()).b.borrow()) + }) + })); + assert!(((*x.borrow()) == 8) && ((*(*(*s.borrow()).upgrade().deref()).b.borrow()) == 3)); + let y: Value = Rc::new(RefCell::new(0)); + (*(*(*s.borrow()).upgrade().deref()).c.borrow_mut()) = ({ + (*y.borrow_mut()) = 99; + (*y.borrow()) + }); + assert!(((*(*(*s.borrow()).upgrade().deref()).c.borrow()) == 99) && ((*y.borrow()) == 99)); + let __rhs = ({ bump_0((*s.borrow()).clone()) }); + (*(*(*s.borrow()).upgrade().deref()).a.borrow_mut()) += __rhs; + assert!( + (((*(*(*s.borrow()).upgrade().deref()).a.borrow()) == 18) + && ((*(*(*s.borrow()).upgrade().deref()).b.borrow()) == 13)) + && ((*(*(*s.borrow()).upgrade().deref()).c.borrow()) == 99) + ); + libcc2rs::free_refcount((*s.borrow()).to_any()); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/first_field_ptr_covers_struct.rs b/tests/unit/out/refcount/first_field_ptr_covers_struct.rs new file mode 100644 index 000000000..78fd3459c --- /dev/null +++ b/tests/unit/out/refcount/first_field_ptr_covers_struct.rs @@ -0,0 +1,158 @@ +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(VaArg, FnPtrArg, Default)] +pub struct In { + pub a: Value, + pub b: Value, +} +impl Clone for In { + fn clone(&self) -> Self { + Self { + a: Rc::new(RefCell::new((*self.a.borrow()).clone())), + b: Rc::new(RefCell::new((*self.b.borrow()).clone())), + } + } +} +impl ByteRepr for In { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.a.borrow()).to_bytes(&mut buf[0..2]); + (*self.b.borrow()).to_bytes(&mut buf[2..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + a: Rc::new(RefCell::new(::from_bytes(&buf[0..2]))), + b: Rc::new(RefCell::new(::from_bytes(&buf[2..4]))), + } + } +} +#[derive(VaArg, FnPtrArg, Default)] +pub struct S { + pub x: Value, + pub in_: Value, + pub z: Value, +} +impl Clone for S { + fn clone(&self) -> Self { + Self { + x: Rc::new(RefCell::new((*self.x.borrow()).clone())), + in_: Rc::new(RefCell::new((*self.in_.borrow()).clone())), + z: Rc::new(RefCell::new((*self.z.borrow()).clone())), + } + } +} +impl ByteRepr for S { + fn byte_size() -> usize { + 12 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + (*self.in_.borrow()).to_bytes(&mut buf[4..8]); + (*self.z.borrow()).to_bytes(&mut buf[8..12]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + in_: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + z: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + } + } +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let src: Value = Rc::new(RefCell::new(S { + x: Rc::new(RefCell::new(1)), + in_: Rc::new(RefCell::new(In { + a: Rc::new(RefCell::new(2_i16)), + b: Rc::new(RefCell::new(3_i16)), + })), + z: Rc::new(RefCell::new(4)), + })); + let p: Value> = Rc::new(RefCell::new( + libcc2rs::malloc_refcount(12usize).reinterpret_cast::(), + )); + assert!((((!((*p.borrow()).is_null())) as i32) != 0)); + { + (((*(*p.borrow()).upgrade().deref()).x.as_pointer()) as Ptr) + .to_any() + .memcpy(&((src.as_pointer()) as Ptr).to_any(), 12usize as usize); + (((*(*p.borrow()).upgrade().deref()).x.as_pointer()) as Ptr).to_any() + }; + assert!( + (((((((((((((*(*(*p.borrow()).upgrade().deref()).x.borrow()) == 1) as i32) != 0) + && (((((*(*(*(*p.borrow()).upgrade().deref()).in_.borrow()) + .a + .borrow()) as i32) + == 2) as i32) + != 0)) as i32) + != 0) + && (((((*(*(*(*p.borrow()).upgrade().deref()).in_.borrow()) + .b + .borrow()) as i32) + == 3) as i32) + != 0)) as i32) + != 0) + && ((((*(*(*p.borrow()).upgrade().deref()).z.borrow()) == 4) as i32) != 0)) + as i32) + != 0) + ); + let n: Value = Rc::new(RefCell::new(In { + a: Rc::new(RefCell::new(5_i16)), + b: Rc::new(RefCell::new(6_i16)), + })); + { + (((*(*p.borrow()).upgrade().deref()).in_.as_pointer()) as Ptr) + .to_any() + .memcpy(&((n.as_pointer()) as Ptr).to_any(), 4usize as usize); + (((*(*p.borrow()).upgrade().deref()).in_.as_pointer()) as Ptr).to_any() + }; + assert!( + (((((((((((((*(*(*p.borrow()).upgrade().deref()).x.borrow()) == 1) as i32) != 0) + && (((((*(*(*(*p.borrow()).upgrade().deref()).in_.borrow()) + .a + .borrow()) as i32) + == 5) as i32) + != 0)) as i32) + != 0) + && (((((*(*(*(*p.borrow()).upgrade().deref()).in_.borrow()) + .b + .borrow()) as i32) + == 6) as i32) + != 0)) as i32) + != 0) + && ((((*(*(*p.borrow()).upgrade().deref()).z.borrow()) == 4) as i32) != 0)) + as i32) + != 0) + ); + let bz: Value> = Rc::new(RefCell::new( + ((*(*p.borrow()).upgrade().deref()).z.as_pointer()).reinterpret_cast::(), + )); + let i: Value = Rc::new(RefCell::new(0)); + 'loop_: while ((((*i.borrow()) < 4) as i32) != 0) { + (*bz.borrow()).offset((*i.borrow()) as isize).write(1_u8); + (*i.borrow_mut()).postfix_inc(); + } + assert!( + (((((((*(*(*p.borrow()).upgrade().deref()).z.borrow()) == 16843009) as i32) != 0) + && (((((*(*(*(*p.borrow()).upgrade().deref()).in_.borrow()) + .b + .borrow()) as i32) + == 6) as i32) + != 0)) as i32) + != 0) + ); + libcc2rs::free_refcount((*p.borrow()).to_any()); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/first_member_downcast.rs b/tests/unit/out/refcount/first_member_downcast.rs new file mode 100644 index 000000000..55b74f468 --- /dev/null +++ b/tests/unit/out/refcount/first_member_downcast.rs @@ -0,0 +1,107 @@ +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(VaArg, FnPtrArg, Default)] +pub struct base { + pub kind: Value, +} +impl Clone for base { + fn clone(&self) -> Self { + Self { + kind: Rc::new(RefCell::new((*self.kind.borrow()).clone())), + } + } +} +impl ByteRepr for base { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.kind.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + kind: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} +#[derive(VaArg, FnPtrArg, Default)] +pub struct derived { + pub head: Value, + pub value: Value, +} +impl Clone for derived { + fn clone(&self) -> Self { + Self { + head: Rc::new(RefCell::new((*self.head.borrow()).clone())), + value: Rc::new(RefCell::new((*self.value.borrow()).clone())), + } + } +} +impl ByteRepr for derived { + fn byte_size() -> usize { + 16 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.head.borrow()).to_bytes(&mut buf[0..4]); + (*self.value.borrow()).to_bytes(&mut buf[8..16]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + head: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + value: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + } + } +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let d: Value> = Rc::new(RefCell::new( + libcc2rs::malloc_refcount(16usize).reinterpret_cast::(), + )); + assert!((((!((*d.borrow()).is_null())) as i32) != 0)); + (*(*(*(*d.borrow()).upgrade().deref()).head.borrow()) + .kind + .borrow_mut()) = 3; + (*(*(*d.borrow()).upgrade().deref()).value.borrow_mut()) = 7_usize; + let b: Value> = Rc::new(RefCell::new( + ((*(*d.borrow()).upgrade().deref()).head.as_pointer()), + )); + let back: Value> = + Rc::new(RefCell::new((*b.borrow()).reinterpret_cast::())); + assert!( + ((({ + let _lhs = (*back.borrow()).clone(); + _lhs == (*d.borrow()).clone() + }) as i32) + != 0) + ); + assert!(((((*(*(*back.borrow()).upgrade().deref()).value.borrow()) == 7_usize) as i32) != 0)); + assert!( + ((((*(*(*(*back.borrow()).upgrade().deref()).head.borrow()) + .kind + .borrow()) + == 3) as i32) + != 0) + ); + (*(*(*back.borrow()).upgrade().deref()).value.borrow_mut()) = 8_usize; + assert!(((((*(*(*d.borrow()).upgrade().deref()).value.borrow()) == 8_usize) as i32) != 0)); + (*(*(*b.borrow()).upgrade().deref()).kind.borrow_mut()) = 4; + assert!( + ((((*(*(*(*d.borrow()).upgrade().deref()).head.borrow()) + .kind + .borrow()) + == 4) as i32) + != 0) + ); + libcc2rs::free_refcount((*back.borrow()).to_any()); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/flex_array_member_sibling_write.rs b/tests/unit/out/refcount/flex_array_member_sibling_write.rs new file mode 100644 index 000000000..d98e6b685 --- /dev/null +++ b/tests/unit/out/refcount/flex_array_member_sibling_write.rs @@ -0,0 +1,219 @@ +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(VaArg, FnPtrArg)] +pub struct S { + pub n: Value, + pub name: Value>, +} +impl Clone for S { + fn clone(&self) -> Self { + Self { + n: Rc::new(RefCell::new((*self.n.borrow()).clone())), + name: Rc::new(RefCell::new((*self.name.borrow()).clone())), + } + } +} +impl Default for S { + fn default() -> Self { + S { + n: Rc::new(RefCell::new(0_i32)), + name: Rc::new(RefCell::new((0..1).map(|_| 0_u8).collect::>())), + } + } +} +impl ByteRepr for S { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.n.borrow()).to_bytes(&mut buf[0..4]); + (*self.name.borrow()).to_bytes(&mut buf[4..5]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + n: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + name: Rc::new(RefCell::new(>::from_bytes(&buf[4..5]))), + } + } +} +#[derive(VaArg, FnPtrArg, Default)] +pub struct E { + pub id: Value, + pub w: Value, +} +impl Clone for E { + fn clone(&self) -> Self { + Self { + id: Rc::new(RefCell::new((*self.id.borrow()).clone())), + w: Rc::new(RefCell::new((*self.w.borrow()).clone())), + } + } +} +impl ByteRepr for E { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.id.borrow()).to_bytes(&mut buf[0..4]); + (*self.w.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + id: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + w: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} +#[derive(VaArg, FnPtrArg)] +pub struct T { + pub n: Value, + pub cap: Value, + pub a: Value>, +} +impl Clone for T { + fn clone(&self) -> Self { + Self { + n: Rc::new(RefCell::new((*self.n.borrow()).clone())), + cap: Rc::new(RefCell::new((*self.cap.borrow()).clone())), + a: Rc::new(RefCell::new((*self.a.borrow()).clone())), + } + } +} +impl Default for T { + fn default() -> Self { + T { + n: Rc::new(RefCell::new(0_i32)), + cap: Rc::new(RefCell::new(0_i32)), + a: Rc::new(RefCell::new(>::default())), + } + } +} +impl ByteRepr for T { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.n.borrow()).to_bytes(&mut buf[0..4]); + (*self.cap.borrow()).to_bytes(&mut buf[4..8]); + (*self.a.borrow()).to_bytes(&mut buf[8..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + n: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + cap: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + a: Rc::new(RefCell::new(>::from_bytes(&buf[8..8]))), + } + } +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let s: Value> = Rc::new(RefCell::new( + libcc2rs::calloc_refcount(1_usize, (8usize as usize).wrapping_add(8_usize)) + .reinterpret_cast::(), + )); + assert!((((!((*s.borrow()).is_null())) as i32) != 0)); + { + (((*(*s.borrow()).upgrade().deref()).name.as_pointer() as Ptr) as Ptr) + .to_any() + .memcpy( + &Ptr::::from_string_literal(b"abcdefg").to_any(), + 8_usize as usize, + ); + (((*(*s.borrow()).upgrade().deref()).name.as_pointer() as Ptr) as Ptr).to_any() + }; + (*(*(*s.borrow()).upgrade().deref()).n.borrow_mut()) = 5; + assert!(((((*(*(*s.borrow()).upgrade().deref()).n.borrow()) == 5) as i32) != 0)); + assert!( + ((({ + let mut __it1 = ((*(*s.borrow()).upgrade().deref()).name.as_pointer() as Ptr) + .to_c_string_iterator(); + let mut __it2 = Ptr::::from_string_literal(b"abcdefg").to_c_string_iterator(); + loop { + let __c1 = __it1.next(); + let __c2 = __it2.next(); + if __c1 != __c2 { + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); + } + if __c1.is_none() { + break 0; + } + } + } == 0) as i32) + != 0) + ); + libcc2rs::free_refcount((*s.borrow()).to_any()); + let t: Value> = Rc::new(RefCell::new( + libcc2rs::malloc_refcount( + (8usize as usize).wrapping_add(((2_usize).wrapping_mul((8usize as usize)) as usize)), + ) + .reinterpret_cast::(), + )); + assert!((((!((*t.borrow()).is_null())) as i32) != 0)); + (*(*(*t.borrow()).upgrade().deref()).n.borrow_mut()) = 2; + (*(*(*t.borrow()).upgrade().deref()).cap.borrow_mut()) = 2; + (*(*(*(*t.borrow()).upgrade().deref()).a.borrow())[(0) as usize] + .id + .borrow_mut()) = 10; + (*(*(*(*t.borrow()).upgrade().deref()).a.borrow())[(1) as usize] + .w + .borrow_mut()) = 20; + (*(*(*t.borrow()).upgrade().deref()).n.borrow_mut()) = 3; + assert!( + (((((((*(*(*(*t.borrow()).upgrade().deref()).a.borrow())[(0) as usize] + .id + .borrow()) + == 10) as i32) + != 0) + && ((((*(*(*(*t.borrow()).upgrade().deref()).a.borrow())[(1) as usize] + .w + .borrow()) + == 20) as i32) + != 0)) as i32) + != 0) + ); + let tail: Value> = Rc::new(RefCell::new( + ((*t.borrow()).offset((1) as isize)).reinterpret_cast::(), + )); + assert!( + ((({ + let _lhs = (*tail.borrow()).clone(); + _lhs == ((*(*t.borrow()).upgrade().deref()).a.as_pointer() as Ptr) + }) as i32) + != 0) + ); + (*(*(*tail.borrow()).offset((1) as isize).upgrade().deref()) + .id + .borrow_mut()) = 30; + (*(*(*t.borrow()).upgrade().deref()).cap.borrow_mut()) = 4; + assert!( + (((((((*(*(*(*t.borrow()).upgrade().deref()).a.borrow())[(1) as usize] + .id + .borrow()) + == 30) as i32) + != 0) + && ((((*(*(*(*t.borrow()).upgrade().deref()).a.borrow())[(1) as usize] + .w + .borrow()) + == 20) as i32) + != 0)) as i32) + != 0) + ); + assert!( + (((((((*(*(*t.borrow()).upgrade().deref()).n.borrow()) == 3) as i32) != 0) + && ((((*(*(*t.borrow()).upgrade().deref()).cap.borrow()) == 4) as i32) != 0)) + as i32) + != 0) + ); + libcc2rs::free_refcount((*t.borrow()).to_any()); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/memset_struct_tail.rs b/tests/unit/out/refcount/memset_struct_tail.rs new file mode 100644 index 000000000..9636b41ae --- /dev/null +++ b/tests/unit/out/refcount/memset_struct_tail.rs @@ -0,0 +1,127 @@ +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(VaArg, FnPtrArg)] +pub struct S { + pub keep: Value, + pub a: Value, + pub b: Value, + pub c: Value>, + pub last: Value, +} +impl Clone for S { + fn clone(&self) -> Self { + Self { + keep: Rc::new(RefCell::new((*self.keep.borrow()).clone())), + a: Rc::new(RefCell::new((*self.a.borrow()).clone())), + b: Rc::new(RefCell::new((*self.b.borrow()).clone())), + c: Rc::new(RefCell::new((*self.c.borrow()).clone())), + last: Rc::new(RefCell::new((*self.last.borrow()).clone())), + } + } +} +impl Default for S { + fn default() -> Self { + S { + keep: Rc::new(RefCell::new(0_i32)), + a: Rc::new(RefCell::new(0_i32)), + b: Rc::new(RefCell::new(0_i64)), + c: Rc::new(RefCell::new((0..5).map(|_| 0_u8).collect::>())), + last: Rc::new(RefCell::new(0_i32)), + } + } +} +impl ByteRepr for S { + fn byte_size() -> usize { + 32 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.keep.borrow()).to_bytes(&mut buf[0..4]); + (*self.a.borrow()).to_bytes(&mut buf[4..8]); + (*self.b.borrow()).to_bytes(&mut buf[8..16]); + (*self.c.borrow()).to_bytes(&mut buf[16..21]); + (*self.last.borrow()).to_bytes(&mut buf[24..28]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + keep: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + a: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + b: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + c: Rc::new(RefCell::new(>::from_bytes(&buf[16..21]))), + last: Rc::new(RefCell::new(::from_bytes(&buf[24..28]))), + } + } +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let p: Value> = Rc::new(RefCell::new( + libcc2rs::malloc_refcount(32usize).reinterpret_cast::(), + )); + assert!((((!((*p.borrow()).is_null())) as i32) != 0)); + { + (*p.borrow()).to_any().memset((255) as u8, 32usize as usize); + (*p.borrow()).to_any() + }; + (*(*(*p.borrow()).upgrade().deref()).keep.borrow_mut()) = 7; + { + (((*(*p.borrow()).upgrade().deref()).a.as_pointer()) as Ptr) + .to_any() + .memset( + (0) as u8, + (32usize as usize).wrapping_sub((4_usize as usize)) as usize, + ); + (((*(*p.borrow()).upgrade().deref()).a.as_pointer()) as Ptr).to_any() + }; + assert!(((((*(*(*p.borrow()).upgrade().deref()).keep.borrow()) == 7) as i32) != 0)); + assert!( + (((((((((((((*(*(*p.borrow()).upgrade().deref()).a.borrow()) == 0) as i32) != 0) + && ((((*(*(*p.borrow()).upgrade().deref()).b.borrow()) == 0_i64) as i32) != 0)) + as i32) + != 0) + && (((((*(*(*p.borrow()).upgrade().deref()).c.borrow())[(4) as usize] as i32) == 0) + as i32) + != 0)) as i32) + != 0) + && ((((*(*(*p.borrow()).upgrade().deref()).last.borrow()) == 0) as i32) != 0)) + as i32) + != 0) + ); + (*(*(*p.borrow()).upgrade().deref()).a.borrow_mut()) = 1; + (*(*(*p.borrow()).upgrade().deref()).b.borrow_mut()) = 2_i64; + (*(*(*p.borrow()).upgrade().deref()).c.borrow_mut())[(0) as usize] = (('x' as i32) as u8); + (*(*(*p.borrow()).upgrade().deref()).last.borrow_mut()) = 3; + { + (((*(*p.borrow()).upgrade().deref()).b.as_pointer()) as Ptr) + .to_any() + .memset( + (0) as u8, + (24_usize as usize).wrapping_sub((8_usize as usize)) as usize, + ); + (((*(*p.borrow()).upgrade().deref()).b.as_pointer()) as Ptr).to_any() + }; + assert!( + (((((((*(*(*p.borrow()).upgrade().deref()).keep.borrow()) == 7) as i32) != 0) + && ((((*(*(*p.borrow()).upgrade().deref()).a.borrow()) == 1) as i32) != 0)) + as i32) + != 0) + ); + assert!( + (((((((*(*(*p.borrow()).upgrade().deref()).b.borrow()) == 0_i64) as i32) != 0) + && (((((*(*(*p.borrow()).upgrade().deref()).c.borrow())[(0) as usize] as i32) == 0) + as i32) + != 0)) as i32) + != 0) + ); + assert!(((((*(*(*p.borrow()).upgrade().deref()).last.borrow()) == 3) as i32) != 0)); + libcc2rs::free_refcount((*p.borrow()).to_any()); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/refcount/struct_2d_array_field.rs b/tests/unit/out/refcount/struct_2d_array_field.rs new file mode 100644 index 000000000..486add8b2 --- /dev/null +++ b/tests/unit/out/refcount/struct_2d_array_field.rs @@ -0,0 +1,242 @@ +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(VaArg, FnPtrArg)] +pub struct table { + pub rows: Value>]>>, + pub count: Value, +} +impl Clone for table { + fn clone(&self) -> Self { + Self { + rows: Rc::new(RefCell::new((*self.rows.borrow()).clone())), + count: Rc::new(RefCell::new((*self.count.borrow()).clone())), + } + } +} +impl Default for table { + fn default() -> Self { + table { + rows: Rc::new(RefCell::new( + (0..3) + .map(|_| Rc::new(RefCell::new((0..10).map(|_| 0_u8).collect::>()))) + .collect::>]>>(), + )), + count: Rc::new(RefCell::new(0_usize)), + } + } +} +impl ByteRepr for table { + fn byte_size() -> usize { + 40 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.rows.borrow()).to_bytes(&mut buf[0..30]); + (*self.count.borrow()).to_bytes(&mut buf[32..40]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + rows: Rc::new(RefCell::new(>]>>::from_bytes( + &buf[0..30], + ))), + count: Rc::new(RefCell::new(::from_bytes(&buf[32..40]))), + } + } +} +thread_local!( + pub static T1_0: Value = Rc::new(RefCell::new(table { + rows: Rc::new(RefCell::new(Box::new([ + Rc::new(RefCell::new(Box::from(*b"alpha\0\0\0\0\0"))), + Rc::new(RefCell::new(Box::new([0; 10]))), + Rc::new(RefCell::new(Box::new([0; 10]))), + ]))), + count: Rc::new(RefCell::new(1_usize)), + })); +); +thread_local!( + pub static T2_1: Value
= Rc::new(RefCell::new(table { + rows: Rc::new(RefCell::new(Box::new([ + Rc::new(RefCell::new(Box::from(*b"alpha\0\0\0\0\0"))), + Rc::new(RefCell::new(Box::from(*b"beta\0\0\0\0\0\0"))), + Rc::new(RefCell::new(Box::new([0; 10]))), + ]))), + count: Rc::new(RefCell::new(2_usize)), + })); +); +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + assert!(((((*(*T1_0.with(Value::clone).borrow()).count.borrow()) == 1_usize) as i32) != 0)); + assert!( + ((({ + let mut __it1 = ((((*T1_0.with(Value::clone).borrow()).rows.as_pointer() + as Ptr>>) + .offset(0) + .read() + .as_pointer()) as Ptr) + .to_c_string_iterator(); + let mut __it2 = Ptr::::from_string_literal(b"alpha").to_c_string_iterator(); + loop { + let __c1 = __it1.next(); + let __c2 = __it2.next(); + if __c1 != __c2 { + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); + } + if __c1.is_none() { + break 0; + } + } + } == 0) as i32) + != 0) + ); + assert!( + (((((*(*T1_0.with(Value::clone).borrow()).rows.borrow())[(1) as usize].borrow() + [(0) as usize] as i32) + == ('\0' as i32)) as i32) + != 0) + ); + assert!(((((*(*T2_1.with(Value::clone).borrow()).count.borrow()) == 2_usize) as i32) != 0)); + assert!( + ((({ + let mut __it1 = ((((*T2_1.with(Value::clone).borrow()).rows.as_pointer() + as Ptr>>) + .offset(0) + .read() + .as_pointer()) as Ptr) + .to_c_string_iterator(); + let mut __it2 = Ptr::::from_string_literal(b"alpha").to_c_string_iterator(); + loop { + let __c1 = __it1.next(); + let __c2 = __it2.next(); + if __c1 != __c2 { + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); + } + if __c1.is_none() { + break 0; + } + } + } == 0) as i32) + != 0) + ); + assert!( + ((({ + let mut __it1 = ((((*T2_1.with(Value::clone).borrow()).rows.as_pointer() + as Ptr>>) + .offset(1) + .read() + .as_pointer()) as Ptr) + .to_c_string_iterator(); + let mut __it2 = Ptr::::from_string_literal(b"beta").to_c_string_iterator(); + loop { + let __c1 = __it1.next(); + let __c2 = __it2.next(); + if __c1 != __c2 { + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); + } + if __c1.is_none() { + break 0; + } + } + } == 0) as i32) + != 0) + ); + assert!( + (((((*(*T2_1.with(Value::clone).borrow()).rows.borrow())[(2) as usize].borrow() + [(0) as usize] as i32) + == ('\0' as i32)) as i32) + != 0) + ); + let local: Value
= Rc::new(RefCell::new(table { + rows: Rc::new(RefCell::new(Box::new([ + Rc::new(RefCell::new(Box::from(*b"one\0\0\0\0\0\0\0"))), + Rc::new(RefCell::new(Box::from(*b"two\0\0\0\0\0\0\0"))), + Rc::new(RefCell::new(Box::from(*b"three\0\0\0\0\0"))), + ]))), + count: Rc::new(RefCell::new(3_usize)), + })); + assert!( + ((({ + let mut __it1 = ((((*local.borrow()).rows.as_pointer() as Ptr>>) + .offset(2) + .read() + .as_pointer()) as Ptr) + .to_c_string_iterator(); + let mut __it2 = Ptr::::from_string_literal(b"three").to_c_string_iterator(); + loop { + let __c1 = __it1.next(); + let __c2 = __it2.next(); + if __c1 != __c2 { + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); + } + if __c1.is_none() { + break 0; + } + } + } == 0) as i32) + != 0) + ); + (*(*local.borrow()).rows.borrow())[(1) as usize].borrow_mut()[(0) as usize] = + (('T' as i32) as u8); + assert!( + ((({ + let mut __it1 = ((((*local.borrow()).rows.as_pointer() as Ptr>>) + .offset(1) + .read() + .as_pointer()) as Ptr) + .to_c_string_iterator(); + let mut __it2 = Ptr::::from_string_literal(b"Two").to_c_string_iterator(); + loop { + let __c1 = __it1.next(); + let __c2 = __it2.next(); + if __c1 != __c2 { + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); + } + if __c1.is_none() { + break 0; + } + } + } == 0) as i32) + != 0) + ); + assert!( + ((({ + let mut __it1 = ((((*local.borrow()).rows.as_pointer() as Ptr>>) + .offset(0) + .read() + .as_pointer()) as Ptr) + .to_c_string_iterator(); + let mut __it2 = Ptr::::from_string_literal(b"one").to_c_string_iterator(); + loop { + let __c1 = __it1.next(); + let __c2 = __it2.next(); + if __c1 != __c2 { + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); + } + if __c1.is_none() { + break 0; + } + } + } == 0) as i32) + != 0) + ); + let p: Value> = Rc::new(RefCell::new( + ((((*local.borrow()).rows.as_pointer() as Ptr>>) + .offset(2) + .read() + .as_pointer()) as Ptr), + )); + assert!(((((((*p.borrow()).offset((0) as isize).read()) as i32) == ('t' as i32)) as i32) != 0)); + assert!(((((*(*local.borrow()).count.borrow()) == 3_usize) as i32) != 0)); + return 0; +} +pub fn __cpp2rust_init_globals() { + let _ = T1_0.with(|_| ()); + let _ = T2_1.with(|_| ()); +} diff --git a/tests/unit/out/refcount/struct_member_array_adjacency.rs b/tests/unit/out/refcount/struct_member_array_adjacency.rs new file mode 100644 index 000000000..0a35031d8 --- /dev/null +++ b/tests/unit/out/refcount/struct_member_array_adjacency.rs @@ -0,0 +1,58 @@ +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(VaArg, FnPtrArg)] +pub struct pair { + pub a: Value>, + pub b: Value>, +} +impl Clone for pair { + fn clone(&self) -> Self { + Self { + a: Rc::new(RefCell::new((*self.a.borrow()).clone())), + b: Rc::new(RefCell::new((*self.b.borrow()).clone())), + } + } +} +impl Default for pair { + fn default() -> Self { + pair { + a: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), + b: Rc::new(RefCell::new((0..4).map(|_| 0_i32).collect::>())), + } + } +} +impl ByteRepr for pair { + fn byte_size() -> usize { + 32 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.a.borrow()).to_bytes(&mut buf[0..16]); + (*self.b.borrow()).to_bytes(&mut buf[16..32]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + a: Rc::new(RefCell::new(>::from_bytes(&buf[0..16]))), + b: Rc::new(RefCell::new(>::from_bytes(&buf[16..32]))), + } + } +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let s: Value = >::default(); + assert!( + (((((*s.borrow()).a.as_pointer() as Ptr::).offset((4) as isize) + == ((*s.borrow()).b.as_pointer() as Ptr::)) as i32) + != 0) + ); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/array_field_addr.rs b/tests/unit/out/unsafe/array_field_addr.rs new file mode 100644 index 000000000..9906d7145 --- /dev/null +++ b/tests/unit/out/unsafe/array_field_addr.rs @@ -0,0 +1,68 @@ +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, VaArg)] +pub struct S { + pub before: i32, + pub mask: [u8; 4], + pub after: i32, +} +impl Default for S { + fn default() -> Self { + S { + before: 0_i32, + mask: [0_u8; 4], + after: 0_i32, + } + } +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut s: *mut S = (libcc2rs::malloc_unsafe(::std::mem::size_of::()) as *mut S); + assert!((((!((s).is_null())) as i32) != 0)); + (*s).before = 1; + { + let byte_0 = ((*s).mask.as_mut_ptr() as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::<[u8; 4]>() { + *byte_0.offset(offset as isize) = 5 as u8; + } + ((*s).mask.as_mut_ptr() as *mut ::libc::c_void) + }; + (*s).after = 2; + (*((&mut (*s).mask as *mut [u8; 4]) as *mut u8)) = 7_u8; + let mut out: [u8; 4] = [0_u8; 4]; + { + if ::std::mem::size_of::<[u8; 4]>() != 0 { + ::std::ptr::copy_nonoverlapping( + ((&mut (*s).mask as *mut [u8; 4]) as *const ::libc::c_void), + (out.as_mut_ptr() as *mut ::libc::c_void), + ::std::mem::size_of::<[u8; 4]>() as usize, + ) + } + (out.as_mut_ptr() as *mut ::libc::c_void) + }; + assert!( + (((((((out[(0) as usize] as i32) == (7)) as i32) != 0) + && ((((out[(3) as usize] as i32) == (5)) as i32) != 0)) as i32) + != 0) + ); + assert!( + ((((((((*s).before) == (1)) as i32) != 0) && (((((*s).after) == (2)) as i32) != 0)) + as i32) + != 0) + ); + libcc2rs::free_unsafe((s as *mut ::libc::c_void)); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/container_field_addr.rs b/tests/unit/out/unsafe/container_field_addr.rs new file mode 100644 index 000000000..b29cbea78 --- /dev/null +++ b/tests/unit/out/unsafe/container_field_addr.rs @@ -0,0 +1,83 @@ +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(Clone, VaArg, Default)] +pub struct S { + pub tag: i32, + pub v: Vec, + pub s: Vec, + pub m: BTreeMap>, +} +pub unsafe fn add_0(mut v: *mut Vec, mut k: i32) { + { + let a0_clone = k.clone(); + (*v).push(a0_clone) + }; +} +pub unsafe fn append_1(mut s: *mut Vec, mut t: *const libc::c_char, mut n: usize) { + (*s).splice((*s).len().saturating_sub(1)..(*s).len(), { + let mut v = ::std::slice::from_raw_parts(t, n as usize).to_vec(); + v.push(0); + v + }); +} +pub unsafe fn put_2(mut m: *mut BTreeMap>, mut k: i32, mut v: i32) { + (*(*m).entry(k).or_default().as_mut()) = v; +} +pub unsafe fn run_3(mut h: *mut S) { + (unsafe { + let _v: *mut Vec = (&mut (*h).v as *mut Vec); + let _k: i32 = (*h).tag; + add_0(_v, _k) + }); + (unsafe { + append_1( + (&mut (*h).s as *mut Vec), + c"ab".as_ptr(), + 2_usize, + ) + }); + (unsafe { + let _m: *mut BTreeMap> = (&mut (*h).m as *mut BTreeMap>); + let _k: i32 = (*h).tag; + put_2(_m, _k, 2) + }); + let mut pv: *mut Vec = (&mut (*h).v as *mut Vec); + (*pv).push(((*(pv).cast_const()).len() as i32)); + assert!( + ((((*h).v.len()) == (2_usize)) && (((&mut (*h)).v[(0_usize)]) == (7))) + && (((&mut (*h)).v[(1_usize)]) == (1)) + ); + assert!( + (*h).s == { + let s = c"ab".as_ptr(); + std::slice::from_raw_parts(s, (0..).take_while(|&i| *s.add(i) != 0).count() + 1) + .to_vec() + } + ); + assert!(((*(*h).m.entry(7).or_default().as_mut()) == (2))); + assert!((((*h).tag) == (7))); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut local: S = ::default(); + local.tag = 7; + (unsafe { run_3((&mut local as *mut S)) }); + let mut heap: *mut S = (Box::leak(Box::new(::default())) as *mut S); + (*heap).tag = 7; + (unsafe { run_3(heap) }); + ::std::mem::drop(Box::from_raw(heap)); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/field_aliasing.rs b/tests/unit/out/unsafe/field_aliasing.rs new file mode 100644 index 000000000..5fa032423 --- /dev/null +++ b/tests/unit/out/unsafe/field_aliasing.rs @@ -0,0 +1,77 @@ +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, VaArg, Default)] +pub struct In { + pub x: i32, + pub y: i32, +} +#[repr(C)] +#[derive(Copy, Clone, VaArg)] +pub struct S { + pub in_: In, + pub total: i32, + pub n: i32, + pub arr: [i32; 4], +} +impl Default for S { + fn default() -> Self { + S { + in_: ::default(), + total: 0_i32, + n: 0_i32, + arr: [0_i32; 4], + } + } +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, Default)] +pub struct Node { + pub x: i32, + pub self_: *mut Node, +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut p: *mut S = (libcc2rs::calloc_unsafe(1_usize, ::std::mem::size_of::()) as *mut S); + assert!((((!((p).is_null())) as i32) != 0)); + let mut q: *mut S = p; + (*p).in_.x = 1; + (*p).in_.y = 2; + (*p).total = (((*q).in_.x) + ((*q).in_.y)); + assert!((((((*q).total) == (3)) as i32) != 0)); + let mut ip: *mut In = (&mut (*p).in_ as *mut In); + (*ip).x = (((*p).total) + (1)); + assert!( + ((((((((*q).in_.x) == (4)) as i32) != 0) && (((((*q).in_.y) == (2)) as i32) != 0)) as i32) + != 0) + ); + (*p).arr[((*p).n) as usize] = (*p).total; + (*p).n += 1; + (*p).arr[((*p).n) as usize] = (*q).in_.x; + assert!( + (((((((((((*q).arr[(0) as usize]) == (3)) as i32) != 0) + && (((((*q).arr[(1) as usize]) == (4)) as i32) != 0)) as i32) + != 0) + && (((((*q).n) == (1)) as i32) != 0)) as i32) + != 0) + ); + libcc2rs::free_unsafe((p as *mut ::libc::c_void)); + let mut s: Node = ::default(); + s.x = 1; + s.self_ = (&mut s as *mut Node); + (*s.self_).x = ((s.x) + (1)); + assert!(((((s.x) == (2)) as i32) != 0)); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/field_method_receiver.rs b/tests/unit/out/unsafe/field_method_receiver.rs new file mode 100644 index 000000000..474824cb3 --- /dev/null +++ b/tests/unit/out/unsafe/field_method_receiver.rs @@ -0,0 +1,98 @@ +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, VaArg, Default)] +pub struct Counter { + pub n: i32, +} +impl Counter { + pub unsafe fn get(&self) -> i32 { + return self.n; + } + pub unsafe fn add(&mut self, mut k: i32) { + self.n += k; + } + pub unsafe fn self_(&mut self) -> *mut Counter { + return (self as *mut Counter); + } + pub unsafe fn take(&mut self, mut other: *mut Counter) { + self.n += (*other).n; + (*other).n = 0; + } +} +#[repr(C)] +#[derive(Clone, VaArg)] +pub struct S { + pub tag: i32, + pub c: Counter, + pub arr: [Counter; 2], + pub v: Vec, +} +impl S { + pub unsafe fn bump(&mut self) { + (unsafe { + let _k: i32 = self.tag; + Counter::add(&mut self.c, _k) + }); + } +} +impl Default for S { + fn default() -> Self { + S { + tag: 0_i32, + c: ::default(), + arr: [::default(); 2], + v: Default::default(), + } + } +} +pub unsafe fn run_0(mut o: *mut S) { + (unsafe { Counter::add(&mut (*o).c, 2) }); + assert!(((unsafe { Counter::get(&(*o).c,) }) == (2))); + (unsafe { Counter::add(&mut (*o).arr[(1) as usize], 5) }); + assert!(((unsafe { Counter::get(&(*o).arr[(1) as usize],) }) == (5))); + (unsafe { Counter::add(&mut (*(unsafe { Counter::self_(&mut (*o).c) })), 1) }); + assert!(((unsafe { Counter::get(&(*o).c,) }) == (3))); + assert!(((unsafe { Counter::self_(&mut (*o).c,) }) == (&mut (*o).c as *mut Counter))); + (unsafe { + let _other: *mut Counter = (&mut (*o).arr[(1) as usize] as *mut Counter); + Counter::take(&mut (*o).arr[(0) as usize], _other) + }); + assert!( + ((unsafe { Counter::get(&(*o).arr[(0) as usize],) }) == (5)) + && ((unsafe { Counter::get(&(*o).arr[(1) as usize],) }) == (0)) + ); + (unsafe { + let _other: *mut Counter = (&mut (*o).c as *mut Counter); + Counter::take(&mut (*o).c, _other) + }); + assert!(((unsafe { Counter::get(&(*o).c,) }) == (0))); + (unsafe { S::bump(&mut (*o)) }); + assert!(((unsafe { Counter::get(&(*o).c,) }) == (1))); + (*o).v.push((unsafe { Counter::get(&(*o).c) })); + assert!((((*o).v.len()) == (1_usize)) && (((&mut (*o)).v[(0_usize)]) == (1))); + assert!((((*o).tag) == (1))); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut local: S = ::default(); + local.tag = 1; + (unsafe { run_0((&mut local as *mut S)) }); + let mut heap: *mut S = (Box::leak(Box::new(::default())) as *mut S); + (*heap).tag = 1; + (unsafe { run_0(heap) }); + ::std::mem::drop(Box::from_raw(heap)); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/field_pointers_malloced_struct.rs b/tests/unit/out/unsafe/field_pointers_malloced_struct.rs new file mode 100644 index 000000000..aac75480d --- /dev/null +++ b/tests/unit/out/unsafe/field_pointers_malloced_struct.rs @@ -0,0 +1,61 @@ +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, VaArg, Default)] +pub struct S { + pub a: i32, + pub b: i32, + pub c: i32, +} +pub unsafe fn bump_0(mut s: *mut S) -> i32 { + (*s).b += 10; + return (*s).b; +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut s: *mut S = (libcc2rs::calloc_unsafe(1_usize, ::std::mem::size_of::()) as *mut S); + assert!(!((s).is_null())); + (*s).b = 1; + (*s).a = (unsafe { bump_0(s) }); + assert!((((*s).a) == (11))); + assert!((((*s).b) == (11))); + (*s).a = 1; + (*s).b = 2; + (*s).c = 0; + if (((*s).a) < ((*s).b)) && (((*s).c.postfix_inc()) == (0)) { + (*s).a = 5; + } + assert!((((*s).a) == (5)) && (((*s).c) == (1))); + if (((*s).a) < ((*s).b)) && (((*s).c.postfix_inc()) == (0)) { + (*s).a = 6; + } + assert!((((*s).a) == (5)) && (((*s).c) == (1))); + let mut x: i32 = (((*s).a) + + ({ + (*s).b = 3; + (*s).b + })); + assert!(((x) == (8)) && (((*s).b) == (3))); + let mut y: i32 = 0; + (*s).c = ({ + y = 99; + y + }); + assert!((((*s).c) == (99)) && ((y) == (99))); + (*s).a += (unsafe { bump_0(s) }); + assert!(((((*s).a) == (18)) && (((*s).b) == (13))) && (((*s).c) == (99))); + libcc2rs::free_unsafe((s as *mut ::libc::c_void)); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/first_field_ptr_covers_struct.rs b/tests/unit/out/unsafe/first_field_ptr_covers_struct.rs new file mode 100644 index 000000000..b42d04c5d --- /dev/null +++ b/tests/unit/out/unsafe/first_field_ptr_covers_struct.rs @@ -0,0 +1,89 @@ +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, VaArg, Default)] +pub struct In { + pub a: i16, + pub b: i16, +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, Default)] +pub struct S { + pub x: i32, + pub in_: In, + pub z: i32, +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut src: S = S { + x: 1, + in_: In { a: 2_i16, b: 3_i16 }, + z: 4, + }; + let mut p: *mut S = (libcc2rs::malloc_unsafe(::std::mem::size_of::()) as *mut S); + assert!((((!((p).is_null())) as i32) != 0)); + { + if ::std::mem::size_of::() != 0 { + ::std::ptr::copy_nonoverlapping( + ((&mut src as *mut S) as *const ::libc::c_void), + ((&mut (*p).x as *mut i32) as *mut ::libc::c_void), + ::std::mem::size_of::() as usize, + ) + } + ((&mut (*p).x as *mut i32) as *mut ::libc::c_void) + }; + assert!( + ((((((((((((((*p).x) == (1)) as i32) != 0) && (((((*p).in_.a as i32) == (2)) as i32) != 0)) + as i32) + != 0) + && (((((*p).in_.b as i32) == (3)) as i32) != 0)) as i32) + != 0) + && (((((*p).z) == (4)) as i32) != 0)) as i32) + != 0) + ); + let mut n: In = In { a: 5_i16, b: 6_i16 }; + { + if ::std::mem::size_of::() != 0 { + ::std::ptr::copy_nonoverlapping( + ((&mut n as *mut In) as *const ::libc::c_void), + ((&mut (*p).in_ as *mut In) as *mut ::libc::c_void), + ::std::mem::size_of::() as usize, + ) + } + ((&mut (*p).in_ as *mut In) as *mut ::libc::c_void) + }; + assert!( + ((((((((((((((*p).x) == (1)) as i32) != 0) && (((((*p).in_.a as i32) == (5)) as i32) != 0)) + as i32) + != 0) + && (((((*p).in_.b as i32) == (6)) as i32) != 0)) as i32) + != 0) + && (((((*p).z) == (4)) as i32) != 0)) as i32) + != 0) + ); + let mut bz: *mut u8 = ((&mut (*p).z as *mut i32) as *mut u8); + let mut i: i32 = 0; + 'loop_: while ((((i) < (4)) as i32) != 0) { + (*bz.offset((i) as isize)) = 1_u8; + i.postfix_inc(); + } + assert!( + ((((((((*p).z) == (16843009)) as i32) != 0) && (((((*p).in_.b as i32) == (6)) as i32) != 0)) + as i32) + != 0) + ); + libcc2rs::free_unsafe((p as *mut ::libc::c_void)); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/first_member_downcast.rs b/tests/unit/out/unsafe/first_member_downcast.rs new file mode 100644 index 000000000..985f9bc49 --- /dev/null +++ b/tests/unit/out/unsafe/first_member_downcast.rs @@ -0,0 +1,44 @@ +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, VaArg, Default)] +pub struct base { + pub kind: i32, +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, Default)] +pub struct derived { + pub head: base, + pub value: usize, +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut d: *mut derived = + (libcc2rs::malloc_unsafe(::std::mem::size_of::()) as *mut derived); + assert!((((!((d).is_null())) as i32) != 0)); + (*d).head.kind = 3; + (*d).value = 7_usize; + let mut b: *mut base = (&mut (*d).head as *mut base); + let mut back: *mut derived = (b as *mut derived); + assert!(((((back) == (d)) as i32) != 0)); + assert!((((((*back).value) == (7_usize)) as i32) != 0)); + assert!((((((*back).head.kind) == (3)) as i32) != 0)); + (*back).value = 8_usize; + assert!((((((*d).value) == (8_usize)) as i32) != 0)); + (*b).kind = 4; + assert!((((((*d).head.kind) == (4)) as i32) != 0)); + libcc2rs::free_unsafe((back as *mut ::libc::c_void)); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/flex_array_member_sibling_write.rs b/tests/unit/out/unsafe/flex_array_member_sibling_write.rs new file mode 100644 index 000000000..066d9d0a9 --- /dev/null +++ b/tests/unit/out/unsafe/flex_array_member_sibling_write.rs @@ -0,0 +1,109 @@ +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, VaArg)] +pub struct S { + pub n: i32, + pub name: [libc::c_char; 1], +} +impl Default for S { + fn default() -> Self { + S { + n: 0_i32, + name: [(0 as libc::c_char); 1], + } + } +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, Default)] +pub struct E { + pub id: i32, + pub w: i32, +} +#[repr(C)] +#[derive(Copy, Clone, VaArg)] +pub struct T { + pub n: i32, + pub cap: i32, + pub a: [E], +} +impl Default for T { + fn default() -> Self { + T { + n: 0_i32, + cap: 0_i32, + a: ::default(), + } + } +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut s: *mut S = (libcc2rs::calloc_unsafe( + 1_usize, + (::std::mem::size_of::() as usize).wrapping_add(8_usize), + ) as *mut S); + assert!((((!((s).is_null())) as i32) != 0)); + { + if 8_usize != 0 { + ::std::ptr::copy_nonoverlapping( + (c"abcdefg".as_ptr().cast_mut() as *const ::libc::c_void), + ((*s).name.as_mut_ptr() as *mut ::libc::c_void), + 8_usize as usize, + ) + } + ((*s).name.as_mut_ptr() as *mut ::libc::c_void) + }; + (*s).n = 5; + assert!((((((*s).n) == (5)) as i32) != 0)); + assert!( + ((((libc::strcmp( + ((*s).name.as_mut_ptr()).cast_const(), + (c"abcdefg".as_ptr().cast_mut()).cast_const() + )) == (0)) as i32) + != 0) + ); + libcc2rs::free_unsafe((s as *mut ::libc::c_void)); + let mut t: *mut T = (libcc2rs::malloc_unsafe( + (::std::mem::size_of::() as usize) + .wrapping_add(((2_usize).wrapping_mul((::std::mem::size_of::() as usize)) as usize)), + ) as *mut T); + assert!((((!((t).is_null())) as i32) != 0)); + (*t).n = 2; + (*t).cap = 2; + (*(*t).a.as_mut_ptr().add((0) as usize)).id = 10; + (*(*t).a.as_mut_ptr().add((1) as usize)).w = 20; + (*t).n = 3; + assert!( + ((((((((*(*t).a.as_mut_ptr().add((0) as usize)).id) == (10)) as i32) != 0) + && (((((*(*t).a.as_mut_ptr().add((1) as usize)).w) == (20)) as i32) != 0)) + as i32) + != 0) + ); + let mut tail: *mut E = ((&mut (*t.offset((1) as isize)) as *mut T) as *mut T as *mut E); + assert!(((((tail) == ((*t).a.as_mut_ptr())) as i32) != 0)); + (*tail.offset((1) as isize)).id = 30; + (*t).cap = 4; + assert!( + ((((((((*(*t).a.as_mut_ptr().add((1) as usize)).id) == (30)) as i32) != 0) + && (((((*(*t).a.as_mut_ptr().add((1) as usize)).w) == (20)) as i32) != 0)) + as i32) + != 0) + ); + assert!( + ((((((((*t).n) == (3)) as i32) != 0) && (((((*t).cap) == (4)) as i32) != 0)) as i32) != 0) + ); + libcc2rs::free_unsafe((t as *mut ::libc::c_void)); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/memset_struct_tail.rs b/tests/unit/out/unsafe/memset_struct_tail.rs new file mode 100644 index 000000000..991a9f816 --- /dev/null +++ b/tests/unit/out/unsafe/memset_struct_tail.rs @@ -0,0 +1,90 @@ +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, VaArg)] +pub struct S { + pub keep: i32, + pub a: i32, + pub b: i64, + pub c: [libc::c_char; 5], + pub last: i32, +} +impl Default for S { + fn default() -> Self { + S { + keep: 0_i32, + a: 0_i32, + b: 0_i64, + c: [(0 as libc::c_char); 5], + last: 0_i32, + } + } +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut p: *mut S = (libcc2rs::malloc_unsafe(::std::mem::size_of::()) as *mut S); + assert!((((!((p).is_null())) as i32) != 0)); + { + let byte_0 = (p as *mut ::libc::c_void) as *mut u8; + for offset in 0..::std::mem::size_of::() { + *byte_0.offset(offset as isize) = 255 as u8; + } + (p as *mut ::libc::c_void) + }; + (*p).keep = 7; + { + let byte_0 = ((&mut (*p).a as *mut i32) as *mut ::libc::c_void) as *mut u8; + for offset in 0..(::std::mem::size_of::() as usize) + .wrapping_sub((::std::mem::offset_of!(S, a) as usize)) + { + *byte_0.offset(offset as isize) = 0 as u8; + } + ((&mut (*p).a as *mut i32) as *mut ::libc::c_void) + }; + assert!((((((*p).keep) == (7)) as i32) != 0)); + assert!( + ((((((((((((((*p).a) == (0)) as i32) != 0) && (((((*p).b) == (0_i64)) as i32) != 0)) + as i32) + != 0) + && (((((*p).c[(4) as usize] as i32) == (0)) as i32) != 0)) as i32) + != 0) + && (((((*p).last) == (0)) as i32) != 0)) as i32) + != 0) + ); + (*p).a = 1; + (*p).b = 2_i64; + (*p).c[(0) as usize] = (('x' as i32) as libc::c_char); + (*p).last = 3; + { + let byte_0 = ((&mut (*p).b as *mut i64) as *mut ::libc::c_void) as *mut u8; + for offset in 0..(::std::mem::offset_of!(S, last) as usize) + .wrapping_sub((::std::mem::offset_of!(S, b) as usize)) + { + *byte_0.offset(offset as isize) = 0 as u8; + } + ((&mut (*p).b as *mut i64) as *mut ::libc::c_void) + }; + assert!( + ((((((((*p).keep) == (7)) as i32) != 0) && (((((*p).a) == (1)) as i32) != 0)) as i32) != 0) + ); + assert!( + ((((((((*p).b) == (0_i64)) as i32) != 0) + && (((((*p).c[(0) as usize] as i32) == (0)) as i32) != 0)) as i32) + != 0) + ); + assert!((((((*p).last) == (3)) as i32) != 0)); + libcc2rs::free_unsafe((p as *mut ::libc::c_void)); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/struct_2d_array_field.rs b/tests/unit/out/unsafe/struct_2d_array_field.rs new file mode 100644 index 000000000..398464de6 --- /dev/null +++ b/tests/unit/out/unsafe/struct_2d_array_field.rs @@ -0,0 +1,125 @@ +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, VaArg)] +pub struct table { + pub rows: [[libc::c_char; 10]; 3], + pub count: usize, +} +impl Default for table { + fn default() -> Self { + table { + rows: [[(0 as libc::c_char); 10]; 3], + count: 0_usize, + } + } +} +pub static mut T1_0: std::cell::LazyCell
= std::cell::LazyCell::new(|| unsafe { + table { + rows: [std::mem::transmute(*b"alpha\0\0\0\0\0"), [0; 10], [0; 10]], + count: 1_usize, + } +}); +pub static mut T2_1: std::cell::LazyCell
= std::cell::LazyCell::new(|| unsafe { + table { + rows: [ + std::mem::transmute(*b"alpha\0\0\0\0\0"), + std::mem::transmute(*b"beta\0\0\0\0\0\0"), + [0; 10], + ], + count: 2_usize, + } +}); +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + assert!( + (((((*std::cell::LazyCell::force_mut(&mut *&raw mut T1_0)).count) == (1_usize)) as i32) + != 0) + ); + assert!( + ((((libc::strcmp( + (*std::cell::LazyCell::force_mut(&mut *&raw mut T1_0)).rows[(0) as usize].as_ptr(), + (c"alpha".as_ptr().cast_mut()).cast_const() + )) == (0)) as i32) + != 0) + ); + assert!( + (((((*std::cell::LazyCell::force_mut(&mut *&raw mut T1_0)).rows[(1) as usize][(0) as usize] + as i32) + == ('\0' as i32)) as i32) + != 0) + ); + assert!( + (((((*std::cell::LazyCell::force_mut(&mut *&raw mut T2_1)).count) == (2_usize)) as i32) + != 0) + ); + assert!( + ((((libc::strcmp( + (*std::cell::LazyCell::force_mut(&mut *&raw mut T2_1)).rows[(0) as usize].as_ptr(), + (c"alpha".as_ptr().cast_mut()).cast_const() + )) == (0)) as i32) + != 0) + ); + assert!( + ((((libc::strcmp( + (*std::cell::LazyCell::force_mut(&mut *&raw mut T2_1)).rows[(1) as usize].as_ptr(), + (c"beta".as_ptr().cast_mut()).cast_const() + )) == (0)) as i32) + != 0) + ); + assert!( + (((((*std::cell::LazyCell::force_mut(&mut *&raw mut T2_1)).rows[(2) as usize][(0) as usize] + as i32) + == ('\0' as i32)) as i32) + != 0) + ); + let mut local: table = table { + rows: [ + std::mem::transmute(*b"one\0\0\0\0\0\0\0"), + std::mem::transmute(*b"two\0\0\0\0\0\0\0"), + std::mem::transmute(*b"three\0\0\0\0\0"), + ], + count: 3_usize, + }; + assert!( + ((((libc::strcmp( + (local.rows[(2) as usize].as_mut_ptr()).cast_const(), + (c"three".as_ptr().cast_mut()).cast_const() + )) == (0)) as i32) + != 0) + ); + local.rows[(1) as usize][(0) as usize] = (('T' as i32) as libc::c_char); + assert!( + ((((libc::strcmp( + (local.rows[(1) as usize].as_mut_ptr()).cast_const(), + (c"Two".as_ptr().cast_mut()).cast_const() + )) == (0)) as i32) + != 0) + ); + assert!( + ((((libc::strcmp( + (local.rows[(0) as usize].as_mut_ptr()).cast_const(), + (c"one".as_ptr().cast_mut()).cast_const() + )) == (0)) as i32) + != 0) + ); + let mut p: *const libc::c_char = (local.rows[(2) as usize].as_mut_ptr()).cast_const(); + assert!((((((*p.offset((0) as isize)) as i32) == ('t' as i32)) as i32) != 0)); + assert!(((((local.count) == (3_usize)) as i32) != 0)); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const T1_0); + std::cell::LazyCell::force(&*&raw const T2_1); +} diff --git a/tests/unit/out/unsafe/struct_member_array_adjacency.rs b/tests/unit/out/unsafe/struct_member_array_adjacency.rs new file mode 100644 index 000000000..6d2eb2908 --- /dev/null +++ b/tests/unit/out/unsafe/struct_member_array_adjacency.rs @@ -0,0 +1,34 @@ +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, VaArg)] +pub struct pair { + pub a: [i32; 4], + pub b: [i32; 4], +} +impl Default for pair { + fn default() -> Self { + pair { + a: [0_i32; 4], + b: [0_i32; 4], + } + } +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut s: pair = ::default(); + assert!(((((s.a.as_mut_ptr().offset((4) as isize)) == (s.b.as_mut_ptr())) as i32) != 0)); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/struct_2d_array_field.c b/tests/unit/struct_2d_array_field.c new file mode 100644 index 000000000..e5aebcf77 --- /dev/null +++ b/tests/unit/struct_2d_array_field.c @@ -0,0 +1,33 @@ +#include +#include +#include + +struct table { + char rows[3][10]; + size_t count; +}; + +static const struct table T1 = {{"alpha"}, 1}; +static const struct table T2 = {{"alpha", "beta"}, 2}; + +int main(void) { + assert(T1.count == 1); + assert(strcmp(T1.rows[0], "alpha") == 0); + assert(T1.rows[1][0] == '\0'); + + assert(T2.count == 2); + assert(strcmp(T2.rows[0], "alpha") == 0); + assert(strcmp(T2.rows[1], "beta") == 0); + assert(T2.rows[2][0] == '\0'); + + struct table local = {{"one", "two", "three"}, 3}; + assert(strcmp(local.rows[2], "three") == 0); + local.rows[1][0] = 'T'; + assert(strcmp(local.rows[1], "Two") == 0); + assert(strcmp(local.rows[0], "one") == 0); + + const char *p = local.rows[2]; + assert(p[0] == 't'); + assert(local.count == 3); + return 0; +} diff --git a/tests/unit/struct_member_array_adjacency.c b/tests/unit/struct_member_array_adjacency.c new file mode 100644 index 000000000..0be7eae9a --- /dev/null +++ b/tests/unit/struct_member_array_adjacency.c @@ -0,0 +1,13 @@ +#include + +struct pair { + int a[4]; + int b[4]; +}; + +int main(void) { + struct pair s; + + assert(s.a + 4 == s.b); + return 0; +} From 9768d8602186eb48bcd03524965295bd092c89e9 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 30 Sep 2026 12:36:57 +0100 Subject: [PATCH 2/7] Update tests --- tests/unit/container_field_addr.cpp | 1 + tests/unit/field_aliasing.c | 1 + tests/unit/field_pointers_malloced_struct.cpp | 1 + tests/unit/first_field_ptr_covers_struct.c | 1 + tests/unit/first_member_downcast.c | 1 + tests/unit/memset_struct_tail.c | 1 + .../flex_array_member_sibling_write.rs | 20 +++++++++---------- .../unsafe/flex_array_member_sibling_write.rs | 11 +++++----- tests/unit/struct_member_array_adjacency.c | 1 + 9 files changed, 22 insertions(+), 16 deletions(-) diff --git a/tests/unit/container_field_addr.cpp b/tests/unit/container_field_addr.cpp index a5d26fc13..1de76fb75 100644 --- a/tests/unit/container_field_addr.cpp +++ b/tests/unit/container_field_addr.cpp @@ -1,3 +1,4 @@ +// panic: refcount #include #include #include diff --git a/tests/unit/field_aliasing.c b/tests/unit/field_aliasing.c index d9cedb187..2c885598f 100644 --- a/tests/unit/field_aliasing.c +++ b/tests/unit/field_aliasing.c @@ -1,3 +1,4 @@ +// panic: refcount #include #include diff --git a/tests/unit/field_pointers_malloced_struct.cpp b/tests/unit/field_pointers_malloced_struct.cpp index f042aa41a..76af7825d 100644 --- a/tests/unit/field_pointers_malloced_struct.cpp +++ b/tests/unit/field_pointers_malloced_struct.cpp @@ -1,3 +1,4 @@ +// panic: refcount #include #include diff --git a/tests/unit/first_field_ptr_covers_struct.c b/tests/unit/first_field_ptr_covers_struct.c index f06cd2419..0eaff4dbb 100644 --- a/tests/unit/first_field_ptr_covers_struct.c +++ b/tests/unit/first_field_ptr_covers_struct.c @@ -1,3 +1,4 @@ +// panic: refcount #include #include #include diff --git a/tests/unit/first_member_downcast.c b/tests/unit/first_member_downcast.c index 4e10ce475..04a9432af 100644 --- a/tests/unit/first_member_downcast.c +++ b/tests/unit/first_member_downcast.c @@ -1,3 +1,4 @@ +// panic: refcount #include #include diff --git a/tests/unit/memset_struct_tail.c b/tests/unit/memset_struct_tail.c index cb3a64914..326d24f78 100644 --- a/tests/unit/memset_struct_tail.c +++ b/tests/unit/memset_struct_tail.c @@ -1,3 +1,4 @@ +// panic: refcount #include #include #include diff --git a/tests/unit/out/refcount/flex_array_member_sibling_write.rs b/tests/unit/out/refcount/flex_array_member_sibling_write.rs index d98e6b685..5289edb28 100644 --- a/tests/unit/out/refcount/flex_array_member_sibling_write.rs +++ b/tests/unit/out/refcount/flex_array_member_sibling_write.rs @@ -90,24 +90,26 @@ impl Default for T { T { n: Rc::new(RefCell::new(0_i32)), cap: Rc::new(RefCell::new(0_i32)), - a: Rc::new(RefCell::new(>::default())), + a: Rc::new(RefCell::new( + (0..1).map(|_| ::default()).collect::>(), + )), } } } impl ByteRepr for T { fn byte_size() -> usize { - 8 + 16 } fn to_bytes(&self, buf: &mut [u8]) { (*self.n.borrow()).to_bytes(&mut buf[0..4]); (*self.cap.borrow()).to_bytes(&mut buf[4..8]); - (*self.a.borrow()).to_bytes(&mut buf[8..8]); + (*self.a.borrow()).to_bytes(&mut buf[8..16]); } fn from_bytes(buf: &[u8]) -> Self { Self { n: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), cap: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - a: Rc::new(RefCell::new(>::from_bytes(&buf[8..8]))), + a: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), } } } @@ -152,10 +154,8 @@ fn main_0() -> i32 { ); libcc2rs::free_refcount((*s.borrow()).to_any()); let t: Value> = Rc::new(RefCell::new( - libcc2rs::malloc_refcount( - (8usize as usize).wrapping_add(((2_usize).wrapping_mul((8usize as usize)) as usize)), - ) - .reinterpret_cast::(), + libcc2rs::malloc_refcount((16usize as usize).wrapping_add((8usize as usize))) + .reinterpret_cast::(), )); assert!((((!((*t.borrow()).is_null())) as i32) != 0)); (*(*(*t.borrow()).upgrade().deref()).n.borrow_mut()) = 2; @@ -186,11 +186,11 @@ fn main_0() -> i32 { assert!( ((({ let _lhs = (*tail.borrow()).clone(); - _lhs == ((*(*t.borrow()).upgrade().deref()).a.as_pointer() as Ptr) + _lhs == (((*(*t.borrow()).upgrade().deref()).a.as_pointer() as Ptr).offset(1)) }) as i32) != 0) ); - (*(*(*tail.borrow()).offset((1) as isize).upgrade().deref()) + (*(*(*tail.borrow()).offset((0) as isize).upgrade().deref()) .id .borrow_mut()) = 30; (*(*(*t.borrow()).upgrade().deref()).cap.borrow_mut()) = 4; diff --git a/tests/unit/out/unsafe/flex_array_member_sibling_write.rs b/tests/unit/out/unsafe/flex_array_member_sibling_write.rs index 066d9d0a9..d7c387ac8 100644 --- a/tests/unit/out/unsafe/flex_array_member_sibling_write.rs +++ b/tests/unit/out/unsafe/flex_array_member_sibling_write.rs @@ -31,14 +31,14 @@ pub struct E { pub struct T { pub n: i32, pub cap: i32, - pub a: [E], + pub a: [E; 1], } impl Default for T { fn default() -> Self { T { n: 0_i32, cap: 0_i32, - a: ::default(), + a: [::default(); 1], } } } @@ -75,8 +75,7 @@ unsafe fn main_0() -> i32 { ); libcc2rs::free_unsafe((s as *mut ::libc::c_void)); let mut t: *mut T = (libcc2rs::malloc_unsafe( - (::std::mem::size_of::() as usize) - .wrapping_add(((2_usize).wrapping_mul((::std::mem::size_of::() as usize)) as usize)), + (::std::mem::size_of::() as usize).wrapping_add((::std::mem::size_of::() as usize)), ) as *mut T); assert!((((!((t).is_null())) as i32) != 0)); (*t).n = 2; @@ -91,8 +90,8 @@ unsafe fn main_0() -> i32 { != 0) ); let mut tail: *mut E = ((&mut (*t.offset((1) as isize)) as *mut T) as *mut T as *mut E); - assert!(((((tail) == ((*t).a.as_mut_ptr())) as i32) != 0)); - (*tail.offset((1) as isize)).id = 30; + assert!(((((tail) == ((*t).a.as_mut_ptr().add((1) as usize))) as i32) != 0)); + (*tail.offset((0) as isize)).id = 30; (*t).cap = 4; assert!( ((((((((*(*t).a.as_mut_ptr().add((1) as usize)).id) == (30)) as i32) != 0) diff --git a/tests/unit/struct_member_array_adjacency.c b/tests/unit/struct_member_array_adjacency.c index 0be7eae9a..4206f0e8a 100644 --- a/tests/unit/struct_member_array_adjacency.c +++ b/tests/unit/struct_member_array_adjacency.c @@ -1,3 +1,4 @@ +// panic: refcount #include struct pair { From 4d0a16f9db2a02a04a4c7448e420394f23fdd3cb Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 30 Sep 2026 12:39:30 +0100 Subject: [PATCH 3/7] Add ByteRepr for Box<[T]> --- libcc2rs/src/reinterpret.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/libcc2rs/src/reinterpret.rs b/libcc2rs/src/reinterpret.rs index 76516534c..ed25c6d70 100644 --- a/libcc2rs/src/reinterpret.rs +++ b/libcc2rs/src/reinterpret.rs @@ -92,7 +92,20 @@ impl ByteRepr for Vec {} impl ByteRepr for Option {} impl ByteRepr for std::rc::Rc {} impl ByteRepr for std::cell::RefCell {} -impl ByteRepr for Box<[T]> {} +impl ByteRepr for Box<[T]> { + fn to_bytes(&self, buf: &mut [u8]) { + let elem_size = T::byte_size(); + for (i, elem) in self.iter().enumerate() { + elem.to_bytes(&mut buf[i * elem_size..(i + 1) * elem_size]); + } + } + + fn from_bytes(buf: &[u8]) -> Self { + let elem_size = T::byte_size(); + assert_eq!(buf.len() % elem_size, 0, "ub: partial array element"); + buf.chunks(elem_size).map(T::from_bytes).collect() + } +} impl ByteRepr for Box {} impl ByteRepr for *const T {} impl ByteRepr for *mut T {} From 8cb7c1b32151e8eceeef52283102f07d987990cb Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 30 Sep 2026 12:40:58 +0100 Subject: [PATCH 4/7] Update expectations --- tests/unit/union_addrof_external.c | 1 - tests/unit/union_memset_memcpy.c | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/unit/union_addrof_external.c b/tests/unit/union_addrof_external.c index 9682f4670..64adaf262 100644 --- a/tests/unit/union_addrof_external.c +++ b/tests/unit/union_addrof_external.c @@ -1,4 +1,3 @@ -// panic: refcount #include #include #include diff --git a/tests/unit/union_memset_memcpy.c b/tests/unit/union_memset_memcpy.c index 175a1f47e..4b9c78771 100644 --- a/tests/unit/union_memset_memcpy.c +++ b/tests/unit/union_memset_memcpy.c @@ -1,4 +1,3 @@ -// panic: refcount #include #include #include From a4f7a0a3078b885c11054d2bb132c62000120e1d Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 30 Sep 2026 12:42:03 +0100 Subject: [PATCH 5/7] Update expectations --- tests/unit/array_field_addr.c | 1 + tests/unit/flex_array_member_sibling_write.c | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/unit/array_field_addr.c b/tests/unit/array_field_addr.c index e02202c51..0802f6900 100644 --- a/tests/unit/array_field_addr.c +++ b/tests/unit/array_field_addr.c @@ -1,3 +1,4 @@ +// no-compile: refcount #include #include #include diff --git a/tests/unit/flex_array_member_sibling_write.c b/tests/unit/flex_array_member_sibling_write.c index 7245cba65..d90d935e4 100644 --- a/tests/unit/flex_array_member_sibling_write.c +++ b/tests/unit/flex_array_member_sibling_write.c @@ -1,3 +1,4 @@ +// panic: refcount #include #include #include From 2f51f9235371c58926d1a5c39652dbfc3c54ab2e Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 30 Sep 2026 14:10:54 +0100 Subject: [PATCH 6/7] Don't implement ByteRepr for non-trivially copyable types --- cpp2rust/converter/converter_lib.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 55b42f6b3..cca5f065c 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -286,6 +286,9 @@ bool TypeImplementsByteRepr(clang::QualType qt) { if (rd->isUnion()) { return true; } + if (!qt.isTriviallyCopyableType(rd->getASTContext())) { + return false; + } for (const auto *field : rd->fields()) { if (!TypeImplementsByteRepr(field->getType())) { return false; From c2b9f29d66caeba6c06ef920ee7869a65709bb03 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 30 Sep 2026 14:15:39 +0100 Subject: [PATCH 7/7] Update tests --- .../out/refcount/defaulted_move_cross_tu.rs | 16 +- .../refcount/out_of_line_methods_cross_tu.rs | 28 +--- .../out/refcount/array_of_noncopy_struct.rs | 16 +- tests/unit/out/refcount/class_templates.rs | 42 +---- tests/unit/out/refcount/constructor.rs | 14 +- .../unit/out/refcount/container_field_addr.rs | 22 +-- tests/unit/out/refcount/copy_assign.rs | 60 +------ tests/unit/out/refcount/copy_ctor.rs | 58 +------ .../unit/out/refcount/copy_move_defaulted.rs | 106 +------------ tests/unit/out/refcount/copy_move_deleted.rs | 44 +----- tests/unit/out/refcount/destructor.rs | 148 ++---------------- .../out/refcount/field_method_receiver.rs | 20 +-- .../refcount/foreach_disjoint_field_borrow.rs | 16 +- tests/unit/out/refcount/forward_basic.rs | 32 +--- tests/unit/out/refcount/forward_decltype.rs | 18 +-- tests/unit/out/refcount/forward_explicit.rs | 18 +-- tests/unit/out/refcount/forward_pack.rs | 34 +--- tests/unit/out/refcount/huffman.rs | 26 +-- tests/unit/out/refcount/implicit_autoref.rs | 14 +- tests/unit/out/refcount/kruskal.rs | 42 +---- .../refcount/libc_struct_without_default.rs | 16 +- tests/unit/out/refcount/move_assign.rs | 28 +--- tests/unit/out/refcount/move_ctor.rs | 64 +------- tests/unit/out/refcount/move_this.rs | 14 +- tests/unit/out/refcount/push_emplace_back.rs | 18 +-- tests/unit/out/refcount/rule_of_five.rs | 16 +- tests/unit/out/refcount/rule_of_three.rs | 16 +- .../unit/out/refcount/typedef-anon-struct.rs | 14 +- tests/unit/out/refcount/unique_ptr.rs | 14 +- .../out/refcount/unique_ptr_const_deref.rs | 14 +- tests/unit/out/refcount/unique_ptr_nested.rs | 14 +- tests/unit/out/refcount/void_cast.rs | 28 +--- 32 files changed, 65 insertions(+), 965 deletions(-) diff --git a/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs index 9c2529519..63cbe7b22 100644 --- a/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs +++ b/tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs @@ -45,21 +45,7 @@ impl Default for S { } } } -impl ByteRepr for S { - fn byte_size() -> usize { - 32 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..24]); - (*self.n.borrow()).to_bytes(&mut buf[24..32]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - n: Rc::new(RefCell::new(>::from_bytes(&buf[24..32]))), - } - } -} +impl ByteRepr for S {} pub fn sum_0(s: Ptr) -> i32 { return { let _lhs = { diff --git a/tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs b/tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs index 450e9dadb..8672fdd6d 100644 --- a/tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs +++ b/tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs @@ -20,19 +20,7 @@ impl Clone for S { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for S { - fn byte_size() -> usize { - 4 - } - 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]))), - } - } -} +impl ByteRepr for S {} pub trait Base { fn apply(&self, x: i32) -> i32; } @@ -50,19 +38,7 @@ impl Clone for Derived { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for Derived { - fn byte_size() -> usize { - 16 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.factor.borrow()).to_bytes(&mut buf[8..12]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - factor: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), - } - } -} +impl ByteRepr for Derived {} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); diff --git a/tests/unit/out/refcount/array_of_noncopy_struct.rs b/tests/unit/out/refcount/array_of_noncopy_struct.rs index 8a4780593..e7c63faad 100644 --- a/tests/unit/out/refcount/array_of_noncopy_struct.rs +++ b/tests/unit/out/refcount/array_of_noncopy_struct.rs @@ -29,21 +29,7 @@ impl Default for NonCopy { } } } -impl ByteRepr for NonCopy { - fn byte_size() -> usize { - 32 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.data.borrow()).to_bytes(&mut buf[0..24]); - (*self.tag.borrow()).to_bytes(&mut buf[24..28]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - data: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - tag: Rc::new(RefCell::new(::from_bytes(&buf[24..28]))), - } - } -} +impl ByteRepr for NonCopy {} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); diff --git a/tests/unit/out/refcount/class_templates.rs b/tests/unit/out/refcount/class_templates.rs index 938cc7f26..638c159d6 100644 --- a/tests/unit/out/refcount/class_templates.rs +++ b/tests/unit/out/refcount/class_templates.rs @@ -19,19 +19,7 @@ impl Clone for MyContainer_int_ { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for MyContainer_int_ { - fn byte_size() -> usize { - 24 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.vec_.borrow()).to_bytes(&mut buf[0..24]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - vec_: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - } - } -} +impl ByteRepr for MyContainer_int_ {} #[derive(VaArg, FnPtrArg, Default)] pub struct MyContainer_char_ { vec_: Value>, @@ -45,19 +33,7 @@ impl Clone for MyContainer_char_ { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for MyContainer_char_ { - fn byte_size() -> usize { - 24 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.vec_.borrow()).to_bytes(&mut buf[0..24]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - vec_: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - } - } -} +impl ByteRepr for MyContainer_char_ {} #[derive(VaArg, FnPtrArg, Default)] pub struct MyContainer_float_ { vec_: Value>, @@ -71,19 +47,7 @@ impl Clone for MyContainer_float_ { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for MyContainer_float_ { - fn byte_size() -> usize { - 24 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.vec_.borrow()).to_bytes(&mut buf[0..24]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - vec_: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - } - } -} +impl ByteRepr for MyContainer_float_ {} #[derive(VaArg, FnPtrArg, Default)] pub struct Boxed_int_ { pub value: Value, diff --git a/tests/unit/out/refcount/constructor.rs b/tests/unit/out/refcount/constructor.rs index c56a9a5ef..048831344 100644 --- a/tests/unit/out/refcount/constructor.rs +++ b/tests/unit/out/refcount/constructor.rs @@ -34,19 +34,7 @@ impl Clone for S { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for S { - fn byte_size() -> usize { - 4 - } - 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]))), - } - } -} +impl ByteRepr for S {} #[derive(VaArg, FnPtrArg)] pub struct Point { pub x: Value, diff --git a/tests/unit/out/refcount/container_field_addr.rs b/tests/unit/out/refcount/container_field_addr.rs index be527e0f3..8c036c545 100644 --- a/tests/unit/out/refcount/container_field_addr.rs +++ b/tests/unit/out/refcount/container_field_addr.rs @@ -30,27 +30,7 @@ impl Clone for S { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for S { - fn byte_size() -> usize { - 112 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.tag.borrow()).to_bytes(&mut buf[0..4]); - (*self.v.borrow()).to_bytes(&mut buf[8..32]); - (*self.s.borrow()).to_bytes(&mut buf[32..64]); - (*self.m.borrow()).to_bytes(&mut buf[64..112]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - tag: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - v: Rc::new(RefCell::new(>::from_bytes(&buf[8..32]))), - s: Rc::new(RefCell::new(>::from_bytes(&buf[32..64]))), - m: Rc::new(RefCell::new(>>::from_bytes( - &buf[64..112], - ))), - } - } -} +impl ByteRepr for S {} pub fn add_0(v: Ptr>, k: i32) { let v: Value>> = Rc::new(RefCell::new(v)); let k: Value = Rc::new(RefCell::new(k)); diff --git a/tests/unit/out/refcount/copy_assign.rs b/tests/unit/out/refcount/copy_assign.rs index 870deeb47..61d707eee 100644 --- a/tests/unit/out/refcount/copy_assign.rs +++ b/tests/unit/out/refcount/copy_assign.rs @@ -43,21 +43,7 @@ impl Clone for Partial { Partial::copy_from(__src.as_pointer()) } } -impl ByteRepr for Partial { - fn byte_size() -> usize { - 8 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); - (*self.keep.borrow()).to_bytes(&mut buf[4..8]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - keep: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - } - } -} +impl ByteRepr for Partial {} #[derive(VaArg, FnPtrArg)] pub struct NonConstAssign { pub mark: Value, @@ -85,19 +71,7 @@ impl Default for NonConstAssign { { NonConstAssign::new() } } } -impl ByteRepr for NonConstAssign { - fn byte_size() -> usize { - 4 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.mark.borrow()).to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - mark: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - } - } -} +impl ByteRepr for NonConstAssign {} #[derive(VaArg, FnPtrArg)] pub struct RefQualified { pub mark: Value, @@ -125,19 +99,7 @@ impl Default for RefQualified { { RefQualified::new() } } } -impl ByteRepr for RefQualified { - fn byte_size() -> usize { - 4 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.mark.borrow()).to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - mark: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - } - } -} +impl ByteRepr for RefQualified {} #[derive(VaArg, FnPtrArg)] pub struct Holder { pub p: Value, @@ -169,21 +131,7 @@ impl Default for Holder { } } } -impl ByteRepr for Holder { - fn byte_size() -> usize { - 24 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.p.borrow()).to_bytes(&mut buf[0..8]); - (*self.arr.borrow()).to_bytes(&mut buf[8..24]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - p: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), - arr: Rc::new(RefCell::new(>::from_bytes(&buf[8..24]))), - } - } -} +impl ByteRepr for Holder {} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); diff --git a/tests/unit/out/refcount/copy_ctor.rs b/tests/unit/out/refcount/copy_ctor.rs index bfb3d5244..e9a3b8fdd 100644 --- a/tests/unit/out/refcount/copy_ctor.rs +++ b/tests/unit/out/refcount/copy_ctor.rs @@ -37,19 +37,7 @@ impl Clone for Counted { Counted::copy_from(__src.as_pointer()) } } -impl ByteRepr for Counted { - fn byte_size() -> usize { - 4 - } - 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]))), - } - } -} +impl ByteRepr for Counted {} #[derive(VaArg, FnPtrArg)] pub struct NonConst { pub mark: Value, @@ -90,19 +78,7 @@ impl Default for NonConst { { NonConst::new() } } } -impl ByteRepr for NonConst { - fn byte_size() -> usize { - 4 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.mark.borrow()).to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - mark: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - } - } -} +impl ByteRepr for NonConst {} #[derive(VaArg, FnPtrArg, Default)] pub struct Ignored { pub v: Value, @@ -131,19 +107,7 @@ impl Clone for Ignored { Ignored::copy_from(__src.as_pointer()) } } -impl ByteRepr for Ignored { - fn byte_size() -> usize { - 4 - } - 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]))), - } - } -} +impl ByteRepr for Ignored {} #[derive(VaArg, FnPtrArg)] pub struct Holder { pub c: Value, @@ -175,21 +139,7 @@ impl Default for Holder { } } } -impl ByteRepr for Holder { - fn byte_size() -> usize { - 12 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.c.borrow()).to_bytes(&mut buf[0..4]); - (*self.arr.borrow()).to_bytes(&mut buf[4..12]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - c: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - arr: Rc::new(RefCell::new(>::from_bytes(&buf[4..12]))), - } - } -} +impl ByteRepr for Holder {} pub fn by_value_1(c: Counted) -> i32 { let c: Value = Rc::new(RefCell::new(c)); return (*(*c.borrow()).v.borrow()); diff --git a/tests/unit/out/refcount/copy_move_defaulted.rs b/tests/unit/out/refcount/copy_move_defaulted.rs index 6e2eec491..35401d4f4 100644 --- a/tests/unit/out/refcount/copy_move_defaulted.rs +++ b/tests/unit/out/refcount/copy_move_defaulted.rs @@ -74,23 +74,7 @@ impl Default for Explicit { } } } -impl ByteRepr for Explicit { - fn byte_size() -> usize { - 16 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); - (*self.inner.borrow()).to_bytes(&mut buf[4..8]); - (*self.arr.borrow()).to_bytes(&mut buf[8..16]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - inner: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - arr: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), - } - } -} +impl ByteRepr for Explicit {} #[derive(VaArg, FnPtrArg)] pub struct Implicit { pub v: Value, @@ -167,19 +151,7 @@ impl Clone for DefaultCopyUserMove { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for DefaultCopyUserMove { - fn byte_size() -> usize { - 4 - } - 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]))), - } - } -} +impl ByteRepr for DefaultCopyUserMove {} #[derive(VaArg, FnPtrArg, Default)] pub struct UserCopyDefaultMove { pub v: Value, @@ -215,19 +187,7 @@ impl Clone for UserCopyDefaultMove { UserCopyDefaultMove::copy_from(__src.as_pointer()) } } -impl ByteRepr for UserCopyDefaultMove { - fn byte_size() -> usize { - 4 - } - 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]))), - } - } -} +impl ByteRepr for UserCopyDefaultMove {} #[derive()] pub struct Buffer { pub data: Value>, @@ -284,27 +244,7 @@ impl Default for Buffer { } } } -impl ByteRepr for Buffer { - fn byte_size() -> usize { - 64 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.data.borrow()).to_bytes(&mut buf[0..24]); - (*self.rows.borrow()).to_bytes(&mut buf[24..48]); - (*self.n.borrow()).to_bytes(&mut buf[48..52]); - (*self.arr.borrow()).to_bytes(&mut buf[52..60]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - data: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - rows: Rc::new(RefCell::new(>>>::from_bytes( - &buf[24..48], - ))), - n: Rc::new(RefCell::new(::from_bytes(&buf[48..52]))), - arr: Rc::new(RefCell::new(>::from_bytes(&buf[52..60]))), - } - } -} +impl ByteRepr for Buffer {} #[derive()] pub struct Owner { pub data: Value>, @@ -340,25 +280,7 @@ impl Default for Owner { } } } -impl ByteRepr for Owner { - fn byte_size() -> usize { - 48 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.data.borrow()).to_bytes(&mut buf[0..24]); - (*self.n.borrow()).to_bytes(&mut buf[24..28]); - (*self.arr.borrow()).to_bytes(&mut buf[28..36]); - (*self.p.borrow()).to_bytes(&mut buf[40..48]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - data: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - n: Rc::new(RefCell::new(::from_bytes(&buf[24..28]))), - arr: Rc::new(RefCell::new(>::from_bytes(&buf[28..36]))), - p: Rc::new(RefCell::new(>>::from_bytes(&buf[40..48]))), - } - } -} +impl ByteRepr for Owner {} #[derive(Default)] pub struct Holder { pub inner: Value, @@ -392,23 +314,7 @@ impl Holder { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for Holder { - fn byte_size() -> usize { - 32 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.inner.borrow()).to_bytes(&mut buf[0..4]); - (*self.e.borrow()).to_bytes(&mut buf[4..20]); - (*self.p.borrow()).to_bytes(&mut buf[24..32]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - inner: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - e: Rc::new(RefCell::new(::from_bytes(&buf[4..20]))), - p: Rc::new(RefCell::new(>>::from_bytes(&buf[24..32]))), - } - } -} +impl ByteRepr for Holder {} pub fn same_0(a: Ptr, b: Ptr) -> bool { return ((({ let _lhs = (*(*a.upgrade().deref()).v.borrow()); diff --git a/tests/unit/out/refcount/copy_move_deleted.rs b/tests/unit/out/refcount/copy_move_deleted.rs index fa7506687..b6b74a224 100644 --- a/tests/unit/out/refcount/copy_move_deleted.rs +++ b/tests/unit/out/refcount/copy_move_deleted.rs @@ -28,19 +28,7 @@ impl NoCopy { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for NoCopy { - fn byte_size() -> usize { - 4 - } - 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]))), - } - } -} +impl ByteRepr for NoCopy {} #[derive()] pub struct PrivateCopy { pub v: Value, @@ -67,19 +55,7 @@ impl Default for PrivateCopy { { PrivateCopy::new() } } } -impl ByteRepr for PrivateCopy { - fn byte_size() -> usize { - 4 - } - 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]))), - } - } -} +impl ByteRepr for PrivateCopy {} #[derive()] pub struct Immovable { pub v: Value, @@ -128,21 +104,7 @@ impl Container { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for Container { - fn byte_size() -> usize { - 8 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.inner.borrow()).to_bytes(&mut buf[0..4]); - (*self.tag.borrow()).to_bytes(&mut buf[4..8]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - inner: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - tag: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - } - } -} +impl ByteRepr for Container {} pub fn bump_0(p: Ptr) { let p: Value> = Rc::new(RefCell::new(p)); (*(*(*p.borrow()).upgrade().deref()).v.borrow_mut()).postfix_inc(); diff --git a/tests/unit/out/refcount/destructor.rs b/tests/unit/out/refcount/destructor.rs index 0f9ac673f..8933ec066 100644 --- a/tests/unit/out/refcount/destructor.rs +++ b/tests/unit/out/refcount/destructor.rs @@ -24,19 +24,7 @@ impl Clone for Defaulted { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for Defaulted { - fn byte_size() -> usize { - 1 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.s.borrow()).to_bytes(&mut buf[0..1]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - s: Rc::new(RefCell::new(::from_bytes(&buf[0..1]))), - } - } -} +impl ByteRepr for Defaulted {} #[derive(VaArg, FnPtrArg, Default)] pub struct Middle { pub s: Value, @@ -50,19 +38,7 @@ impl Clone for Middle { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for Middle { - fn byte_size() -> usize { - 1 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.s.borrow()).to_bytes(&mut buf[0..1]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - s: Rc::new(RefCell::new(::from_bytes(&buf[0..1]))), - } - } -} +impl ByteRepr for Middle {} #[derive(VaArg, FnPtrArg, Default)] pub struct Outer { pub m: Value, @@ -76,19 +52,7 @@ impl Clone for Outer { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for Outer { - fn byte_size() -> usize { - 1 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.m.borrow()).to_bytes(&mut buf[0..1]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - m: Rc::new(RefCell::new(::from_bytes(&buf[0..1]))), - } - } -} +impl ByteRepr for Outer {} #[derive(VaArg, FnPtrArg)] pub struct ArrayMember { pub items: Value>, @@ -113,19 +77,7 @@ impl Default for ArrayMember { } } } -impl ByteRepr for ArrayMember { - fn byte_size() -> usize { - 3 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.items.borrow()).to_bytes(&mut buf[0..3]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - items: Rc::new(RefCell::new(>::from_bytes(&buf[0..3]))), - } - } -} +impl ByteRepr for ArrayMember {} #[derive(VaArg, FnPtrArg, Default)] pub struct EmptyBody { pub s: Value, @@ -139,19 +91,7 @@ impl Clone for EmptyBody { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for EmptyBody { - fn byte_size() -> usize { - 1 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.s.borrow()).to_bytes(&mut buf[0..1]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - s: Rc::new(RefCell::new(::from_bytes(&buf[0..1]))), - } - } -} +impl ByteRepr for EmptyBody {} #[derive(VaArg, FnPtrArg, Default)] pub struct Templated_char_ { pub v: Value, @@ -165,19 +105,7 @@ impl Clone for Templated_char_ { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for Templated_char_ { - fn byte_size() -> usize { - 1 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..1]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..1]))), - } - } -} +impl ByteRepr for Templated_char_ {} #[derive(VaArg, FnPtrArg, Default)] pub struct Templated_int_ { pub v: Value, @@ -191,19 +119,7 @@ impl Clone for Templated_int_ { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for Templated_int_ { - fn byte_size() -> usize { - 4 - } - 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]))), - } - } -} +impl ByteRepr for Templated_int_ {} #[derive(VaArg, FnPtrArg, Default)] pub struct Copied { pub v: Value, @@ -217,19 +133,7 @@ impl Clone for Copied { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for Copied { - fn byte_size() -> usize { - 4 - } - 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]))), - } - } -} +impl ByteRepr for Copied {} thread_local!( pub static order_1: Value> = Rc::new(RefCell::new((0..3).map(|_| 0_i32).collect::>())); @@ -250,19 +154,7 @@ impl Clone for Tagged { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for Tagged { - fn byte_size() -> usize { - 4 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.tag.borrow()).to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - tag: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - } - } -} +impl ByteRepr for Tagged {} #[derive(VaArg, FnPtrArg, Default)] pub struct Ordered { pub first: Value, @@ -284,27 +176,7 @@ impl Clone for Ordered { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for Ordered { - fn byte_size() -> usize { - 20 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.first.borrow()).to_bytes(&mut buf[0..4]); - (*self.dummy1.borrow()).to_bytes(&mut buf[4..8]); - (*self.second.borrow()).to_bytes(&mut buf[8..12]); - (*self.dummy2.borrow()).to_bytes(&mut buf[12..16]); - (*self.third.borrow()).to_bytes(&mut buf[16..20]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - first: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - dummy1: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - second: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), - dummy2: Rc::new(RefCell::new(::from_bytes(&buf[12..16]))), - third: Rc::new(RefCell::new(::from_bytes(&buf[16..20]))), - } - } -} +impl ByteRepr for Ordered {} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); diff --git a/tests/unit/out/refcount/field_method_receiver.rs b/tests/unit/out/refcount/field_method_receiver.rs index f03717be2..145c59634 100644 --- a/tests/unit/out/refcount/field_method_receiver.rs +++ b/tests/unit/out/refcount/field_method_receiver.rs @@ -67,25 +67,7 @@ impl Default for S { } } } -impl ByteRepr for S { - fn byte_size() -> usize { - 40 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.tag.borrow()).to_bytes(&mut buf[0..4]); - (*self.c.borrow()).to_bytes(&mut buf[4..8]); - (*self.arr.borrow()).to_bytes(&mut buf[8..16]); - (*self.v.borrow()).to_bytes(&mut buf[16..40]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - tag: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - c: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - arr: Rc::new(RefCell::new(>::from_bytes(&buf[8..16]))), - v: Rc::new(RefCell::new(>::from_bytes(&buf[16..40]))), - } - } -} +impl ByteRepr for S {} pub fn run_0(o: Ptr) { let o: Value> = Rc::new(RefCell::new(o)); ({ CounterImpl::add(&(*(*o.borrow()).upgrade().deref()).c.as_pointer(), 2) }); diff --git a/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs b/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs index 21cea5349..2d28a7024 100644 --- a/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs +++ b/tests/unit/out/refcount/foreach_disjoint_field_borrow.rs @@ -21,21 +21,7 @@ impl Clone for S { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for S { - fn byte_size() -> usize { - 32 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..24]); - (*self.a.borrow()).to_bytes(&mut buf[24..28]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - a: Rc::new(RefCell::new(::from_bytes(&buf[24..28]))), - } - } -} +impl ByteRepr for S {} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); diff --git a/tests/unit/out/refcount/forward_basic.rs b/tests/unit/out/refcount/forward_basic.rs index a12cf9b57..a46927926 100644 --- a/tests/unit/out/refcount/forward_basic.rs +++ b/tests/unit/out/refcount/forward_basic.rs @@ -58,23 +58,7 @@ impl Clone for Tracked { Tracked::copy_from(__src.as_pointer()) } } -impl ByteRepr for Tracked { - fn byte_size() -> usize { - 12 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); - (*self.copies.borrow()).to_bytes(&mut buf[4..8]); - (*self.moves.borrow()).to_bytes(&mut buf[8..12]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - copies: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - moves: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), - } - } -} +impl ByteRepr for Tracked {} pub fn chosen_overload_0(_a0: Ptr) -> Overload { return Overload_kLvalueOverload; } @@ -112,19 +96,7 @@ impl Clone for Holder { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for Holder { - fn byte_size() -> usize { - 12 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.t.borrow()).to_bytes(&mut buf[0..12]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - t: Rc::new(RefCell::new(::from_bytes(&buf[0..12]))), - } - } -} +impl ByteRepr for Holder {} pub fn forward_once_2(x: Ptr) -> Overload { return ({ chosen_overload_0((x).clone()) }); } diff --git a/tests/unit/out/refcount/forward_decltype.rs b/tests/unit/out/refcount/forward_decltype.rs index 2f842846a..91fd805b8 100644 --- a/tests/unit/out/refcount/forward_decltype.rs +++ b/tests/unit/out/refcount/forward_decltype.rs @@ -58,23 +58,7 @@ impl Clone for Tracked { Tracked::copy_from(__src.as_pointer()) } } -impl ByteRepr for Tracked { - fn byte_size() -> usize { - 12 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); - (*self.copies.borrow()).to_bytes(&mut buf[4..8]); - (*self.moves.borrow()).to_bytes(&mut buf[8..12]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - copies: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - moves: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), - } - } -} +impl ByteRepr for Tracked {} pub fn chosen_overload_0(_a0: Ptr) -> Overload { return Overload_kLvalueOverload; } diff --git a/tests/unit/out/refcount/forward_explicit.rs b/tests/unit/out/refcount/forward_explicit.rs index 986760d87..047b4c155 100644 --- a/tests/unit/out/refcount/forward_explicit.rs +++ b/tests/unit/out/refcount/forward_explicit.rs @@ -60,23 +60,7 @@ impl Clone for Tracked { Tracked::copy_from(__src.as_pointer()) } } -impl ByteRepr for Tracked { - fn byte_size() -> usize { - 12 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); - (*self.copies.borrow()).to_bytes(&mut buf[4..8]); - (*self.moves.borrow()).to_bytes(&mut buf[8..12]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - copies: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - moves: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), - } - } -} +impl ByteRepr for Tracked {} pub fn chosen_overload_0(_a0: Ptr) -> Overload { return Overload_kLvalueOverload; } diff --git a/tests/unit/out/refcount/forward_pack.rs b/tests/unit/out/refcount/forward_pack.rs index cae97e6b4..6f7ba65dd 100644 --- a/tests/unit/out/refcount/forward_pack.rs +++ b/tests/unit/out/refcount/forward_pack.rs @@ -60,23 +60,7 @@ impl Clone for Tracked { Tracked::copy_from(__src.as_pointer()) } } -impl ByteRepr for Tracked { - fn byte_size() -> usize { - 12 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); - (*self.copies.borrow()).to_bytes(&mut buf[4..8]); - (*self.moves.borrow()).to_bytes(&mut buf[8..12]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - copies: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - moves: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), - } - } -} +impl ByteRepr for Tracked {} pub fn chosen_overload_0(_a0: Ptr) -> Overload { return Overload_kLvalueOverload; } @@ -172,21 +156,7 @@ impl Clone for Pair { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for Pair { - fn byte_size() -> usize { - 24 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..12]); - (*self.b.borrow()).to_bytes(&mut buf[12..24]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..12]))), - b: Rc::new(RefCell::new(::from_bytes(&buf[12..24]))), - } - } -} +impl ByteRepr for Pair {} pub fn forward_pack_into_ctor_8(args_0: Ptr, args_1: Ptr) -> Pair { return Pair::new({ (args_0).clone() }, { (args_1).clone() }); } diff --git a/tests/unit/out/refcount/huffman.rs b/tests/unit/out/refcount/huffman.rs index ce85f61a3..3d534bb9e 100644 --- a/tests/unit/out/refcount/huffman.rs +++ b/tests/unit/out/refcount/huffman.rs @@ -99,31 +99,7 @@ impl MinHeap { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for MinHeap { - fn byte_size() -> usize { - 32 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.size.borrow()).to_bytes(&mut buf[0..4]); - (*self.capacity.borrow()).to_bytes(&mut buf[4..8]); - (*self.arr.borrow()).to_bytes(&mut buf[8..16]); - (*self.next.borrow()).to_bytes(&mut buf[16..20]); - (*self.alloc.borrow()).to_bytes(&mut buf[24..32]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - size: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - capacity: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - arr: Rc::new(RefCell::new( - ]>>>>::from_bytes(&buf[8..16]), - )), - next: Rc::new(RefCell::new(::from_bytes(&buf[16..20]))), - alloc: Rc::new(RefCell::new( - >>>::from_bytes(&buf[24..32]), - )), - } - } -} +impl ByteRepr for MinHeap {} pub fn AllocMinHeap_1(capacity: i32) -> Option> { let capacity: Value = Rc::new(RefCell::new(capacity)); let minHeap: Value>> = diff --git a/tests/unit/out/refcount/implicit_autoref.rs b/tests/unit/out/refcount/implicit_autoref.rs index 7ffa52b62..fedfc640f 100644 --- a/tests/unit/out/refcount/implicit_autoref.rs +++ b/tests/unit/out/refcount/implicit_autoref.rs @@ -19,19 +19,7 @@ impl Clone for Holder { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for Holder { - fn byte_size() -> usize { - 24 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..24]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - } - } -} +impl ByteRepr for Holder {} pub fn write_through_0(p: Ptr) { let p: Value> = Rc::new(RefCell::new(p)); (*p.borrow()).write(42); diff --git a/tests/unit/out/refcount/kruskal.rs b/tests/unit/out/refcount/kruskal.rs index 091b42c11..10a25232c 100644 --- a/tests/unit/out/refcount/kruskal.rs +++ b/tests/unit/out/refcount/kruskal.rs @@ -236,27 +236,7 @@ impl DisjointSet { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for DisjointSet { - fn byte_size() -> usize { - 24 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.rank.borrow()).to_bytes(&mut buf[0..8]); - (*self.parent.borrow()).to_bytes(&mut buf[8..16]); - (*self.n.borrow()).to_bytes(&mut buf[16..20]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - rank: Rc::new(RefCell::new(>>>::from_bytes( - &buf[0..8], - ))), - parent: Rc::new(RefCell::new(>>>::from_bytes( - &buf[8..16], - ))), - n: Rc::new(RefCell::new(::from_bytes(&buf[16..20]))), - } - } -} +impl ByteRepr for DisjointSet {} #[derive(Default)] pub struct Graph { pub edges: Value>>>, @@ -276,25 +256,7 @@ impl Graph { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for Graph { - fn byte_size() -> usize { - 16 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.edges.borrow()).to_bytes(&mut buf[0..8]); - (*self.V.borrow()).to_bytes(&mut buf[8..12]); - (*self.E.borrow()).to_bytes(&mut buf[12..16]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - edges: Rc::new(RefCell::new(>>>::from_bytes( - &buf[0..8], - ))), - V: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), - E: Rc::new(RefCell::new(::from_bytes(&buf[12..16]))), - } - } -} +impl ByteRepr for Graph {} pub fn MSTKruskal_2(graph: Ptr) -> f64 { ({ let _arr: Ptr>>> = (*graph.upgrade().deref()).edges.as_pointer(); diff --git a/tests/unit/out/refcount/libc_struct_without_default.rs b/tests/unit/out/refcount/libc_struct_without_default.rs index 5b974ddb8..402758c24 100644 --- a/tests/unit/out/refcount/libc_struct_without_default.rs +++ b/tests/unit/out/refcount/libc_struct_without_default.rs @@ -31,21 +31,7 @@ impl Default for UserDefined { } } } -impl ByteRepr for UserDefined { - fn byte_size() -> usize { - 32 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..4]); - (*self.v.borrow()).to_bytes(&mut buf[8..32]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - a: Rc::new(RefCell::new(>::from_bytes(&buf[0..4]))), - v: Rc::new(RefCell::new(>::from_bytes(&buf[8..32]))), - } - } -} +impl ByteRepr for UserDefined {} #[derive(VaArg, FnPtrArg)] pub struct FieldIsLibcType { pub addr: Value, diff --git a/tests/unit/out/refcount/move_assign.rs b/tests/unit/out/refcount/move_assign.rs index 11b440151..aec0b65c2 100644 --- a/tests/unit/out/refcount/move_assign.rs +++ b/tests/unit/out/refcount/move_assign.rs @@ -28,19 +28,7 @@ impl MoveOnly { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for MoveOnly { - fn byte_size() -> usize { - 4 - } - 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]))), - } - } -} +impl ByteRepr for MoveOnly {} #[derive()] pub struct ConstMoveAssign { pub mark: Value, @@ -59,19 +47,7 @@ impl Default for ConstMoveAssign { { ConstMoveAssign::new() } } } -impl ByteRepr for ConstMoveAssign { - fn byte_size() -> usize { - 4 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.mark.borrow()).to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - mark: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - } - } -} +impl ByteRepr for ConstMoveAssign {} pub fn make_0(v: i32) -> MoveOnly { let v: Value = Rc::new(RefCell::new(v)); let m: Value = Rc::new(RefCell::new(MoveOnly::new({ (*v.borrow()) }))); diff --git a/tests/unit/out/refcount/move_ctor.rs b/tests/unit/out/refcount/move_ctor.rs index 1a9d3c78e..4f54df578 100644 --- a/tests/unit/out/refcount/move_ctor.rs +++ b/tests/unit/out/refcount/move_ctor.rs @@ -28,19 +28,7 @@ impl MoveOnly { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for MoveOnly { - fn byte_size() -> usize { - 4 - } - 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]))), - } - } -} +impl ByteRepr for MoveOnly {} #[derive()] pub struct ConstMove { pub mark: Value, @@ -73,19 +61,7 @@ impl Default for ConstMove { { ConstMove::new() } } } -impl ByteRepr for ConstMove { - fn byte_size() -> usize { - 4 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.mark.borrow()).to_bytes(&mut buf[0..4]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - mark: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - } - } -} +impl ByteRepr for ConstMove {} #[derive(VaArg, FnPtrArg, Default)] pub struct ThrowingMove { pub v: Value, @@ -135,23 +111,7 @@ impl Clone for ThrowingMove { ThrowingMove::copy_from(__src.as_pointer()) } } -impl ByteRepr for ThrowingMove { - fn byte_size() -> usize { - 12 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); - (*self.copies.borrow()).to_bytes(&mut buf[4..8]); - (*self.moves.borrow()).to_bytes(&mut buf[8..12]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - copies: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - moves: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), - } - } -} +impl ByteRepr for ThrowingMove {} #[derive(VaArg, FnPtrArg, Default)] pub struct NoexceptMove { pub v: Value, @@ -201,23 +161,7 @@ impl Clone for NoexceptMove { NoexceptMove::copy_from(__src.as_pointer()) } } -impl ByteRepr for NoexceptMove { - fn byte_size() -> usize { - 12 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.v.borrow()).to_bytes(&mut buf[0..4]); - (*self.copies.borrow()).to_bytes(&mut buf[4..8]); - (*self.moves.borrow()).to_bytes(&mut buf[8..12]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - v: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - copies: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - moves: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), - } - } -} +impl ByteRepr for NoexceptMove {} pub fn by_value_0(m: MoveOnly) -> i32 { let m: Value = Rc::new(RefCell::new(m)); return (*(*m.borrow()).v.borrow()); diff --git a/tests/unit/out/refcount/move_this.rs b/tests/unit/out/refcount/move_this.rs index caaebd814..997c96ac1 100644 --- a/tests/unit/out/refcount/move_this.rs +++ b/tests/unit/out/refcount/move_this.rs @@ -41,19 +41,7 @@ impl Clone for Chain { Chain::copy_from(__src.as_pointer()) } } -impl ByteRepr for Chain { - fn byte_size() -> usize { - 4 - } - 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]))), - } - } -} +impl ByteRepr for Chain {} pub fn consume_0(c: Chain) -> i32 { let c: Value = Rc::new(RefCell::new(c)); return (*(*c.borrow()).v.borrow()); diff --git a/tests/unit/out/refcount/push_emplace_back.rs b/tests/unit/out/refcount/push_emplace_back.rs index 1869e9650..6c09b8051 100644 --- a/tests/unit/out/refcount/push_emplace_back.rs +++ b/tests/unit/out/refcount/push_emplace_back.rs @@ -87,23 +87,7 @@ impl Clone for JPEGData { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for JPEGData { - fn byte_size() -> usize { - 48 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.com_data.borrow()).to_bytes(&mut buf[0..24]); - (*self.app_data.borrow()).to_bytes(&mut buf[24..48]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - com_data: Rc::new(RefCell::new(>>>::from_bytes(&buf[0..24]))), - app_data: Rc::new(RefCell::new(>>>::from_bytes( - &buf[24..48], - ))), - } - } -} +impl ByteRepr for JPEGData {} pub fn push_param_0(dest: Ptr>>>) { let dest: Value>>>> = Rc::new(RefCell::new(dest)); ((*dest.borrow()).clone() as Ptr>>>) diff --git a/tests/unit/out/refcount/rule_of_five.rs b/tests/unit/out/refcount/rule_of_five.rs index 9cc02017c..2bddf88f9 100644 --- a/tests/unit/out/refcount/rule_of_five.rs +++ b/tests/unit/out/refcount/rule_of_five.rs @@ -93,21 +93,7 @@ impl Default for Buffer { } } } -impl ByteRepr for Buffer { - fn byte_size() -> usize { - 20 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.data.borrow()).to_bytes(&mut buf[0..16]); - (*self.size.borrow()).to_bytes(&mut buf[16..20]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - data: Rc::new(RefCell::new(>::from_bytes(&buf[0..16]))), - size: Rc::new(RefCell::new(::from_bytes(&buf[16..20]))), - } - } -} +impl ByteRepr for Buffer {} pub fn make_3(size: i32) -> Buffer { let size: Value = Rc::new(RefCell::new(size)); let b: Value = Rc::new(RefCell::new(Buffer::new({ (*size.borrow()) }))); diff --git a/tests/unit/out/refcount/rule_of_three.rs b/tests/unit/out/refcount/rule_of_three.rs index 90c903ec3..f65582ef0 100644 --- a/tests/unit/out/refcount/rule_of_three.rs +++ b/tests/unit/out/refcount/rule_of_three.rs @@ -72,21 +72,7 @@ impl Default for Buffer { } } } -impl ByteRepr for Buffer { - fn byte_size() -> usize { - 20 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.data.borrow()).to_bytes(&mut buf[0..16]); - (*self.size.borrow()).to_bytes(&mut buf[16..20]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - data: Rc::new(RefCell::new(>::from_bytes(&buf[0..16]))), - size: Rc::new(RefCell::new(::from_bytes(&buf[16..20]))), - } - } -} +impl ByteRepr for Buffer {} pub fn sum_2(b: Ptr) -> i32 { let s: Value = Rc::new(RefCell::new(0)); let i: Value = Rc::new(RefCell::new(0)); diff --git a/tests/unit/out/refcount/typedef-anon-struct.rs b/tests/unit/out/refcount/typedef-anon-struct.rs index 7b54b5ad3..8b579910b 100644 --- a/tests/unit/out/refcount/typedef-anon-struct.rs +++ b/tests/unit/out/refcount/typedef-anon-struct.rs @@ -49,19 +49,7 @@ impl Clone for Outer { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for Outer { - fn byte_size() -> usize { - 24 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.runs.borrow()).to_bytes(&mut buf[0..24]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - runs: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - } - } -} +impl ByteRepr for Outer {} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); diff --git a/tests/unit/out/refcount/unique_ptr.rs b/tests/unit/out/refcount/unique_ptr.rs index 408cf2b9d..34e348324 100644 --- a/tests/unit/out/refcount/unique_ptr.rs +++ b/tests/unit/out/refcount/unique_ptr.rs @@ -21,19 +21,7 @@ impl SafePointer { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for SafePointer { - fn byte_size() -> usize { - 8 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.ptr.borrow()).to_bytes(&mut buf[0..8]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - ptr: Rc::new(RefCell::new(>>::from_bytes(&buf[0..8]))), - } - } -} +impl ByteRepr for SafePointer {} #[derive(VaArg, FnPtrArg, Default)] pub struct Pair { pub x: Value, diff --git a/tests/unit/out/refcount/unique_ptr_const_deref.rs b/tests/unit/out/refcount/unique_ptr_const_deref.rs index bd6783531..e4b70edfd 100644 --- a/tests/unit/out/refcount/unique_ptr_const_deref.rs +++ b/tests/unit/out/refcount/unique_ptr_const_deref.rs @@ -21,19 +21,7 @@ impl Holder { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for Holder { - fn byte_size() -> usize { - 8 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.val.borrow()).to_bytes(&mut buf[0..8]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - val: Rc::new(RefCell::new(>>::from_bytes(&buf[0..8]))), - } - } -} +impl ByteRepr for Holder {} pub fn read_val_0(h: Ptr) -> i32 { let h: Value> = Rc::new(RefCell::new(h)); return (*(*(*(*h.borrow()).upgrade().deref()).val.borrow()) diff --git a/tests/unit/out/refcount/unique_ptr_nested.rs b/tests/unit/out/refcount/unique_ptr_nested.rs index 194e275d1..e90b0333d 100644 --- a/tests/unit/out/refcount/unique_ptr_nested.rs +++ b/tests/unit/out/refcount/unique_ptr_nested.rs @@ -51,19 +51,7 @@ impl Outer { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for Outer { - fn byte_size() -> usize { - 8 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.inner.borrow()).to_bytes(&mut buf[0..8]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - inner: Rc::new(RefCell::new(>>::from_bytes(&buf[0..8]))), - } - } -} +impl ByteRepr for Outer {} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); diff --git a/tests/unit/out/refcount/void_cast.rs b/tests/unit/out/refcount/void_cast.rs index ab18f034f..a7fff2c36 100644 --- a/tests/unit/out/refcount/void_cast.rs +++ b/tests/unit/out/refcount/void_cast.rs @@ -23,19 +23,7 @@ impl Clone for NonTrivial { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for NonTrivial { - fn byte_size() -> usize { - 24 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.data.borrow()).to_bytes(&mut buf[0..24]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - data: Rc::new(RefCell::new(>::from_bytes(&buf[0..24]))), - } - } -} +impl ByteRepr for NonTrivial {} pub fn unused_ref_param_1(x: Ptr) { &(*x.upgrade().deref()); } @@ -91,19 +79,7 @@ impl NonCopyable { Rc::try_unwrap(__this).ok().unwrap().into_inner() } } -impl ByteRepr for NonCopyable { - fn byte_size() -> usize { - 8 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.value.borrow()).to_bytes(&mut buf[0..8]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - value: Rc::new(RefCell::new(>>::from_bytes(&buf[0..8]))), - } - } -} +impl ByteRepr for NonCopyable {} pub fn unused_noncopyable_param_5(x: Ptr) { &(*x.upgrade().deref()); }