Skip to content

Commit 803613d

Browse files
committed
Add C++ checks for triviality of Trivial types.
1 parent 0f752e1 commit 803613d

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

gen/src/write.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ pub(super) fn gen(
8080
write_struct_with_methods(out, ety, methods);
8181
}
8282
}
83+
Api::TypeAlias(ety) => {
84+
if types.required_trivial_types.contains(&ety.ident) {
85+
check_trivial_extern_type(out, &ety.ident)
86+
}
87+
}
8388
_ => {}
8489
}
8590
}
@@ -124,12 +129,17 @@ pub(super) fn gen(
124129
fn write_includes(out: &mut OutFile, types: &Types) {
125130
for ty in types {
126131
match ty {
127-
Type::Ident(ident) => match Atom::from(ident) {
128-
Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
129-
| Some(I64) => out.include.cstdint = true,
130-
Some(Usize) => out.include.cstddef = true,
131-
Some(CxxString) => out.include.string = true,
132-
Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
132+
Type::Ident(ident) => {
133+
match Atom::from(ident) {
134+
Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
135+
| Some(I64) => out.include.cstdint = true,
136+
Some(Usize) => out.include.cstddef = true,
137+
Some(CxxString) => out.include.string = true,
138+
Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) | None => {}
139+
};
140+
if types.required_trivial_types.contains(&ident) {
141+
out.include.type_traits = true;
142+
};
133143
},
134144
Type::RustBox(_) => out.include.type_traits = true,
135145
Type::UniquePtr(_) => out.include.memory = true,
@@ -401,6 +411,11 @@ fn check_enum(out: &mut OutFile, enm: &Enum) {
401411
}
402412
}
403413

414+
fn check_trivial_extern_type(out: &mut OutFile, id: &Ident) {
415+
writeln!(out, "static_assert(std::is_trivially_move_constructible<{}>::value,\"type {} marked as Trivial in Rust is not trivially move constructible in C++\");", id, id);
416+
writeln!(out, "static_assert(std::is_trivially_destructible<{}>::value,\"type {} marked as Trivial in Rust is not trivially destructible in C++\");", id, id);
417+
}
418+
404419
fn write_exception_glue(out: &mut OutFile, apis: &[Api]) {
405420
let mut has_cxx_throws = false;
406421
for api in apis {

0 commit comments

Comments
 (0)