Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
marmeladema committed Aug 31, 2019
0 parents commit 8d61450
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
**/*.rs.bk
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Cargo.toml
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"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# rust-ffi-enum-box
11 changes: 11 additions & 0 deletions build.rs
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");
}
44 changes: 44 additions & 0 deletions include/tests.h
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_
56 changes: 56 additions & 0 deletions src/main.rs
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() };
}
20 changes: 20 additions & 0 deletions src/tests.c
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);
}

0 comments on commit 8d61450

Please sign in to comment.