Manually copy arrays
C++ arrays can't be assigned. We detect this situation and manually copy over the elements.
This commit is contained in:
committed by
Ryan Hunt
parent
b718662520
commit
42b4290c64
@@ -684,15 +684,33 @@ impl Source for Enum {
|
|||||||
write!(out, "{} result;", self.export_name);
|
write!(out, "{} result;", self.export_name);
|
||||||
|
|
||||||
if let Some((ref variant_name, ref body)) = variant.body {
|
if let Some((ref variant_name, ref body)) = variant.body {
|
||||||
for &(ref field_name, ..) in body.fields.iter().skip(skip_fields) {
|
for &(ref field_name, ref ty, ..) in body.fields.iter().skip(skip_fields) {
|
||||||
out.new_line();
|
out.new_line();
|
||||||
write!(
|
match ty {
|
||||||
out,
|
Type::Array(ref _ty, ref length) => {
|
||||||
"result.{}.{} = {};",
|
// arrays are not assignable in C++ so we
|
||||||
variant_name,
|
// need to manually copy the elements
|
||||||
field_name,
|
write!(
|
||||||
arg_renamer(field_name)
|
out,
|
||||||
);
|
"for (int i = 0; i < {}; i++) {{\
|
||||||
|
result.{}.{}[i] = {}[i];\
|
||||||
|
}}",
|
||||||
|
length.as_str(),
|
||||||
|
variant_name,
|
||||||
|
field_name,
|
||||||
|
arg_renamer(field_name)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
write!(
|
||||||
|
out,
|
||||||
|
"result.{}.{} = {};",
|
||||||
|
variant_name,
|
||||||
|
field_name,
|
||||||
|
arg_renamer(field_name)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
A,
|
||||||
|
} Foo_Tag;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
float _0[20];
|
||||||
|
} A_Body;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
Foo_Tag tag;
|
||||||
|
union {
|
||||||
|
A_Body a;
|
||||||
|
};
|
||||||
|
} Foo;
|
||||||
|
|
||||||
|
void root(Foo a);
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
#include <cstdarg>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
struct Foo {
|
||||||
|
enum class Tag {
|
||||||
|
A,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct A_Body {
|
||||||
|
float _0[20];
|
||||||
|
};
|
||||||
|
|
||||||
|
Tag tag;
|
||||||
|
union {
|
||||||
|
A_Body a;
|
||||||
|
};
|
||||||
|
|
||||||
|
static Foo A(const float (&a0)[20]) {
|
||||||
|
Foo result;
|
||||||
|
for (int i = 0; i < 20; i++) {result.a._0[i] = a0[i];}
|
||||||
|
result.tag = Tag::A;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsA() const {
|
||||||
|
return tag == Tag::A;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
|
||||||
|
void root(Foo a);
|
||||||
|
|
||||||
|
} // extern "C"
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef enum Foo_Tag {
|
||||||
|
A,
|
||||||
|
} Foo_Tag;
|
||||||
|
|
||||||
|
typedef struct A_Body {
|
||||||
|
float _0[20];
|
||||||
|
} A_Body;
|
||||||
|
|
||||||
|
typedef struct Foo {
|
||||||
|
Foo_Tag tag;
|
||||||
|
union {
|
||||||
|
A_Body a;
|
||||||
|
};
|
||||||
|
} Foo;
|
||||||
|
|
||||||
|
void root(Foo a);
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
enum Foo_Tag {
|
||||||
|
A,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct A_Body {
|
||||||
|
float _0[20];
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Foo {
|
||||||
|
enum Foo_Tag tag;
|
||||||
|
union {
|
||||||
|
struct A_Body a;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
void root(struct Foo a);
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
#[repr(C)]
|
||||||
|
enum Foo {
|
||||||
|
A([f32; 20])
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn root(a: Foo) {}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
[enum]
|
||||||
|
derive_helper_methods = true
|
||||||
Reference in New Issue
Block a user