diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 43e380f1..6332cdba 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -200,6 +200,43 @@ bool IsMut(clang::QualType qual_type) { qual_type->getPointeeType().isConstQualified()); } +bool TypeImplementsByteRepr(clang::QualType qt) { + if (qt->isIntegerType() || qt->isFloatingType() || qt->isEnumeralType()) { + return true; + } + if (const auto *arr = qt->getAsArrayTypeUnsafe()) { + return TypeImplementsByteRepr(arr->getElementType()); + } + if (const auto *rd = qt->getAsRecordDecl()) { + if (rd->getASTContext().getSourceManager().isInSystemHeader( + rd->getLocation())) { + return false; + } + if (rd->isUnion()) { + return false; + } + for (const auto *field : rd->fields()) { + if (!TypeImplementsByteRepr(field->getType())) { + return false; + } + } + return true; + } + return false; +} + +bool RustSizeDivergesFromC(clang::QualType qt) { + qt = qt.getCanonicalType(); + // Records have Rc> fields that diverge from the C size + if (qt->isRecordType()) { + return true; + } + if (auto *arr = qt->getAsArrayTypeUnsafe()) { + return RustSizeDivergesFromC(arr->getElementType()); + } + return false; +} + bool IsMutatingCall(const clang::CallExpr *expr) { if (auto *callee = expr->getDirectCallee()) { if (auto *method = clang::dyn_cast(callee)) { diff --git a/cpp2rust/converter/converter_lib.h b/cpp2rust/converter/converter_lib.h index 25c84210..d652735e 100644 --- a/cpp2rust/converter/converter_lib.h +++ b/cpp2rust/converter/converter_lib.h @@ -52,6 +52,10 @@ bool IsUnsignedArithOp(const clang::BinaryOperator *expr); bool IsMut(clang::QualType qual_type); +bool TypeImplementsByteRepr(clang::QualType qt); + +bool RustSizeDivergesFromC(clang::QualType qt); + bool IsMutatingCall(const clang::CallExpr *expr); bool IsOverloadedFunction(const clang::FunctionDecl *decl); diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index eb89d496..fa484685 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -545,28 +545,10 @@ void ConverterRefCount::AddDropTrait(const clang::CXXRecordDecl *decl) { StrCat('}'); } -static bool recordImplementsByteRepr(const clang::RecordDecl *decl) { - if (decl->isUnion()) { - return false; - } - - // ByteRepr is only supported for user-defined structs that contain ByteRepr - // fields. - for (auto *f : decl->fields()) { - auto qt = f->getType(); - if (!qt->isIntegerType() && !qt->isFloatingType() && - !qt->isEnumeralType()) { - return false; - } - } - - return true; -} - void ConverterRefCount::AddByteReprTrait(const clang::RecordDecl *decl) { auto struct_name = GetRecordName(decl); - if (!recordImplementsByteRepr(decl)) { + if (!TypeImplementsByteRepr(ctx_.getCanonicalTagType(decl))) { StrCat(std::format("impl ByteRepr for {}", struct_name)); PushBrace brace(*this); return; @@ -577,6 +559,9 @@ void ConverterRefCount::AddByteReprTrait(const clang::RecordDecl *decl) { const auto &layout = ctx_.getASTRecordLayout(decl); + StrCat(std::format("fn byte_size() -> usize {{ {} }}", + ctx_.getTypeSize(ctx_.getCanonicalTagType(decl)) / 8)); + StrCat("fn to_bytes(&self, buf: &mut [u8])"); { PushBrace fn_brace(*this); @@ -600,9 +585,12 @@ void ConverterRefCount::AddByteReprTrait(const clang::RecordDecl *decl) { for (auto *field : decl->fields()) { auto byte_off = layout.getFieldOffset(idx) / 8; auto byte_size = ctx_.getTypeSize(field->getType()) / 8; + PushConversionKind push(*this, ConversionKind::FullRefCount); + std::string storage_ty = ToString(field->getType()); + Unwrap(storage_ty, "Value<", ">"); StrCat(std::format( "{}: Rc::new(RefCell::new(<{}>::from_bytes(&buf[{}..{}]))),", - GetNamedDeclAsString(field), Mapper::Map(field->getType()), byte_off, + GetNamedDeclAsString(field), storage_ty, byte_off, byte_off + byte_size)); ++idx; } @@ -1322,6 +1310,20 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) { } } +bool ConverterRefCount::VisitUnaryExprOrTypeTraitExpr( + clang::UnaryExprOrTypeTraitExpr *expr) { + if (expr->getKind() == clang::UnaryExprOrTypeTrait::UETT_SizeOf) { + auto arg_type = expr->isArgumentType() ? expr->getArgumentType() + : expr->getArgumentExpr()->getType(); + if (RustSizeDivergesFromC(arg_type)) { + StrCat(std::format("{}usize", ctx_.getTypeSize(arg_type) / 8)); + computed_expr_type_ = ComputedExprType::FreshValue; + return false; + } + } + return Converter::VisitUnaryExprOrTypeTraitExpr(expr); +} + bool ConverterRefCount::VisitStmtExpr(clang::StmtExpr *expr) { PushConversionKind push(*this, ConversionKind::FullRefCount); return Converter::VisitStmtExpr(expr); diff --git a/cpp2rust/converter/models/converter_refcount.h b/cpp2rust/converter/models/converter_refcount.h index 1a3634a3..7737a244 100644 --- a/cpp2rust/converter/models/converter_refcount.h +++ b/cpp2rust/converter/models/converter_refcount.h @@ -43,6 +43,9 @@ class ConverterRefCount final : public Converter { void AddByteReprTrait(const clang::EnumDecl *decl) override; + bool + VisitUnaryExprOrTypeTraitExpr(clang::UnaryExprOrTypeTraitExpr *expr) override; + void AddDefaultTrait(const clang::RecordDecl *decl) override; void AddDefaultTraitForUnion(const clang::RecordDecl *decl) override; diff --git a/libcc2rs/src/rc.rs b/libcc2rs/src/rc.rs index ccf2f991..ac09c80c 100644 --- a/libcc2rs/src/rc.rs +++ b/libcc2rs/src/rc.rs @@ -16,6 +16,13 @@ use crate::reinterpret::{ByteRepr, OriginalAlloc, SingleOriginalAlloc, SliceOrig pub type Value = Rc>; +struct ReinterpretedView { + // Pointer to the source of reinterpret + alloc: Rc, + // C++ size of the reinterpreted view + elem_byte_size: usize, +} + #[derive(Default)] enum PtrKind { #[default] @@ -25,7 +32,7 @@ enum PtrKind { HeapSingle(Weak>), HeapArray(Weak>>), Vec(Weak>>), - Reinterpreted(Rc), + Reinterpreted(Rc), } pub enum StrongPtr { @@ -59,7 +66,7 @@ impl StrongPtr { cell, } => { // Read-through: always re-read from the original allocation. - let mut buf = vec![0u8; std::mem::size_of::()]; + let mut buf = vec![0u8; T::byte_size()]; alloc.read_bytes(*byte_offset, &mut buf); *cell.borrow_mut() = Some(T::from_bytes(&buf)); Ref::map(cell.borrow(), |opt| opt.as_ref().unwrap()) @@ -78,7 +85,7 @@ impl fmt::Debug for PtrKind { PtrKind::StackArray(w) => write!(f, "StackArray({:?})", w.as_ptr()), PtrKind::HeapArray(w) => write!(f, "HeapArray({:?})", w.as_ptr()), PtrKind::Reinterpreted(data) => { - write!(f, "Reinterpreted(0x{:x})", data.address()) + write!(f, "Reinterpreted(0x{:x})", data.alloc.address()) } } } @@ -105,7 +112,7 @@ impl PtrKind { PtrKind::StackSingle(w) | PtrKind::HeapSingle(w) => w.as_ptr() as usize, PtrKind::Vec(w) => w.as_ptr() as usize, PtrKind::StackArray(w) | PtrKind::HeapArray(w) => w.as_ptr() as usize, - PtrKind::Reinterpreted(data) => data.address(), + PtrKind::Reinterpreted(data) => data.alloc.address(), } } } @@ -251,40 +258,40 @@ impl Ptr { #[inline] fn elem_step(&self) -> usize { match &self.kind { - PtrKind::Reinterpreted(_) => std::mem::size_of::(), + PtrKind::Reinterpreted(data) => data.elem_byte_size, _ => 1, } } #[inline] pub fn len(&self) -> usize { - match self.kind { + match &self.kind { PtrKind::Null => 0, PtrKind::StackSingle(_) | PtrKind::HeapSingle(_) => 1, - PtrKind::Vec(ref weak) => weak.upgrade().expect("ub: dangling pointer").borrow().len(), - PtrKind::StackArray(ref weak) | PtrKind::HeapArray(ref weak) => { + PtrKind::Vec(weak) => weak.upgrade().expect("ub: dangling pointer").borrow().len(), + PtrKind::StackArray(weak) | PtrKind::HeapArray(weak) => { weak.upgrade().expect("ub: dangling pointer").borrow().len() } - PtrKind::Reinterpreted(ref data) => data.total_byte_len() / std::mem::size_of::(), + PtrKind::Reinterpreted(data) => data.alloc.total_byte_len() / data.elem_byte_size, } } #[inline] pub fn is_empty(&self) -> bool { - match self.kind { + match &self.kind { PtrKind::Null => true, PtrKind::StackSingle(_) | PtrKind::HeapSingle(_) => false, - PtrKind::Vec(ref weak) => weak + PtrKind::Vec(weak) => weak .upgrade() .expect("ub: dangling pointer") .borrow() .is_empty(), - PtrKind::StackArray(ref weak) | PtrKind::HeapArray(ref weak) => weak + PtrKind::StackArray(weak) | PtrKind::HeapArray(weak) => weak .upgrade() .expect("ub: dangling pointer") .borrow() .is_empty(), - PtrKind::Reinterpreted(ref data) => self.offset >= data.total_byte_len(), + PtrKind::Reinterpreted(data) => self.offset >= data.alloc.total_byte_len(), } } @@ -340,7 +347,7 @@ impl Ptr { offset: self.offset, }, PtrKind::Reinterpreted(data) => StrongPtr::Reinterpreted { - alloc: Rc::clone(data), + alloc: Rc::clone(&data.alloc), byte_offset: self.offset, cell: RefCell::new(None), }, @@ -366,9 +373,9 @@ impl Ptr { rc.borrow_mut()[self.offset] = value; } PtrKind::Reinterpreted(data) => { - let mut buf = vec![0u8; std::mem::size_of::()]; + let mut buf = vec![0u8; T::byte_size()]; value.to_bytes(&mut buf); - data.write_bytes(self.offset, &buf); + data.alloc.write_bytes(self.offset, &buf); } } } @@ -391,30 +398,34 @@ impl Ptr { return self_any.downcast_ref::>().unwrap().clone(); } - if std::mem::size_of::() == 0 { + if U::byte_size() == 0 { panic!("cannot reinterpret_cast to zero-sized type"); } + let src_byte_off = self.offset.wrapping_mul(T::byte_size()); let (alloc, abs_byte_off): (Rc, usize) = match &self.kind { PtrKind::Null => return Ptr::null(), PtrKind::StackSingle(weak) | PtrKind::HeapSingle(weak) => ( Rc::new(SingleOriginalAlloc { weak: weak.clone() }), - self.byte_offset(), + src_byte_off, ), PtrKind::Vec(weak) => ( Rc::new(SliceOriginalAlloc { weak: weak.clone() }), - self.byte_offset(), + src_byte_off, ), PtrKind::StackArray(weak) | PtrKind::HeapArray(weak) => ( Rc::new(SliceOriginalAlloc { weak: weak.clone() }), - self.byte_offset(), + src_byte_off, ), - PtrKind::Reinterpreted(data) => (Rc::clone(data), self.offset), + PtrKind::Reinterpreted(data) => (Rc::clone(&data.alloc), self.offset), }; Ptr { offset: abs_byte_off, - kind: PtrKind::Reinterpreted(alloc), + kind: PtrKind::Reinterpreted(Rc::new(ReinterpretedView { + alloc, + elem_byte_size: U::byte_size(), + })), } } } @@ -442,12 +453,12 @@ impl Ptr { f(&mut borrow[self.offset]) } PtrKind::Reinterpreted(data) => { - let mut buf = vec![0u8; std::mem::size_of::()]; - data.read_bytes(self.offset, &mut buf); + let mut buf = vec![0u8; T::byte_size()]; + data.alloc.read_bytes(self.offset, &mut buf); let mut val = T::from_bytes(&buf); let ret = f(&mut val); val.to_bytes(&mut buf); - data.write_bytes(self.offset, &buf); + data.alloc.write_bytes(self.offset, &buf); ret } } @@ -475,8 +486,8 @@ impl Ptr { f(&borrow[self.offset]) } PtrKind::Reinterpreted(data) => { - let mut buf = vec![0u8; std::mem::size_of::()]; - data.read_bytes(self.offset, &mut buf); + let mut buf = vec![0u8; T::byte_size()]; + data.alloc.read_bytes(self.offset, &mut buf); let val = T::from_bytes(&buf); f(&val) } @@ -502,8 +513,8 @@ impl Ptr { weak.upgrade().expect("ub: dangling pointer").borrow()[self.offset].clone() } PtrKind::Reinterpreted(ref data) => { - let mut buf = vec![0u8; std::mem::size_of::()]; - data.read_bytes(self.offset, &mut buf); + let mut buf = vec![0u8; T::byte_size()]; + data.alloc.read_bytes(self.offset, &mut buf); T::from_bytes(&buf) } } @@ -535,7 +546,7 @@ impl Ptr { let strong = weak.upgrade().expect("ub: dangling pointer"); (*strong.borrow_mut())[self.get_offset()..last].sort(); } - PtrKind::Reinterpreted(..) => { + PtrKind::Reinterpreted(_) => { panic!("sorting not supported for reinterpreted pointers") } } @@ -580,7 +591,7 @@ impl Ptr { let mut borrow = strong.borrow_mut(); sort(&mut borrow, self.get_offset(), last, &mut cmp); } - PtrKind::Reinterpreted(..) => { + PtrKind::Reinterpreted(_) => { panic!("sorting not supported for reinterpreted pointers") } } @@ -856,7 +867,7 @@ impl ToOwnedOption for Ptr { } PtrKind::Vec(_) => panic!("Can't own a vector"), PtrKind::HeapArray(_) => panic!("Can't own an array variable as single"), - PtrKind::Reinterpreted(..) => panic!("Can't own a reinterpreted pointer"), + PtrKind::Reinterpreted(_) => panic!("Can't own a reinterpreted pointer"), } } } @@ -882,7 +893,7 @@ impl ToOwnedOption> for Ptr { } PtrKind::Vec(_) => panic!("Can't own a vector"), PtrKind::HeapSingle(_) => panic!("Can't own a single variable as an array"), - PtrKind::Reinterpreted(..) => panic!("Can't own a reinterpreted pointer"), + PtrKind::Reinterpreted(_) => panic!("Can't own a reinterpreted pointer"), } } } @@ -898,7 +909,7 @@ impl fmt::Debug for Ptr { (Weak::as_ptr(w) as usize).wrapping_add(self.byte_offset()) } PtrKind::Vec(w) => (Weak::as_ptr(w) as usize).wrapping_add(self.byte_offset()), - PtrKind::Reinterpreted(data) => data.address().wrapping_add(self.byte_offset()), + PtrKind::Reinterpreted(data) => data.alloc.address().wrapping_add(self.byte_offset()), }; write!(f, "0x{:x}", addr) } @@ -996,7 +1007,7 @@ impl Ptr { } PtrKind::Reinterpreted(ref data) => { let mut buf = vec![0u8; end.wrapping_sub(start)]; - data.read_bytes(start, &mut buf); + data.alloc.read_bytes(start, &mut buf); buf } } diff --git a/libcc2rs/src/reinterpret.rs b/libcc2rs/src/reinterpret.rs index 452b35de..b496f8b2 100644 --- a/libcc2rs/src/reinterpret.rs +++ b/libcc2rs/src/reinterpret.rs @@ -4,20 +4,39 @@ use std::{cell::RefCell, rc::Weak}; pub trait ByteRepr: 'static { + fn byte_size() -> usize + where + Self: Sized, + { + panic!( + "byte_size is not implemented for {}", + std::any::type_name::() + ) + } fn to_bytes(&self, _buf: &mut [u8]) { - panic!("ByteRepr not supported for this type"); + panic!( + "to_bytes is not implemented for {}", + std::any::type_name::() + ) } fn from_bytes(_buf: &[u8]) -> Self where Self: Sized, { - panic!("ByteRepr not supported for this type"); + panic!( + "from_bytes is not implemented for {}", + std::any::type_name::() + ) } } macro_rules! impl_byte_repr { ($ty:ty) => { impl ByteRepr for $ty { + #[inline] + fn byte_size() -> usize { + std::mem::size_of::<$ty>() + } #[inline] fn to_bytes(&self, buf: &mut [u8]) { buf.copy_from_slice(&self.to_ne_bytes()); @@ -46,6 +65,10 @@ impl_byte_repr!(f32); impl_byte_repr!(f64); impl ByteRepr for bool { + #[inline] + fn byte_size() -> usize { + 1 + } #[inline] fn to_bytes(&self, buf: &mut [u8]) { buf[0] = *self as u8; @@ -83,7 +106,7 @@ pub trait OriginalAlloc { // Only serializes the overlapping elements, not the whole slice. fn slice_read_bytes(slice: &[S], byte_offset: usize, buf: &mut [u8]) { let len = buf.len(); - let elem_size = std::mem::size_of::(); + let elem_size = S::byte_size(); let first_elem = byte_offset / elem_size; let last_elem = (byte_offset + len).div_ceil(elem_size); let tmp_len = (last_elem - first_elem) * elem_size; @@ -98,7 +121,7 @@ fn slice_read_bytes(slice: &[S], byte_offset: usize, buf: &mut [u8] // Write `data` at `byte_offset` into a slice of S elements. // Only deserializes/reserializes the overlapping elements. fn slice_write_bytes(slice: &mut [S], byte_offset: usize, data: &[u8]) { - let elem_size = std::mem::size_of::(); + let elem_size = S::byte_size(); let mut elem_buf = vec![0u8; elem_size]; let first_elem = byte_offset / elem_size; let num_elem = data.len().div_ceil(elem_size); @@ -136,7 +159,7 @@ impl OriginalAlloc for SingleOriginalAlloc { } fn total_byte_len(&self) -> usize { - std::mem::size_of::() + T::byte_size() } fn address(&self) -> usize { @@ -190,7 +213,7 @@ impl OriginalAlloc for SliceOriginalAlloc { fn total_byte_len(&self) -> usize { let rc = self.weak.upgrade().expect("ub: dangling pointer"); let val = rc.borrow(); - std::mem::size_of_val(val.as_slice()) + val.as_slice().len() * ::byte_size() } fn address(&self) -> usize { diff --git a/tests/multi-file/cross_tu_tag_collision/out/refcount/cross_tu_tag_collision.rs b/tests/multi-file/cross_tu_tag_collision/out/refcount/cross_tu_tag_collision.rs index 90529368..981fa9ae 100644 --- a/tests/multi-file/cross_tu_tag_collision/out/refcount/cross_tu_tag_collision.rs +++ b/tests/multi-file/cross_tu_tag_collision/out/refcount/cross_tu_tag_collision.rs @@ -11,6 +11,9 @@ pub struct widget { pub id: Value, } impl ByteRepr for widget { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.id.borrow()).to_bytes(&mut buf[0..4]); } diff --git a/tests/unit/out/refcount/addr_of_global.rs b/tests/unit/out/refcount/addr_of_global.rs index 01ddb7f7..0d03ca3b 100644 --- a/tests/unit/out/refcount/addr_of_global.rs +++ b/tests/unit/out/refcount/addr_of_global.rs @@ -19,6 +19,9 @@ impl Clone for Inner { } } impl ByteRepr for Inner { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.value.borrow()).to_bytes(&mut buf[0..4]); } diff --git a/tests/unit/out/refcount/anonymous-struct.rs b/tests/unit/out/refcount/anonymous-struct.rs index fcd7db80..ad257c16 100644 --- a/tests/unit/out/refcount/anonymous-struct.rs +++ b/tests/unit/out/refcount/anonymous-struct.rs @@ -21,6 +21,9 @@ impl Clone for Outer_Named { } } impl ByteRepr for Outer_Named { + fn byte_size() -> usize { + 8 + } 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]); @@ -47,6 +50,9 @@ impl Clone for anon_0 { } } impl ByteRepr for anon_0 { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.c.borrow()).to_bytes(&mut buf[0..4]); (*self.d.borrow()).to_bytes(&mut buf[4..8]); @@ -73,6 +79,9 @@ impl Clone for anon_1 { } } impl ByteRepr for anon_1 { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.g.borrow()).to_bytes(&mut buf[0..4]); (*self.h.borrow()).to_bytes(&mut buf[4..8]); @@ -99,6 +108,9 @@ impl Clone for anon_2 { } } impl ByteRepr for anon_2 { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.e.borrow()).to_bytes(&mut buf[0..4]); (*self.f.borrow()).to_bytes(&mut buf[4..8]); @@ -123,6 +135,9 @@ impl Clone for anon_4 { } } impl ByteRepr for anon_4 { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.j.borrow()).to_bytes(&mut buf[0..4]); } @@ -145,6 +160,9 @@ impl Clone for anon_5 { } } impl ByteRepr for anon_5 { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.k.borrow()).to_bytes(&mut buf[0..4]); } @@ -170,7 +188,23 @@ impl Clone for anon_3 { this } } -impl ByteRepr for anon_3 {} +impl ByteRepr for anon_3 { + fn byte_size() -> usize { + 12 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.i.borrow()).to_bytes(&mut buf[0..4]); + (*self.inner_named.borrow()).to_bytes(&mut buf[4..8]); + (*self.anon_5.borrow()).to_bytes(&mut buf[8..12]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + i: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + inner_named: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + anon_5: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + } + } +} #[derive(Default)] pub struct Outer { pub named: Value, @@ -191,7 +225,27 @@ impl Clone for Outer { this } } -impl ByteRepr for Outer {} +impl ByteRepr for Outer { + fn byte_size() -> usize { + 44 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.named.borrow()).to_bytes(&mut buf[0..8]); + (*self.anonymous_named_0.borrow()).to_bytes(&mut buf[8..16]); + (*self.anonymous_named_1.borrow()).to_bytes(&mut buf[16..24]); + (*self.anon_2.borrow()).to_bytes(&mut buf[24..32]); + (*self.anon_3.borrow()).to_bytes(&mut buf[32..44]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + named: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), + anonymous_named_0: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + anonymous_named_1: Rc::new(RefCell::new(::from_bytes(&buf[16..24]))), + anon_2: Rc::new(RefCell::new(::from_bytes(&buf[24..32]))), + anon_3: Rc::new(RefCell::new(::from_bytes(&buf[32..44]))), + } + } +} pub fn main() { std::process::exit(main_0()); } @@ -274,6 +328,9 @@ fn main_0() -> i32 { } } impl ByteRepr for anon_6 { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.x.borrow()).to_bytes(&mut buf[0..4]); (*self.z.borrow()).to_bytes(&mut buf[4..8]); diff --git a/tests/unit/out/refcount/anonymous-struct_c.rs b/tests/unit/out/refcount/anonymous-struct_c.rs index 23ce8df8..970d6e99 100644 --- a/tests/unit/out/refcount/anonymous-struct_c.rs +++ b/tests/unit/out/refcount/anonymous-struct_c.rs @@ -12,6 +12,9 @@ pub struct Named { pub b: Value, } impl ByteRepr for Named { + fn byte_size() -> usize { + 8 + } 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]); @@ -29,6 +32,9 @@ pub struct anon_0 { pub d: Value, } impl ByteRepr for anon_0 { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.c.borrow()).to_bytes(&mut buf[0..4]); (*self.d.borrow()).to_bytes(&mut buf[4..8]); @@ -46,6 +52,9 @@ pub struct anon_1 { pub h: Value, } impl ByteRepr for anon_1 { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.g.borrow()).to_bytes(&mut buf[0..4]); (*self.h.borrow()).to_bytes(&mut buf[4..8]); @@ -63,6 +72,9 @@ pub struct anon_2 { pub f: Value, } impl ByteRepr for anon_2 { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.e.borrow()).to_bytes(&mut buf[0..4]); (*self.f.borrow()).to_bytes(&mut buf[4..8]); @@ -79,6 +91,9 @@ pub struct anon_4 { pub j: Value, } impl ByteRepr for anon_4 { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.j.borrow()).to_bytes(&mut buf[0..4]); } @@ -93,6 +108,9 @@ pub struct anon_5 { pub k: Value, } impl ByteRepr for anon_5 { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.k.borrow()).to_bytes(&mut buf[0..4]); } @@ -108,7 +126,23 @@ pub struct anon_3 { pub inner_named: Value, pub anon_5: Value, } -impl ByteRepr for anon_3 {} +impl ByteRepr for anon_3 { + fn byte_size() -> usize { + 12 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.i.borrow()).to_bytes(&mut buf[0..4]); + (*self.inner_named.borrow()).to_bytes(&mut buf[4..8]); + (*self.anon_5.borrow()).to_bytes(&mut buf[8..12]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + i: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + inner_named: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + anon_5: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + } + } +} #[derive(Default)] pub struct Outer { pub named: Value, @@ -117,7 +151,27 @@ pub struct Outer { pub anon_2: Value, pub anon_3: Value, } -impl ByteRepr for Outer {} +impl ByteRepr for Outer { + fn byte_size() -> usize { + 44 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.named.borrow()).to_bytes(&mut buf[0..8]); + (*self.anon0.borrow()).to_bytes(&mut buf[8..16]); + (*self.anon1.borrow()).to_bytes(&mut buf[16..24]); + (*self.anon_2.borrow()).to_bytes(&mut buf[24..32]); + (*self.anon_3.borrow()).to_bytes(&mut buf[32..44]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + named: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), + anon0: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + anon1: Rc::new(RefCell::new(::from_bytes(&buf[16..24]))), + anon_2: Rc::new(RefCell::new(::from_bytes(&buf[24..32]))), + anon_3: Rc::new(RefCell::new(::from_bytes(&buf[32..44]))), + } + } +} pub fn main() { std::process::exit(main_0()); } @@ -176,6 +230,9 @@ fn main_0() -> i32 { pub z: Value, } impl ByteRepr for anon_6 { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.x.borrow()).to_bytes(&mut buf[0..4]); (*self.z.borrow()).to_bytes(&mut buf[4..8]); diff --git a/tests/unit/out/refcount/anonymous_enum.rs b/tests/unit/out/refcount/anonymous_enum.rs index 9fbf91ed..47298b56 100644 --- a/tests/unit/out/refcount/anonymous_enum.rs +++ b/tests/unit/out/refcount/anonymous_enum.rs @@ -67,6 +67,9 @@ impl Clone for S { } } impl ByteRepr for S { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.a.borrow()).to_bytes(&mut buf[0..4]); } @@ -139,6 +142,9 @@ impl Clone for WithAnonField { } } impl ByteRepr for WithAnonField { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.a.borrow()).to_bytes(&mut buf[0..4]); (*self.field.borrow()).to_bytes(&mut buf[4..8]); diff --git a/tests/unit/out/refcount/anonymous_enum_c.rs b/tests/unit/out/refcount/anonymous_enum_c.rs index 199dc233..a851a2b5 100644 --- a/tests/unit/out/refcount/anonymous_enum_c.rs +++ b/tests/unit/out/refcount/anonymous_enum_c.rs @@ -59,6 +59,9 @@ pub struct S { pub a: Value, } impl ByteRepr for S { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.a.borrow()).to_bytes(&mut buf[0..4]); } @@ -122,6 +125,9 @@ pub struct WithAnonField { pub field: Value, } impl ByteRepr for WithAnonField { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.a.borrow()).to_bytes(&mut buf[0..4]); (*self.field.borrow()).to_bytes(&mut buf[4..8]); diff --git a/tests/unit/out/refcount/array_const_init.rs b/tests/unit/out/refcount/array_const_init.rs index 784f3a73..7a2cf78d 100644 --- a/tests/unit/out/refcount/array_const_init.rs +++ b/tests/unit/out/refcount/array_const_init.rs @@ -25,7 +25,23 @@ impl Default for S { } } } -impl ByteRepr for S {} +impl ByteRepr for S { + fn byte_size() -> usize { + 20 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.head.borrow()).to_bytes(&mut buf[0..4]); + (*self.tail.borrow()).to_bytes(&mut buf[4..16]); + (*self.buf.borrow()).to_bytes(&mut buf[16..20]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + head: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + tail: Rc::new(RefCell::new(>::from_bytes(&buf[4..16]))), + buf: Rc::new(RefCell::new(>::from_bytes(&buf[16..20]))), + } + } +} thread_local!( pub static s_0: Value = Rc::new(RefCell::new(S { head: Rc::new(RefCell::new(5)), diff --git a/tests/unit/out/refcount/bounded_struct_ptr.rs b/tests/unit/out/refcount/bounded_struct_ptr.rs index 477a71a6..6da5e2fb 100644 --- a/tests/unit/out/refcount/bounded_struct_ptr.rs +++ b/tests/unit/out/refcount/bounded_struct_ptr.rs @@ -21,6 +21,9 @@ impl Clone for Foo { } } impl ByteRepr for Foo { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.x1.borrow()).to_bytes(&mut buf[0..4]); (*self.x2.borrow()).to_bytes(&mut buf[4..8]); diff --git a/tests/unit/out/refcount/c_struct.rs b/tests/unit/out/refcount/c_struct.rs index 36ebdeb4..1214d4a9 100644 --- a/tests/unit/out/refcount/c_struct.rs +++ b/tests/unit/out/refcount/c_struct.rs @@ -12,6 +12,9 @@ pub struct Point { pub y: Value, } impl ByteRepr for Point { + 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]); @@ -28,7 +31,21 @@ pub struct Line { pub start: Value, pub end: Value, } -impl ByteRepr for Line {} +impl ByteRepr for Line { + fn byte_size() -> usize { + 16 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.start.borrow()).to_bytes(&mut buf[0..8]); + (*self.end.borrow()).to_bytes(&mut buf[8..16]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + start: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), + end: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + } + } +} #[derive(Default)] pub struct Node { pub value: Value, @@ -67,6 +84,9 @@ pub struct Inner { pub b: Value, } impl ByteRepr for Inner { + fn byte_size() -> usize { + 8 + } 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]); @@ -84,7 +104,23 @@ pub struct Container { pub color: Value, pub count: Value, } -impl ByteRepr for Container {} +impl ByteRepr for Container { + fn byte_size() -> usize { + 16 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.inner.borrow()).to_bytes(&mut buf[0..8]); + (*self.color.borrow()).to_bytes(&mut buf[8..12]); + (*self.count.borrow()).to_bytes(&mut buf[12..16]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + inner: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), + color: Rc::new(RefCell::new(::from_bytes(&buf[8..12]))), + count: Rc::new(RefCell::new(::from_bytes(&buf[12..16]))), + } + } +} pub fn main() { std::process::exit(main_0()); } diff --git a/tests/unit/out/refcount/class.rs b/tests/unit/out/refcount/class.rs index a0c1f970..ff1c3cf4 100644 --- a/tests/unit/out/refcount/class.rs +++ b/tests/unit/out/refcount/class.rs @@ -54,6 +54,9 @@ impl Clone for Pair { } } impl ByteRepr for Pair { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.first.borrow()).to_bytes(&mut buf[0..4]); (*self.second.borrow()).to_bytes(&mut buf[4..8]); @@ -87,7 +90,21 @@ impl Clone for Route { this } } -impl ByteRepr for Route {} +impl ByteRepr for Route { + fn byte_size() -> usize { + 16 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.path.borrow()).to_bytes(&mut buf[0..8]); + (*self.cost.borrow()).to_bytes(&mut buf[8..16]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + path: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), + cost: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + } + } +} pub fn RandomRoute_0(route: Ptr) -> i32 { if (((*(*(*route.upgrade().deref()).path.borrow()).first.borrow()) % 2) != 0) { return ({ diff --git a/tests/unit/out/refcount/clone_vs_move.rs b/tests/unit/out/refcount/clone_vs_move.rs index 9f6ee411..1953e3d9 100644 --- a/tests/unit/out/refcount/clone_vs_move.rs +++ b/tests/unit/out/refcount/clone_vs_move.rs @@ -19,6 +19,9 @@ impl Clone for Bar { } } impl ByteRepr for Bar { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.w.borrow()).to_bytes(&mut buf[0..4]); } diff --git a/tests/unit/out/refcount/complex_function.rs b/tests/unit/out/refcount/complex_function.rs index 861fee9f..737edc12 100644 --- a/tests/unit/out/refcount/complex_function.rs +++ b/tests/unit/out/refcount/complex_function.rs @@ -30,6 +30,9 @@ impl Clone for X1 { } } impl ByteRepr for X1 { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.v.borrow()).to_bytes(&mut buf[0..4]); } diff --git a/tests/unit/out/refcount/enum_default_in_static.rs b/tests/unit/out/refcount/enum_default_in_static.rs index 0e6f4c29..866240a2 100644 --- a/tests/unit/out/refcount/enum_default_in_static.rs +++ b/tests/unit/out/refcount/enum_default_in_static.rs @@ -38,6 +38,9 @@ pub struct Config { pub mode: Value, } impl ByteRepr for Config { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.count.borrow()).to_bytes(&mut buf[0..4]); (*self.mode.borrow()).to_bytes(&mut buf[4..8]); diff --git a/tests/unit/out/refcount/exprs.rs b/tests/unit/out/refcount/exprs.rs index 6a4a1646..e8a9eed6 100644 --- a/tests/unit/out/refcount/exprs.rs +++ b/tests/unit/out/refcount/exprs.rs @@ -19,6 +19,9 @@ impl Clone for X { } } impl ByteRepr for X { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.x.borrow()).to_bytes(&mut buf[0..4]); } diff --git a/tests/unit/out/refcount/fft.rs b/tests/unit/out/refcount/fft.rs index 55e71cd7..12551ddb 100644 --- a/tests/unit/out/refcount/fft.rs +++ b/tests/unit/out/refcount/fft.rs @@ -21,6 +21,9 @@ impl Clone for Complex { } } impl ByteRepr for Complex { + fn byte_size() -> usize { + 16 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.re.borrow()).to_bytes(&mut buf[0..8]); (*self.img.borrow()).to_bytes(&mut buf[8..16]); diff --git a/tests/unit/out/refcount/fn_ptr_stable_sort.rs b/tests/unit/out/refcount/fn_ptr_stable_sort.rs index 5182170f..da7b2e8a 100644 --- a/tests/unit/out/refcount/fn_ptr_stable_sort.rs +++ b/tests/unit/out/refcount/fn_ptr_stable_sort.rs @@ -21,6 +21,9 @@ impl Clone for Item { } } impl ByteRepr for Item { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.key.borrow()).to_bytes(&mut buf[0..4]); (*self.value.borrow()).to_bytes(&mut buf[4..8]); diff --git a/tests/unit/out/refcount/function_overloading.rs b/tests/unit/out/refcount/function_overloading.rs index 7110cd08..4e39bf8f 100644 --- a/tests/unit/out/refcount/function_overloading.rs +++ b/tests/unit/out/refcount/function_overloading.rs @@ -63,6 +63,9 @@ impl Clone for Foo { } } impl ByteRepr for Foo { + fn byte_size() -> usize { + 1 + } fn to_bytes(&self, buf: &mut [u8]) {} fn from_bytes(buf: &[u8]) -> Self { Self {} diff --git a/tests/unit/out/refcount/global_without_initializer.rs b/tests/unit/out/refcount/global_without_initializer.rs index 51fce351..f1dd9e2a 100644 --- a/tests/unit/out/refcount/global_without_initializer.rs +++ b/tests/unit/out/refcount/global_without_initializer.rs @@ -19,6 +19,9 @@ impl Clone for S { } } impl ByteRepr for S { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.a.borrow()).to_bytes(&mut buf[0..4]); } diff --git a/tests/unit/out/refcount/goto_aggregate_default.rs b/tests/unit/out/refcount/goto_aggregate_default.rs index 1043512f..c2c6d4d4 100644 --- a/tests/unit/out/refcount/goto_aggregate_default.rs +++ b/tests/unit/out/refcount/goto_aggregate_default.rs @@ -12,6 +12,9 @@ pub struct Point { pub y: Value, } impl ByteRepr for Point { + 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]); diff --git a/tests/unit/out/refcount/immutable-deref-on-func-call.rs b/tests/unit/out/refcount/immutable-deref-on-func-call.rs index 416e1438..84ae55c9 100644 --- a/tests/unit/out/refcount/immutable-deref-on-func-call.rs +++ b/tests/unit/out/refcount/immutable-deref-on-func-call.rs @@ -25,6 +25,9 @@ impl Clone for Item { } } impl ByteRepr for Item { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.value.borrow()).to_bytes(&mut buf[0..4]); } diff --git a/tests/unit/out/refcount/init.rs b/tests/unit/out/refcount/init.rs index f74601da..2fed9750 100644 --- a/tests/unit/out/refcount/init.rs +++ b/tests/unit/out/refcount/init.rs @@ -19,6 +19,9 @@ impl Clone for X { } } impl ByteRepr for X { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.x.borrow()).to_bytes(&mut buf[0..4]); } diff --git a/tests/unit/out/refcount/kruskal.rs b/tests/unit/out/refcount/kruskal.rs index e558cab3..28978811 100644 --- a/tests/unit/out/refcount/kruskal.rs +++ b/tests/unit/out/refcount/kruskal.rs @@ -23,6 +23,9 @@ impl Clone for Edge { } } impl ByteRepr for Edge { + fn byte_size() -> usize { + 16 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.u.borrow()).to_bytes(&mut buf[0..4]); (*self.v.borrow()).to_bytes(&mut buf[4..8]); diff --git a/tests/unit/out/refcount/local_anon_struct_collision.rs b/tests/unit/out/refcount/local_anon_struct_collision.rs index c4b23e73..d69250d0 100644 --- a/tests/unit/out/refcount/local_anon_struct_collision.rs +++ b/tests/unit/out/refcount/local_anon_struct_collision.rs @@ -13,6 +13,9 @@ pub fn first_0() -> i32 { pub y: Value, } impl ByteRepr for anon_1 { + 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]); @@ -36,6 +39,9 @@ pub fn second_2() -> i32 { pub b: Value, } impl ByteRepr for anon_3 { + fn byte_size() -> usize { + 16 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.a.borrow()).to_bytes(&mut buf[0..8]); (*self.b.borrow()).to_bytes(&mut buf[8..16]); diff --git a/tests/unit/out/refcount/memcpy_struct_struct.rs b/tests/unit/out/refcount/memcpy_struct_struct.rs index 5ccc910e..158e7cff 100644 --- a/tests/unit/out/refcount/memcpy_struct_struct.rs +++ b/tests/unit/out/refcount/memcpy_struct_struct.rs @@ -21,6 +21,9 @@ impl Clone for Entry { } } impl ByteRepr for Entry { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.bits.borrow()).to_bytes(&mut buf[0..1]); (*self.value.borrow()).to_bytes(&mut buf[2..4]); @@ -77,9 +80,7 @@ fn main_0() -> i32 { .to_any() .memcpy( &(((table.as_pointer() as Ptr).offset(0 as isize)) as Ptr).to_any(), - (((*table_size.borrow()) as u64) - .wrapping_mul((::std::mem::size_of::() as u64)) - as usize) as usize, + (((*table_size.borrow()) as u64).wrapping_mul((4usize as u64)) as usize) as usize, ); (((table.as_pointer() as Ptr).offset((*table_size.borrow()) as isize)) as Ptr) .to_any() diff --git a/tests/unit/out/refcount/nested_structs.rs b/tests/unit/out/refcount/nested_structs.rs index 491cdf12..febf11a8 100644 --- a/tests/unit/out/refcount/nested_structs.rs +++ b/tests/unit/out/refcount/nested_structs.rs @@ -19,6 +19,9 @@ impl Clone for Level0_Level1_1_Level2_1_Level3_1 { } } impl ByteRepr for Level0_Level1_1_Level2_1_Level3_1 { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.x1.borrow()).to_bytes(&mut buf[0..4]); } @@ -43,6 +46,9 @@ impl Clone for Level0_Level1_1_Level2_1_Level3_2 { } } impl ByteRepr for Level0_Level1_1_Level2_1_Level3_2 { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.x1.borrow()).to_bytes(&mut buf[0..4]); (*self.x2.borrow()).to_bytes(&mut buf[4..8]); @@ -67,6 +73,9 @@ impl Clone for Level0_Level1_1_Level2_1 { } } impl ByteRepr for Level0_Level1_1_Level2_1 { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.x1.borrow()).to_bytes(&mut buf[0..4]); } @@ -89,6 +98,9 @@ impl Clone for Level0_Level1_1 { } } impl ByteRepr for Level0_Level1_1 { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.x1.borrow()).to_bytes(&mut buf[0..4]); } @@ -113,6 +125,9 @@ impl Clone for Level0_Level1_2 { } } impl ByteRepr for Level0_Level1_2 { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.x1.borrow()).to_bytes(&mut buf[0..4]); (*self.x2.borrow()).to_bytes(&mut buf[4..8]); @@ -133,6 +148,9 @@ impl Clone for Level0 { } } impl ByteRepr for Level0 { + fn byte_size() -> usize { + 1 + } fn to_bytes(&self, buf: &mut [u8]) {} fn from_bytes(buf: &[u8]) -> Self { Self {} diff --git a/tests/unit/out/refcount/new_struct.rs b/tests/unit/out/refcount/new_struct.rs index 9885653c..05ba3965 100644 --- a/tests/unit/out/refcount/new_struct.rs +++ b/tests/unit/out/refcount/new_struct.rs @@ -21,6 +21,9 @@ impl Clone for Pair { } } impl ByteRepr for Pair { + 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]); diff --git a/tests/unit/out/refcount/operator_less_than.rs b/tests/unit/out/refcount/operator_less_than.rs index cbf3d70b..94102602 100644 --- a/tests/unit/out/refcount/operator_less_than.rs +++ b/tests/unit/out/refcount/operator_less_than.rs @@ -62,6 +62,9 @@ impl Clone for Pair { } } impl ByteRepr for Pair { + 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]); diff --git a/tests/unit/out/refcount/pod.rs b/tests/unit/out/refcount/pod.rs index 9e62ffde..5db49891 100644 --- a/tests/unit/out/refcount/pod.rs +++ b/tests/unit/out/refcount/pod.rs @@ -23,6 +23,9 @@ impl Clone for POD { } } impl ByteRepr for POD { + fn byte_size() -> usize { + 12 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.x1.borrow()).to_bytes(&mut buf[0..4]); (*self.x2.borrow()).to_bytes(&mut buf[4..8]); diff --git a/tests/unit/out/refcount/pointers.rs b/tests/unit/out/refcount/pointers.rs index 065eb4bd..bbfc53c9 100644 --- a/tests/unit/out/refcount/pointers.rs +++ b/tests/unit/out/refcount/pointers.rs @@ -35,6 +35,9 @@ impl Clone for Test { } } impl ByteRepr for Test { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.x.borrow()).to_bytes(&mut buf[0..4]); } diff --git a/tests/unit/out/refcount/polymorphism.rs b/tests/unit/out/refcount/polymorphism.rs index b1f4297f..b50c4491 100644 --- a/tests/unit/out/refcount/polymorphism.rs +++ b/tests/unit/out/refcount/polymorphism.rs @@ -23,6 +23,9 @@ impl Clone for Dog { } } impl ByteRepr for Dog { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) {} fn from_bytes(buf: &[u8]) -> Self { Self {} @@ -47,6 +50,9 @@ impl Clone for Cat { } } impl ByteRepr for Cat { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) {} fn from_bytes(buf: &[u8]) -> Self { Self {} diff --git a/tests/unit/out/refcount/push_emplace_back.rs b/tests/unit/out/refcount/push_emplace_back.rs index 0ee8c04b..c4309f6f 100644 --- a/tests/unit/out/refcount/push_emplace_back.rs +++ b/tests/unit/out/refcount/push_emplace_back.rs @@ -19,6 +19,9 @@ impl Clone for Chunk { } } impl ByteRepr for Chunk { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.data.borrow()).to_bytes(&mut buf[0..4]); } diff --git a/tests/unit/out/refcount/random.rs b/tests/unit/out/refcount/random.rs index 3b1057f8..aa0e5a31 100644 --- a/tests/unit/out/refcount/random.rs +++ b/tests/unit/out/refcount/random.rs @@ -82,6 +82,9 @@ impl Clone for X1 { } } impl ByteRepr for X1 { + fn byte_size() -> usize { + 1 + } fn to_bytes(&self, buf: &mut [u8]) {} fn from_bytes(buf: &[u8]) -> Self { Self {} diff --git a/tests/unit/out/refcount/reinterpret_cast_struct.rs b/tests/unit/out/refcount/reinterpret_cast_struct.rs index 9e2370a1..1c9e14b5 100644 --- a/tests/unit/out/refcount/reinterpret_cast_struct.rs +++ b/tests/unit/out/refcount/reinterpret_cast_struct.rs @@ -21,6 +21,9 @@ impl Clone for Point { } } impl ByteRepr for Point { + 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]); diff --git a/tests/unit/out/refcount/reinterpret_cast_struct_to_struct.rs b/tests/unit/out/refcount/reinterpret_cast_struct_to_struct.rs index 0f065102..5b5d82c9 100644 --- a/tests/unit/out/refcount/reinterpret_cast_struct_to_struct.rs +++ b/tests/unit/out/refcount/reinterpret_cast_struct_to_struct.rs @@ -21,6 +21,9 @@ impl Clone for Point { } } impl ByteRepr for Point { + 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]); @@ -47,6 +50,9 @@ impl Clone for Pair { } } impl ByteRepr for Pair { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.first.borrow()).to_bytes(&mut buf[0..4]); (*self.second.borrow()).to_bytes(&mut buf[4..8]); diff --git a/tests/unit/out/refcount/reserved_keywords.rs b/tests/unit/out/refcount/reserved_keywords.rs index b670988f..5258cfab 100644 --- a/tests/unit/out/refcount/reserved_keywords.rs +++ b/tests/unit/out/refcount/reserved_keywords.rs @@ -93,6 +93,9 @@ impl Clone for S { } } impl ByteRepr for S { + fn byte_size() -> usize { + 152 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.as_.borrow()).to_bytes(&mut buf[0..4]); (*self.async_.borrow()).to_bytes(&mut buf[4..8]); diff --git a/tests/unit/out/refcount/static_var_in_class.rs b/tests/unit/out/refcount/static_var_in_class.rs index 35e012e7..d08a4f63 100644 --- a/tests/unit/out/refcount/static_var_in_class.rs +++ b/tests/unit/out/refcount/static_var_in_class.rs @@ -23,6 +23,9 @@ impl Clone for C { } } impl ByteRepr for C { + fn byte_size() -> usize { + 1 + } fn to_bytes(&self, buf: &mut [u8]) {} fn from_bytes(buf: &[u8]) -> Self { Self {} @@ -40,6 +43,9 @@ impl Clone for S { } } impl ByteRepr for S { + fn byte_size() -> usize { + 1 + } fn to_bytes(&self, buf: &mut [u8]) {} fn from_bytes(buf: &[u8]) -> Self { Self {} diff --git a/tests/unit/out/refcount/struct_ctor.rs b/tests/unit/out/refcount/struct_ctor.rs index d5a4c660..430fd275 100644 --- a/tests/unit/out/refcount/struct_ctor.rs +++ b/tests/unit/out/refcount/struct_ctor.rs @@ -40,6 +40,9 @@ impl Clone for StructWithCtor { } } impl ByteRepr for StructWithCtor { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.x1_.borrow()).to_bytes(&mut buf[0..4]); (*self.x2_.borrow()).to_bytes(&mut buf[4..8]); diff --git a/tests/unit/out/refcount/struct_ptr.rs b/tests/unit/out/refcount/struct_ptr.rs index 4defc67a..beb54800 100644 --- a/tests/unit/out/refcount/struct_ptr.rs +++ b/tests/unit/out/refcount/struct_ptr.rs @@ -19,6 +19,9 @@ impl Clone for XX { } } impl ByteRepr for XX { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.x.borrow()).to_bytes(&mut buf[0..4]); } diff --git a/tests/unit/out/refcount/tag_vs_identifier_collision.rs b/tests/unit/out/refcount/tag_vs_identifier_collision.rs index eccceda4..bf14ce14 100644 --- a/tests/unit/out/refcount/tag_vs_identifier_collision.rs +++ b/tests/unit/out/refcount/tag_vs_identifier_collision.rs @@ -38,6 +38,9 @@ pub struct widget { pub mode: Value, } impl ByteRepr for widget { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.id.borrow()).to_bytes(&mut buf[0..4]); (*self.mode.borrow()).to_bytes(&mut buf[4..8]); @@ -55,6 +58,9 @@ pub struct point_struct { pub y: Value, } impl ByteRepr for point_struct { + 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]); @@ -147,6 +153,9 @@ pub struct Inner { pub tag_field: Value, } impl ByteRepr for Inner { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.tag_field.borrow()).to_bytes(&mut buf[0..4]); } @@ -160,12 +169,27 @@ impl ByteRepr for Inner { pub struct Outer { pub field: Value, } -impl ByteRepr for Outer {} +impl ByteRepr for Outer { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.field.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + field: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} #[derive(Default)] pub struct Inner_struct { pub typedef_field: Value, } impl ByteRepr for Inner_struct { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.typedef_field.borrow()).to_bytes(&mut buf[0..4]); } diff --git a/tests/unit/out/refcount/typedef-anon-struct.rs b/tests/unit/out/refcount/typedef-anon-struct.rs index 251d05d8..81475e04 100644 --- a/tests/unit/out/refcount/typedef-anon-struct.rs +++ b/tests/unit/out/refcount/typedef-anon-struct.rs @@ -21,6 +21,9 @@ impl Clone for Outer_RunInfo { } } impl ByteRepr for Outer_RunInfo { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.block_idx.borrow()).to_bytes(&mut buf[0..4]); (*self.num_extra_zero_runs.borrow()).to_bytes(&mut buf[4..8]); diff --git a/tests/unit/out/refcount/union_tagged_struct_arms.rs b/tests/unit/out/refcount/union_tagged_struct_arms.rs index 37417848..4155c690 100644 --- a/tests/unit/out/refcount/union_tagged_struct_arms.rs +++ b/tests/unit/out/refcount/union_tagged_struct_arms.rs @@ -47,6 +47,9 @@ pub struct anon_2 { pub step: Value, } impl ByteRepr for anon_2 { + fn byte_size() -> usize { + 16 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.lo.borrow()).to_bytes(&mut buf[0..4]); (*self.hi.borrow()).to_bytes(&mut buf[4..8]); @@ -71,6 +74,9 @@ pub struct anon_3 { pub width: Value, } impl ByteRepr for anon_3 { + fn byte_size() -> usize { + 40 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.lo.borrow()).to_bytes(&mut buf[0..8]); (*self.hi.borrow()).to_bytes(&mut buf[8..16]); diff --git a/tests/unit/out/refcount/unique_ptr.rs b/tests/unit/out/refcount/unique_ptr.rs index 9221a605..8f570b48 100644 --- a/tests/unit/out/refcount/unique_ptr.rs +++ b/tests/unit/out/refcount/unique_ptr.rs @@ -31,6 +31,9 @@ impl Clone for Pair { } } impl ByteRepr for Pair { + 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]); diff --git a/tests/unit/out/refcount/unique_ptr_nested.rs b/tests/unit/out/refcount/unique_ptr_nested.rs index 94cfb1d5..e1ad9181 100644 --- a/tests/unit/out/refcount/unique_ptr_nested.rs +++ b/tests/unit/out/refcount/unique_ptr_nested.rs @@ -21,6 +21,9 @@ impl Clone for Inner { } } impl ByteRepr for Inner { + 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]); diff --git a/tests/unit/out/refcount/unique_ptr_struct.rs b/tests/unit/out/refcount/unique_ptr_struct.rs index ec65ca5b..bc8381b7 100644 --- a/tests/unit/out/refcount/unique_ptr_struct.rs +++ b/tests/unit/out/refcount/unique_ptr_struct.rs @@ -21,6 +21,9 @@ impl Clone for Point { } } impl ByteRepr for Point { + 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]); diff --git a/tests/unit/out/refcount/va_arg_struct_ctx.rs b/tests/unit/out/refcount/va_arg_struct_ctx.rs index 37effcea..b6b031f5 100644 --- a/tests/unit/out/refcount/va_arg_struct_ctx.rs +++ b/tests/unit/out/refcount/va_arg_struct_ctx.rs @@ -12,6 +12,9 @@ pub struct context { pub last_error: Value, } impl ByteRepr for context { + fn byte_size() -> usize { + 8 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.verbose.borrow()).to_bytes(&mut buf[0..4]); (*self.last_error.borrow()).to_bytes(&mut buf[4..8]); diff --git a/tests/unit/out/refcount/void_cast.rs b/tests/unit/out/refcount/void_cast.rs index aa18bf00..0933260f 100644 --- a/tests/unit/out/refcount/void_cast.rs +++ b/tests/unit/out/refcount/void_cast.rs @@ -50,6 +50,9 @@ impl Clone for Holder { } } impl ByteRepr for Holder { + fn byte_size() -> usize { + 4 + } fn to_bytes(&self, buf: &mut [u8]) { (*self.field.borrow()).to_bytes(&mut buf[0..4]); }