Add similar examples that work to each test.
This commit is contained in:
parent
f1b52b34f2
commit
26bb0f15e7
4 changed files with 87 additions and 6 deletions
|
|
@ -1,7 +1,23 @@
|
|||
#![crate_type = "lib"]
|
||||
#![crate_name = "nonclike"]
|
||||
|
||||
#[repr(C,u8)]
|
||||
#[repr(C, u8)]
|
||||
pub enum TT {
|
||||
AA(u64, u64),
|
||||
BB,
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn tt_add(a: TT, b: TT) -> u64 {
|
||||
match (a, b) {
|
||||
(TT::AA(a1, b1), TT::AA(a2, b2)) => a1 + a2 + b1 + b2,
|
||||
(TT::AA(a1, b1), TT::BB) => a1 + b1,
|
||||
(TT::BB, TT::AA(a1, b1)) => a1 + b1,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C, u8)]
|
||||
pub enum T {
|
||||
A(u64),
|
||||
B,
|
||||
|
|
@ -9,7 +25,7 @@ pub enum T {
|
|||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn t_add(a: T, b: T) -> u64 {
|
||||
match (a,b) {
|
||||
match (a, b) {
|
||||
(T::A(a), T::A(b)) => a + b,
|
||||
(T::A(a), T::B) => a,
|
||||
(T::B, T::A(b)) => b,
|
||||
|
|
|
|||
|
|
@ -3,6 +3,26 @@
|
|||
|
||||
#include <stdio.h>
|
||||
|
||||
/* This is the code generated by cbindgen 0.12.1 for the `enum TT`
|
||||
* type in nonclike.rs . */
|
||||
enum TT_Tag {
|
||||
AA,
|
||||
BB,
|
||||
};
|
||||
typedef uint8_t TT_Tag;
|
||||
|
||||
typedef struct {
|
||||
uint64_t _0;
|
||||
uint64_t _1;
|
||||
} AA_Body;
|
||||
|
||||
typedef struct {
|
||||
TT_Tag tag;
|
||||
union {
|
||||
AA_Body aa;
|
||||
};
|
||||
} TT;
|
||||
|
||||
/* This is the code generated by cbindgen 0.12.1 for the `enum T` type
|
||||
* in nonclike.rs . */
|
||||
enum T_Tag {
|
||||
|
|
@ -22,18 +42,24 @@ typedef struct {
|
|||
};
|
||||
} T;
|
||||
|
||||
/* This symbol is defined by the Rust staticlib built from
|
||||
/* These symbols are defined by the Rust staticlib built from
|
||||
* nonclike.rs. */
|
||||
extern uint64_t t_add(T a, T b);
|
||||
extern uint64_t tt_add(TT a, TT b);
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
(void)argc; (void)argv;
|
||||
|
||||
/* This example works. */
|
||||
TT xx = { .tag = AA, .aa = { ._0 = 1, ._1 = 2 } };
|
||||
TT yy = { .tag = AA, .aa = { ._0 = 10, ._1 = 20 } };
|
||||
uint64_t rr = tt_add(xx, yy);
|
||||
assert(33 == rr);
|
||||
|
||||
/* This one returns an incorrect result. */
|
||||
T x = { .tag = A, .a = { ._0 = 1 } };
|
||||
T y = { .tag = A, .a = { ._0 = 10 } };
|
||||
|
||||
uint64_t r = t_add(x, y);
|
||||
|
||||
assert(11 == r);
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,17 @@
|
|||
#![crate_type = "lib"]
|
||||
#![crate_name = "nonclike"]
|
||||
|
||||
#[repr(C, u8)]
|
||||
pub enum TT {
|
||||
AA(u64, u64),
|
||||
BB,
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn tt_new(a: u64, b: u64) -> TT {
|
||||
TT::AA(a, b)
|
||||
}
|
||||
|
||||
#[repr(C,u8)]
|
||||
pub enum T {
|
||||
A(u64),
|
||||
|
|
|
|||
|
|
@ -1,6 +1,26 @@
|
|||
#include <stdint.h>
|
||||
#include <assert.h>
|
||||
|
||||
/* This is the code generated by cbindgen 0.12.1 for the `enum TT`
|
||||
* type in nonclike.rs . */
|
||||
enum TT_Tag {
|
||||
AA,
|
||||
BB,
|
||||
};
|
||||
typedef uint8_t TT_Tag;
|
||||
|
||||
typedef struct {
|
||||
uint64_t _0;
|
||||
uint64_t _1;
|
||||
} AA_Body;
|
||||
|
||||
typedef struct {
|
||||
TT_Tag tag;
|
||||
union {
|
||||
AA_Body aa;
|
||||
};
|
||||
} TT;
|
||||
|
||||
/* This is the code generated by cbindgen 0.12.1 for the `enum T` type
|
||||
* in nonclike.rs . */
|
||||
enum T_Tag {
|
||||
|
|
@ -20,13 +40,21 @@ typedef struct {
|
|||
};
|
||||
} T;
|
||||
|
||||
/* This symbol is defined by the Rust staticlib built from
|
||||
/* These symbols are defined by the Rust staticlib built from
|
||||
* nonclike.rs. */
|
||||
extern TT tt_new(uint64_t a, uint64_t b);
|
||||
extern T t_new(uint64_t v);
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
(void)argc; (void)argv;
|
||||
|
||||
/* This example works. */
|
||||
TT tt = tt_new(10, 20);
|
||||
assert(AA == tt.tag);
|
||||
assert(10 == tt.aa._0);
|
||||
assert(20 == tt.aa._1);
|
||||
|
||||
/* This one segfaults. */
|
||||
T t = t_new(10);
|
||||
assert(A == t.tag);
|
||||
assert(10 == t.a._0);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue