-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8d61450
Showing
8 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
**/*.rs.bk |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "rust-ffi-enum-box" | ||
version = "0.1.0" | ||
authors = ["marmeladema <[email protected]>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
|
||
[build-dependencies] | ||
cc = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# rust-ffi-enum-box |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use cc; | ||
|
||
fn main() { | ||
cc::Build::new() | ||
.include("include") | ||
.file("src/tests.c") | ||
.warnings(true) | ||
.extra_warnings(true) | ||
.warnings_into_errors(true) | ||
.compile("ctests"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#ifndef _TESTS_H_ | ||
#define _TESTS_H_ | ||
|
||
#include <stdlib.h> | ||
#include <stdint.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
// https://github.com/rust-lang/rfcs/blob/master/text/2195-really-tagged-unions.md | ||
typedef enum { | ||
SCHEME_TYPE_TAG_IP, | ||
SCHEME_TYPE_TAG_BYTES, | ||
SCHEME_TYPE_TAG_INT, | ||
SCHEME_TYPE_TAG_BOOL, | ||
SCHEME_TYPE_TAG_ARRAY, | ||
SCHEME_TYPE_TAG_MAP, | ||
} scheme_type_tag_t; | ||
|
||
typedef struct { | ||
scheme_type_tag_t tag; | ||
void *data[1]; | ||
} scheme_type_t; | ||
|
||
static const scheme_type_t SCHEME_TYPE_IP = {.tag = SCHEME_TYPE_TAG_IP, .data = {NULL}}; | ||
static const scheme_type_t SCHEME_TYPE_BYTES = {.tag = SCHEME_TYPE_TAG_BYTES, .data = {NULL}}; | ||
static const scheme_type_t SCHEME_TYPE_INT = {.tag = SCHEME_TYPE_TAG_INT, .data = {NULL}}; | ||
static const scheme_type_t SCHEME_TYPE_BOOL = {.tag = SCHEME_TYPE_TAG_BOOL, .data = {NULL}}; | ||
|
||
typedef struct scheme scheme_t; | ||
|
||
scheme_t *scheme_new(); | ||
void scheme_free(scheme_t *); | ||
|
||
void scheme_set_type(scheme_t *scheme, scheme_type_t ty); | ||
|
||
void scheme_print(scheme_t *scheme); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // _TESTS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#[derive(Debug)] | ||
#[repr(C)] | ||
pub enum SchemeType { | ||
Ip, | ||
Bytes, | ||
Int, | ||
Bool, | ||
Array(Box<SchemeType>), | ||
Map(Box<SchemeType>), | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Scheme { | ||
ty: Option<SchemeType>, | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn scheme_new() -> *mut Scheme { | ||
Box::into_raw(Box::new(Scheme { ty: None })) | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn scheme_free(scheme: *mut Scheme) { | ||
unsafe { | ||
Box::from_raw(scheme); | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn scheme_set_type(scheme: *mut Scheme, ty: SchemeType) { | ||
unsafe { | ||
(*scheme).ty = Some(ty); | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn scheme_print(scheme: *mut Scheme) { | ||
unsafe { | ||
println!("{:?}", (*scheme)); | ||
} | ||
} | ||
|
||
extern "C" { | ||
fn scheme_ctest_01(); | ||
fn scheme_ctest_02(); | ||
} | ||
|
||
fn main() { | ||
println!("Hello, world!"); | ||
println!( | ||
"sizeof(SchemeType) = {:?}", | ||
std::mem::size_of::<SchemeType>() | ||
); | ||
unsafe { scheme_ctest_01() }; | ||
unsafe { scheme_ctest_02() }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include "tests.h" | ||
|
||
void scheme_ctest_01() { | ||
scheme_t *scheme = scheme_new(); | ||
scheme_print(scheme); | ||
scheme_set_type(scheme, SCHEME_TYPE_INT); | ||
scheme_print(scheme); | ||
scheme_free(scheme); | ||
} | ||
|
||
void scheme_ctest_02() { | ||
scheme_t *scheme = scheme_new(); | ||
scheme_print(scheme); | ||
scheme_type_t *ty = (scheme_type_t *)malloc(sizeof(scheme_type_t)); | ||
*ty = SCHEME_TYPE_INT; | ||
scheme_set_type(scheme, *ty); | ||
free(ty); | ||
scheme_print(scheme); | ||
scheme_free(scheme); | ||
} |