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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ bool TypeImplementsByteRepr(clang::QualType qt) {
return false;
}
if (rd->isUnion()) {
return false;
return true;
}
for (const auto *field : rd->fields()) {
if (!TypeImplementsByteRepr(field->getType())) {
Expand Down
13 changes: 12 additions & 1 deletion cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,9 +554,20 @@ void ConverterRefCount::AddByteReprTrait(const clang::RecordDecl *decl) {
return;
}

StrCat(std::format("impl ByteRepr for {}", struct_name));
StrCat("impl ByteRepr for ", struct_name);
PushBrace impl_brace(*this);

if (decl->isUnion()) {
StrCat(std::format("fn byte_size() -> usize {{ {} }}",
ctx_.getTypeSize(ctx_.getCanonicalTagType(decl)) / 8));
StrCat("fn to_bytes(&self, buf: &mut [u8]) { "
"buf.copy_from_slice(&self.__bytes.borrow()); }");
StrCat(std::format("fn from_bytes(buf: &[u8]) -> Self {{ {} {{ __bytes: "
"Rc::new(RefCell::new(Box::from(buf))) }} }}",
struct_name));
return;
}

const auto &layout = ctx_.getASTRecordLayout(decl);

StrCat(std::format("fn byte_size() -> usize {{ {} }}",
Expand Down
28 changes: 26 additions & 2 deletions tests/unit/out/refcount/tag_vs_identifier_collision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,19 @@ impl Default for point {
}
}
}
impl ByteRepr for point {}
impl ByteRepr for point {
fn byte_size() -> usize {
4
}
fn to_bytes(&self, buf: &mut [u8]) {
buf.copy_from_slice(&self.__bytes.borrow());
}
fn from_bytes(buf: &[u8]) -> Self {
point {
__bytes: Rc::new(RefCell::new(Box::from(buf))),
}
}
}
pub struct slot_union {
__bytes: Value<Box<[u8]>>,
}
Expand All @@ -123,7 +135,19 @@ impl Default for slot_union {
}
}
}
impl ByteRepr for slot_union {}
impl ByteRepr for slot_union {
fn byte_size() -> usize {
4
}
fn to_bytes(&self, buf: &mut [u8]) {
buf.copy_from_slice(&self.__bytes.borrow());
}
fn from_bytes(buf: &[u8]) -> Self {
slot_union {
__bytes: Rc::new(RefCell::new(Box::from(buf))),
}
}
}
#[derive(Clone, Copy, PartialEq, Debug, Default)]
enum slot {
#[default]
Expand Down
14 changes: 13 additions & 1 deletion tests/unit/out/refcount/union_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,19 @@ impl Default for basic {
}
}
}
impl ByteRepr for basic {}
impl ByteRepr for basic {
fn byte_size() -> usize {
4
}
fn to_bytes(&self, buf: &mut [u8]) {
buf.copy_from_slice(&self.__bytes.borrow());
}
fn from_bytes(buf: &[u8]) -> Self {
basic {
__bytes: Rc::new(RefCell::new(Box::from(buf))),
}
}
}
pub fn main() {
std::process::exit(main_0());
}
Expand Down
14 changes: 13 additions & 1 deletion tests/unit/out/refcount/union_pointer_pun_writethrough.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,19 @@ fn main_0() -> i32 {
}
}
}
impl ByteRepr for anon_0 {};
impl ByteRepr for anon_0 {
fn byte_size() -> usize {
8
}
fn to_bytes(&self, buf: &mut [u8]) {
buf.copy_from_slice(&self.__bytes.borrow());
}
fn from_bytes(buf: &[u8]) -> Self {
anon_0 {
__bytes: Rc::new(RefCell::new(Box::from(buf))),
}
}
};
let pp: Value<anon_0> = <Value<anon_0>>::default();
(*pp.borrow_mut()).as_signed().write((x.as_pointer()));
((*pp.borrow()).as_unsigned().read()).write(42_u64);
Expand Down
32 changes: 30 additions & 2 deletions tests/unit/out/refcount/union_tagged_struct_arms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,42 @@ impl Default for anon_0 {
}
}
}
impl ByteRepr for anon_0 {}
impl ByteRepr for anon_0 {
fn byte_size() -> usize {
40
}
fn to_bytes(&self, buf: &mut [u8]) {
buf.copy_from_slice(&self.__bytes.borrow());
}
fn from_bytes(buf: &[u8]) -> Self {
anon_0 {
__bytes: Rc::new(RefCell::new(Box::from(buf))),
}
}
}
#[derive(Default)]
pub struct Branch {
pub choice: Value<Choice_enum>,
pub index: Value<i32>,
pub v: Value<anon_0>,
}
impl ByteRepr for Branch {}
impl ByteRepr for Branch {
fn byte_size() -> usize {
48
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.choice.borrow()).to_bytes(&mut buf[0..4]);
(*self.index.borrow()).to_bytes(&mut buf[4..8]);
(*self.v.borrow()).to_bytes(&mut buf[8..48]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
choice: Rc::new(RefCell::new(<Choice_enum>::from_bytes(&buf[0..4]))),
index: Rc::new(RefCell::new(<i32>::from_bytes(&buf[4..8]))),
v: Rc::new(RefCell::new(<anon_0>::from_bytes(&buf[8..48]))),
}
}
}
pub fn main() {
std::process::exit(main_0());
}
Expand Down
Loading