├── .gitignore ├── Makefile ├── README.md ├── lib ├── Cargo.lock ├── Cargo.toml ├── build.rs ├── cbindgen.toml ├── hyper.h ├── libhyper.so └── src │ └── lib.rs └── main.go /.gitignore: -------------------------------------------------------------------------------- 1 | lib/target/ 2 | main 3 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ROOT_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) 2 | 3 | .PHONY: clear build run 4 | 5 | clear: 6 | rm -rf lib/target 7 | rm -f lib/libhyper.so 8 | rm -f lib/hyper.h 9 | 10 | build: 11 | clear 12 | cd lib/ && cargo build --release 13 | cp lib/target/release/libhyper.so lib/ 14 | go build -ldflags="-r $(ROOT_DIR)lib" main.go 15 | 16 | run: clear build 17 | ./main 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Golang调用Rust代码 2 | 3 | 将rust中组合子示例函数封装成动态库,并在golang中调用 4 | 5 | ## 1 Rust 生成c语言头文件和动态库 6 | 7 | 为 Rust 库编写[FFI](https://doc.rust-lang.org/nomicon/ffi.html)(Foreign Function Interface) 8 | 9 | ### 包装Rust库 10 | 11 | 由于rust类型无法直接导出成c语言识别的数据类型, 因此需要将数据类型和函数声明重新包装 12 | 13 | ```Rust 14 | #[repr(C)] 15 | pub struct PairStr{ 16 | left : *const c_char, 17 | right : *const c_char, 18 | } 19 | 20 | #[no_mangle] 21 | pub extern fn space_find_export(input: *const c_char) -> PairStr { 22 | 23 | } 24 | ``` 25 | 26 | ### 使用cbindgen自动导出h文件 27 | 28 | 使用[cbindgen](https://github.com/eqrion/cbindgen)工具根据rust代码自动生成c语言头文件 29 | > cbindgen has a simple but effective strategy. It walks through your crate looking for: 30 | 1、#[no_mangle] pub extern fn ("functions") 31 | 2、#[no_mangle] pub static ("globals") 32 | 3、pub const ("constants") 33 | 34 | ### 使用步骤 35 | 36 | 1. yum install clang 37 | 2. cargo install --force cbindgen 38 | 3. 在Cargo.toml中添加依赖 39 | 40 | ```shell 41 | [build-dependencies] 42 | cbindgen = "0.12.2" 43 | ``` 44 | 45 | 4. 在项目中添加cbindgen.toml文件 46 | 47 | ```shell 48 | include_guard = "HYPER_H" 49 | autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */" 50 | language = "C" 51 | ``` 52 | 53 | 5. 添加build.rs脚本,用于生成c语言头文件hyper.h, 执行cargo build命令,Rust的OUT_DIR将会包含hyper.h 54 | 55 | ## 2 Go 调用 C 56 | 57 | go语言自带[cgo](https://chai2010.cn/advanced-go-programming-book/ch2-cgo/readme.html)工具支持c语言的函数调用,但需要安装gcc,并且开启CGO_ENABLED 58 | 在go的源代码中把C代码作为注释来写,并标明依赖的库文件和路径,最后使用import "C" 59 | 60 | ```golang 61 | import "C" 62 | /* 63 | #cgo LDFLAGS: -L./lib -lhyper 64 | #include "./lib/hyper.h" 65 | */ 66 | func main() { 67 | C.space_find_export(C.CString("hello world")) 68 | } 69 | ``` 70 | 71 | ## 3 运行 72 | 73 | 编译rust程序 + 生成c头文件和动态库 + 编译运行go程序 74 | 75 | ```shell 76 | make run 77 | ``` 78 | 79 | ## 注意事项 80 | 81 | 1. Rust中所有权问题 82 | 2. 内存安全, Go语言和C语言内存模型存在差异 83 | -------------------------------------------------------------------------------- /lib/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "ansi_term" 5 | version = "0.11.0" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | dependencies = [ 8 | "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", 9 | ] 10 | 11 | [[package]] 12 | name = "arrayvec" 13 | version = "0.4.12" 14 | source = "registry+https://github.com/rust-lang/crates.io-index" 15 | dependencies = [ 16 | "nodrop 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)", 17 | ] 18 | 19 | [[package]] 20 | name = "atty" 21 | version = "0.2.14" 22 | source = "registry+https://github.com/rust-lang/crates.io-index" 23 | dependencies = [ 24 | "hermit-abi 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", 25 | "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", 26 | "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", 27 | ] 28 | 29 | [[package]] 30 | name = "bitflags" 31 | version = "1.2.1" 32 | source = "registry+https://github.com/rust-lang/crates.io-index" 33 | 34 | [[package]] 35 | name = "c2-chacha" 36 | version = "0.2.3" 37 | source = "registry+https://github.com/rust-lang/crates.io-index" 38 | dependencies = [ 39 | "ppv-lite86 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", 40 | ] 41 | 42 | [[package]] 43 | name = "cbindgen" 44 | version = "0.12.2" 45 | source = "registry+https://github.com/rust-lang/crates.io-index" 46 | dependencies = [ 47 | "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", 48 | "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", 49 | "proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", 50 | "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", 51 | "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", 52 | "serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)", 53 | "syn 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", 54 | "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 55 | "toml 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", 56 | ] 57 | 58 | [[package]] 59 | name = "cfg-if" 60 | version = "0.1.10" 61 | source = "registry+https://github.com/rust-lang/crates.io-index" 62 | 63 | [[package]] 64 | name = "clap" 65 | version = "2.33.0" 66 | source = "registry+https://github.com/rust-lang/crates.io-index" 67 | dependencies = [ 68 | "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", 69 | "atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", 70 | "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 71 | "strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", 72 | "textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", 73 | "unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", 74 | "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", 75 | ] 76 | 77 | [[package]] 78 | name = "getrandom" 79 | version = "0.1.14" 80 | source = "registry+https://github.com/rust-lang/crates.io-index" 81 | dependencies = [ 82 | "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 83 | "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", 84 | "wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)", 85 | ] 86 | 87 | [[package]] 88 | name = "hermit-abi" 89 | version = "0.1.6" 90 | source = "registry+https://github.com/rust-lang/crates.io-index" 91 | dependencies = [ 92 | "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", 93 | ] 94 | 95 | [[package]] 96 | name = "hyper" 97 | version = "0.1.0" 98 | dependencies = [ 99 | "cbindgen 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)", 100 | "nom 5.1.0 (registry+https://github.com/rust-lang/crates.io-index)", 101 | "toml 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", 102 | ] 103 | 104 | [[package]] 105 | name = "itoa" 106 | version = "0.4.4" 107 | source = "registry+https://github.com/rust-lang/crates.io-index" 108 | 109 | [[package]] 110 | name = "lexical-core" 111 | version = "0.4.6" 112 | source = "registry+https://github.com/rust-lang/crates.io-index" 113 | dependencies = [ 114 | "arrayvec 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", 115 | "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 116 | "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", 117 | "ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", 118 | "static_assertions 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", 119 | ] 120 | 121 | [[package]] 122 | name = "libc" 123 | version = "0.2.66" 124 | source = "registry+https://github.com/rust-lang/crates.io-index" 125 | 126 | [[package]] 127 | name = "log" 128 | version = "0.4.8" 129 | source = "registry+https://github.com/rust-lang/crates.io-index" 130 | dependencies = [ 131 | "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 132 | ] 133 | 134 | [[package]] 135 | name = "memchr" 136 | version = "2.3.0" 137 | source = "registry+https://github.com/rust-lang/crates.io-index" 138 | 139 | [[package]] 140 | name = "nodrop" 141 | version = "0.1.14" 142 | source = "registry+https://github.com/rust-lang/crates.io-index" 143 | 144 | [[package]] 145 | name = "nom" 146 | version = "5.1.0" 147 | source = "registry+https://github.com/rust-lang/crates.io-index" 148 | dependencies = [ 149 | "lexical-core 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", 150 | "memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", 151 | "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", 152 | ] 153 | 154 | [[package]] 155 | name = "ppv-lite86" 156 | version = "0.2.6" 157 | source = "registry+https://github.com/rust-lang/crates.io-index" 158 | 159 | [[package]] 160 | name = "proc-macro2" 161 | version = "1.0.8" 162 | source = "registry+https://github.com/rust-lang/crates.io-index" 163 | dependencies = [ 164 | "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 165 | ] 166 | 167 | [[package]] 168 | name = "quote" 169 | version = "1.0.2" 170 | source = "registry+https://github.com/rust-lang/crates.io-index" 171 | dependencies = [ 172 | "proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", 173 | ] 174 | 175 | [[package]] 176 | name = "rand" 177 | version = "0.7.3" 178 | source = "registry+https://github.com/rust-lang/crates.io-index" 179 | dependencies = [ 180 | "getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)", 181 | "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", 182 | "rand_chacha 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", 183 | "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", 184 | "rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 185 | ] 186 | 187 | [[package]] 188 | name = "rand_chacha" 189 | version = "0.2.1" 190 | source = "registry+https://github.com/rust-lang/crates.io-index" 191 | dependencies = [ 192 | "c2-chacha 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", 193 | "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", 194 | ] 195 | 196 | [[package]] 197 | name = "rand_core" 198 | version = "0.5.1" 199 | source = "registry+https://github.com/rust-lang/crates.io-index" 200 | dependencies = [ 201 | "getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)", 202 | ] 203 | 204 | [[package]] 205 | name = "rand_hc" 206 | version = "0.2.0" 207 | source = "registry+https://github.com/rust-lang/crates.io-index" 208 | dependencies = [ 209 | "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", 210 | ] 211 | 212 | [[package]] 213 | name = "redox_syscall" 214 | version = "0.1.56" 215 | source = "registry+https://github.com/rust-lang/crates.io-index" 216 | 217 | [[package]] 218 | name = "remove_dir_all" 219 | version = "0.5.2" 220 | source = "registry+https://github.com/rust-lang/crates.io-index" 221 | dependencies = [ 222 | "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", 223 | ] 224 | 225 | [[package]] 226 | name = "rustc_version" 227 | version = "0.2.3" 228 | source = "registry+https://github.com/rust-lang/crates.io-index" 229 | dependencies = [ 230 | "semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", 231 | ] 232 | 233 | [[package]] 234 | name = "ryu" 235 | version = "1.0.2" 236 | source = "registry+https://github.com/rust-lang/crates.io-index" 237 | 238 | [[package]] 239 | name = "semver" 240 | version = "0.9.0" 241 | source = "registry+https://github.com/rust-lang/crates.io-index" 242 | dependencies = [ 243 | "semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", 244 | ] 245 | 246 | [[package]] 247 | name = "semver-parser" 248 | version = "0.7.0" 249 | source = "registry+https://github.com/rust-lang/crates.io-index" 250 | 251 | [[package]] 252 | name = "serde" 253 | version = "1.0.104" 254 | source = "registry+https://github.com/rust-lang/crates.io-index" 255 | dependencies = [ 256 | "serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", 257 | ] 258 | 259 | [[package]] 260 | name = "serde_derive" 261 | version = "1.0.104" 262 | source = "registry+https://github.com/rust-lang/crates.io-index" 263 | dependencies = [ 264 | "proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", 265 | "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", 266 | "syn 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", 267 | ] 268 | 269 | [[package]] 270 | name = "serde_json" 271 | version = "1.0.44" 272 | source = "registry+https://github.com/rust-lang/crates.io-index" 273 | dependencies = [ 274 | "itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", 275 | "ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", 276 | "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", 277 | ] 278 | 279 | [[package]] 280 | name = "static_assertions" 281 | version = "0.3.4" 282 | source = "registry+https://github.com/rust-lang/crates.io-index" 283 | 284 | [[package]] 285 | name = "strsim" 286 | version = "0.8.0" 287 | source = "registry+https://github.com/rust-lang/crates.io-index" 288 | 289 | [[package]] 290 | name = "syn" 291 | version = "1.0.13" 292 | source = "registry+https://github.com/rust-lang/crates.io-index" 293 | dependencies = [ 294 | "proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", 295 | "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", 296 | "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", 297 | ] 298 | 299 | [[package]] 300 | name = "tempfile" 301 | version = "3.1.0" 302 | source = "registry+https://github.com/rust-lang/crates.io-index" 303 | dependencies = [ 304 | "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", 305 | "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", 306 | "rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", 307 | "redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)", 308 | "remove_dir_all 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", 309 | "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", 310 | ] 311 | 312 | [[package]] 313 | name = "textwrap" 314 | version = "0.11.0" 315 | source = "registry+https://github.com/rust-lang/crates.io-index" 316 | dependencies = [ 317 | "unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", 318 | ] 319 | 320 | [[package]] 321 | name = "toml" 322 | version = "0.5.6" 323 | source = "registry+https://github.com/rust-lang/crates.io-index" 324 | dependencies = [ 325 | "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", 326 | ] 327 | 328 | [[package]] 329 | name = "unicode-width" 330 | version = "0.1.7" 331 | source = "registry+https://github.com/rust-lang/crates.io-index" 332 | 333 | [[package]] 334 | name = "unicode-xid" 335 | version = "0.2.0" 336 | source = "registry+https://github.com/rust-lang/crates.io-index" 337 | 338 | [[package]] 339 | name = "vec_map" 340 | version = "0.8.1" 341 | source = "registry+https://github.com/rust-lang/crates.io-index" 342 | 343 | [[package]] 344 | name = "version_check" 345 | version = "0.1.5" 346 | source = "registry+https://github.com/rust-lang/crates.io-index" 347 | 348 | [[package]] 349 | name = "wasi" 350 | version = "0.9.0+wasi-snapshot-preview1" 351 | source = "registry+https://github.com/rust-lang/crates.io-index" 352 | 353 | [[package]] 354 | name = "winapi" 355 | version = "0.3.8" 356 | source = "registry+https://github.com/rust-lang/crates.io-index" 357 | dependencies = [ 358 | "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 359 | "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 360 | ] 361 | 362 | [[package]] 363 | name = "winapi-i686-pc-windows-gnu" 364 | version = "0.4.0" 365 | source = "registry+https://github.com/rust-lang/crates.io-index" 366 | 367 | [[package]] 368 | name = "winapi-x86_64-pc-windows-gnu" 369 | version = "0.4.0" 370 | source = "registry+https://github.com/rust-lang/crates.io-index" 371 | 372 | [metadata] 373 | "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" 374 | "checksum arrayvec 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "cd9fd44efafa8690358b7408d253adf110036b88f55672a933f01d616ad9b1b9" 375 | "checksum atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" 376 | "checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" 377 | "checksum c2-chacha 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "214238caa1bf3a496ec3392968969cab8549f96ff30652c9e56885329315f6bb" 378 | "checksum cbindgen 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1595ec41c4ad8109ad17eb2e38fa52af1244b2321c02144176b5aef0655d5f8e" 379 | "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" 380 | "checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" 381 | "checksum getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" 382 | "checksum hermit-abi 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eff2656d88f158ce120947499e971d743c05dbcbed62e5bd2f38f1698bbc3772" 383 | "checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f" 384 | "checksum lexical-core 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2304bccb228c4b020f3a4835d247df0a02a7c4686098d4167762cfbbe4c5cb14" 385 | "checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558" 386 | "checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" 387 | "checksum memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3197e20c7edb283f87c071ddfc7a2cca8f8e0b888c242959846a6fce03c72223" 388 | "checksum nodrop 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" 389 | "checksum nom 5.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c433f4d505fe6ce7ff78523d2fa13a0b9f2690e181fc26168bcbe5ccc5d14e07" 390 | "checksum ppv-lite86 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b" 391 | "checksum proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "3acb317c6ff86a4e579dfa00fc5e6cca91ecbb4e7eb2df0468805b674eb88548" 392 | "checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" 393 | "checksum rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" 394 | "checksum rand_chacha 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "03a2a90da8c7523f554344f921aa97283eadf6ac484a6d2a7d0212fa7f8d6853" 395 | "checksum rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" 396 | "checksum rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" 397 | "checksum redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)" = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" 398 | "checksum remove_dir_all 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4a83fa3702a688b9359eccba92d153ac33fd2e8462f9e0e3fdf155239ea7792e" 399 | "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" 400 | "checksum ryu 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "bfa8506c1de11c9c4e4c38863ccbe02a305c8188e85a05a784c9e11e1c3910c8" 401 | "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" 402 | "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" 403 | "checksum serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "414115f25f818d7dfccec8ee535d76949ae78584fc4f79a6f45a904bf8ab4449" 404 | "checksum serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "128f9e303a5a29922045a830221b8f78ec74a5f544944f3d5984f8ec3895ef64" 405 | "checksum serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)" = "48c575e0cc52bdd09b47f330f646cf59afc586e9c4e3ccd6fc1f625b8ea1dad7" 406 | "checksum static_assertions 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7f3eb36b47e512f8f1c9e3d10c2c1965bc992bd9cdb024fa581e2194501c83d3" 407 | "checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" 408 | "checksum syn 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1e4ff033220a41d1a57d8125eab57bf5263783dfdcc18688b1dacc6ce9651ef8" 409 | "checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" 410 | "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" 411 | "checksum toml 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ffc92d160b1eef40665be3a05630d003936a3bc7da7421277846c2613e92c71a" 412 | "checksum unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" 413 | "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" 414 | "checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" 415 | "checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" 416 | "checksum wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" 417 | "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" 418 | "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 419 | "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 420 | -------------------------------------------------------------------------------- /lib/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "hyper" 3 | version = "0.1.0" 4 | authors = ["luopan"] 5 | 6 | [lib] 7 | name = "hyper" 8 | path = "src/lib.rs" 9 | crate-type = ["cdylib"] 10 | 11 | [build-dependencies] 12 | cbindgen = "0.12.2" 13 | 14 | [dependencies] 15 | nom = "5" 16 | toml = "0.5.5" -------------------------------------------------------------------------------- /lib/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let crate_dir = env::var("CARGO_MANIFEST_DIR") 5 | .expect("CARGO_MANIFEST_DIR env var is not defined"); 6 | let config = cbindgen::Config::from_file("cbindgen.toml") 7 | .expect("Unable to find cbindgen.toml configuration file"); 8 | cbindgen::generate_with_config(&crate_dir, config) 9 | .unwrap() 10 | .write_to_file("./hyper.h"); 11 | } -------------------------------------------------------------------------------- /lib/cbindgen.toml: -------------------------------------------------------------------------------- 1 | include_guard = "HYPER_H" 2 | autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */" 3 | language = "C" -------------------------------------------------------------------------------- /lib/hyper.h: -------------------------------------------------------------------------------- 1 | #ifndef HYPER_H 2 | #define HYPER_H 3 | 4 | /* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | typedef struct { 12 | const char *left; 13 | const char *right; 14 | } PairStr; 15 | 16 | PairStr space_find_export(const char *input); 17 | 18 | #endif /* HYPER_H */ 19 | -------------------------------------------------------------------------------- /lib/libhyper.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/loloxiaoz/rust-cgo/4f5bda4a1b4122d8cf461fc22fb416510c70b491/lib/libhyper.so -------------------------------------------------------------------------------- /lib/src/lib.rs: -------------------------------------------------------------------------------- 1 | extern crate nom; 2 | use nom::{ 3 | bytes::complete::{is_not}, 4 | combinator::{opt}, 5 | IResult, 6 | }; 7 | use std::os::raw::c_char; 8 | use std::ffi::{CString, CStr}; 9 | 10 | #[repr(C)] 11 | pub struct PairStr{ 12 | left : *const c_char, 13 | right : *const c_char, 14 | } 15 | 16 | pub fn space_find(input: &str) -> IResult<&str, &str> { 17 | let (char, found) = opt(is_not(" "))(input)?; 18 | if let Some(left) = found { 19 | return Ok((char.trim(), left)); 20 | } 21 | Ok((input, "")) 22 | } 23 | 24 | #[no_mangle] 25 | pub extern fn space_find_export(input: *const c_char) -> PairStr { 26 | unsafe { 27 | let input_str: &str= CStr::from_ptr(input).to_str().unwrap(); 28 | let (x, y) = space_find(input_str).unwrap(); 29 | PairStr{ 30 | left : CString::new(x).unwrap().into_raw(), 31 | right : CString::new(y).unwrap().into_raw(), 32 | } 33 | } 34 | } 35 | 36 | 37 | #[cfg(test)] 38 | mod tests { 39 | use super::*; 40 | 41 | #[test] 42 | fn test_space_find() { 43 | let input_str = b"hello world\0"; 44 | let input: *const c_char = input_str.as_ptr() as *const c_char; 45 | let ret = space_find_export(input); 46 | unsafe { 47 | assert_eq!(CStr::from_ptr(ret.left).to_str().unwrap(), "world"); 48 | assert_eq!(CStr::from_ptr(ret.right).to_str().unwrap(), "hello"); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | /* 4 | #cgo LDFLAGS: -L./lib -lhyper 5 | #include "./lib/hyper.h" 6 | */ 7 | import "C" 8 | 9 | import ( 10 | "fmt" 11 | ) 12 | 13 | func main() { 14 | ret := C.space_find_export(C.CString("hello world")) 15 | fmt.Println(C.GoString(ret.left)) 16 | fmt.Println(C.GoString(ret.right)) 17 | } 18 | --------------------------------------------------------------------------------