add c_export
This commit is contained in:
14
c_export/Cargo.toml
Normal file
14
c_export/Cargo.toml
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
[package]
|
||||||
|
name = "c_export"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
name = "stringtools"
|
||||||
|
crate-type = ["dylib"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
libc = "0.2.60"
|
||||||
29
c_export/README.md
Normal file
29
c_export/README.md
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
|
||||||
|
Rust libray build:
|
||||||
|
```
|
||||||
|
cargo build
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
Python test:
|
||||||
|
```
|
||||||
|
python call_in_python.py
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
C test:
|
||||||
|
```
|
||||||
|
gcc call_in_c.c -L target/debug/ -lstringtools
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
NodeJS test:
|
||||||
|
```
|
||||||
|
npm install ffi
|
||||||
|
node call_in_node.js
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
Reference:
|
||||||
|
http://siciarz.net/24-days-of-rust-calling-rust-from-other-languages/
|
||||||
|
|
||||||
9
c_export/call_in_c.c
Normal file
9
c_export/call_in_c.c
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int32_t count_substrings(const char* value, const char* substr);
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
printf("%d\n", count_substrings("banana", "na"));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
8
c_export/call_in_node.js
Normal file
8
c_export/call_in_node.js
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
var ffi = require('ffi');
|
||||||
|
|
||||||
|
var stringtools = ffi.Library('target/debug/libstringtools.dylib', {
|
||||||
|
'count_substrings': ['int', ['string', 'string']]
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(stringtools.count_substrings("banana", "na"));
|
||||||
|
|
||||||
5
c_export/call_in_python.py
Normal file
5
c_export/call_in_python.py
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import ctypes
|
||||||
|
|
||||||
|
stringtools = ctypes.CDLL("target/debug/libstringtools.dylib")
|
||||||
|
print(stringtools.count_substrings(b"banana", b"na"))
|
||||||
|
|
||||||
17
c_export/src/lib.rs
Normal file
17
c_export/src/lib.rs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
extern crate libc;
|
||||||
|
|
||||||
|
use std::ffi::CStr;
|
||||||
|
use libc::c_char;
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn count_substrings(value: *const c_char, substr: *const c_char) -> i32 {
|
||||||
|
let c_value = unsafe { CStr::from_ptr(value) };
|
||||||
|
let c_substr = unsafe { CStr::from_ptr(substr) };
|
||||||
|
match c_value.to_str() {
|
||||||
|
Ok(value) => match c_substr.to_str() {
|
||||||
|
Ok(substr) => value.match_indices(substr).count() as i32,
|
||||||
|
Err(_) => -1,
|
||||||
|
},
|
||||||
|
Err(_) => -1,
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user