chore: move __c_cpp/* to __ffi
This commit is contained in:
16
__ffi/c_export/Cargo.lock
generated
Normal file
16
__ffi/c_export/Cargo.lock
generated
Normal file
@@ -0,0 +1,16 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "c_export"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.60"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[metadata]
|
||||
"checksum libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)" = "d44e80633f007889c7eff624b709ab43c92d708caad982295768a7b13ca3b5eb"
|
||||
14
__ffi/c_export/Cargo.toml
Normal file
14
__ffi/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
__ffi/c_export/README.md
Normal file
29
__ffi/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
__ffi/c_export/call_in_c.c
Normal file
9
__ffi/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
__ffi/c_export/call_in_node.js
Normal file
8
__ffi/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
__ffi/c_export/call_in_python.py
Normal file
5
__ffi/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
__ffi/c_export/src/lib.rs
Normal file
17
__ffi/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,
|
||||
}
|
||||
}
|
||||
161
__ffi/cpp/Cargo.lock
generated
Normal file
161
__ffi/cpp/Cargo.lock
generated
Normal file
@@ -0,0 +1,161 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "0.7.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "743ad5a418686aad3b87fd14c43badd828cf26e214a00f92a384291cf22e1811"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.50"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "95e28fa049fda1c330bcf9d723be7663a899c4679724b34c81e9f5a326aab8cd"
|
||||
|
||||
[[package]]
|
||||
name = "cpp"
|
||||
version = "0.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d0d94d130fd605c2df503d59a2784d8ecf8326d2311b50c848dff7bc7b1a61bd"
|
||||
dependencies = [
|
||||
"cpp_macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cpp_build"
|
||||
version = "0.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d7a67671b8ace8c2eeab3f6e00edd0bfab24026208fa5d039808d6b6efe4beb6"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"cpp_common",
|
||||
"lazy_static",
|
||||
"proc-macro2",
|
||||
"regex",
|
||||
"syn",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cpp_common"
|
||||
version = "0.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6b827ced56ffc05f4c918858040091d26f4b4176c670270be85538fe58d89923"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"proc-macro2",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cpp_macros"
|
||||
version = "0.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a5c116686c51a8e0b0e0ad5e85121be6a26f175d3193e3507e0b2d8bed65190d"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"byteorder",
|
||||
"cpp_common",
|
||||
"if_rust_version",
|
||||
"lazy_static",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "if_rust_version"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "46dbcb333e86939721589d25a3557e180b52778cb33c7fdfe9e0158ff790d5ec"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3197e20c7edb283f87c071ddfc7a2cca8f8e0b888c242959846a6fce03c72223"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3acb317c6ff86a4e579dfa00fc5e6cca91ecbb4e7eb2df0468805b674eb88548"
|
||||
dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "322cf97724bea3ee221b78fe25ac9c46114ebb51747ad5babd51a2fc6a8235a8"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
"thread_local",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.6.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b28dfe3fe9badec5dbf0a79a9cccad2cfc2ab5484bdb3e44cbd1ae8b3ba2be06"
|
||||
|
||||
[[package]]
|
||||
name = "rust_sample_cpp"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"cpp",
|
||||
"cpp_build",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af6f3550d8dff9ef7dc34d384ac6f107e5d31c8f57d9f28e0081503f547ac8f5"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread_local"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
|
||||
13
__ffi/cpp/Cargo.toml
Normal file
13
__ffi/cpp/Cargo.toml
Normal file
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "rust_sample_cpp"
|
||||
version = "0.1.0"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
build = "build.rs"
|
||||
|
||||
[dependencies]
|
||||
cpp = "0.5.4"
|
||||
|
||||
[build-dependencies]
|
||||
cpp_build = "0.5.4"
|
||||
|
||||
5
__ffi/cpp/build.rs
Normal file
5
__ffi/cpp/build.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
extern crate cpp_build;
|
||||
|
||||
fn main() {
|
||||
cpp_build::build("src/main.rs");
|
||||
}
|
||||
16
__ffi/cpp/src/main.rs
Normal file
16
__ffi/cpp/src/main.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
#[macro_use]
|
||||
extern crate cpp;
|
||||
|
||||
cpp!{{
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
}}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
cpp!([] {
|
||||
printf("Hello, World!\n");
|
||||
std::cout << "Hello, World2!" << std::endl;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user