我正在嘗試建立從 go 呼叫 rust 函數的可能性,然後所述 rust 函數將函數回調到 go。我使用 cgo 作為 go 和 rust 之間的 ffi 介面。 以下是我的 go 程式碼(src/main.go):
package main import ( "c" "fmt" "unsafe" ) /* #cgo cflags: -i./../lib #cgo ldflags: -l./../bin -lgo_move -wl,-rpath=./bin #include "move.h" */ //export cosmoscallbackwrapper func cosmoscallbackwrapper(data *c.uchar, datalen c.int) { // convert data to go slice godata := c.gobytes(unsafe.pointer(data), datalen) // call the actual callback function cosmoscallback(godata) } // setcosmoscallback sets the callback function to be called by the move vm. func setcosmoscallback(callback func([]byte)) { cosmoscallback = callback c.set_cosmos_callback((c.cosmos_callback)(unsafe.pointer(c.cosmoscallbackwrapper))) } var cosmoscallback func([]byte) func main() { // create a new move interpreter // set the callback function setcosmoscallback(func(data []byte) { fmt.println("received data from move vm:", data) // handle data and call cosmos sdk functions as needed }) }
這是我的 rust 程式碼 (src/lib.rs)
use std::os::raw::{c_char, c_int}; use std::ffi::cstring; use std::sync::mutex; #[macro_use] extern crate lazy_static; pub fn main() { } pub type cosmoscallback = extern "c" fn(*const c_char, c_int); lazy_static! { static ref callback: mutex<option<cosmoscallback>> = mutex::new(none); } #[no_mangle] pub extern "c" fn set_cosmos_callback(callback: cosmoscallback) { let mut cb = callback.lock().unwrap(); *cb = some(callback); } #[no_mangle] pub extern "c" fn cosmoscallbackwrapper(data: *const c_char, data_len: c_int) { let cb = callback.lock().unwrap(); if let some(callback) = &*cb { callback(data, data_len); } }
這是我的 cargo.toml 檔案:
[package] name = "go-move" version = "0.1.0" edition = "2021" [lib] crate-type = ["cdylib"] [dependencies] libc = "0.2" lazy_static = "1.4.0" [workspace] members = [ ".",]
這是 lib/move.h(共享庫的頭檔):
#ifndef move_vm_lib_h #define move_vm_lib_h #include <stdint.h> //typedef void (*cosmos_callback)(const unsigned char *data, int data_len); //void set_cosmos_callback(cosmos_callback callback); typedef void (*cosmos_callback)(const uint8_t* data, int32_t data_len); void set_cosmos_callback(cosmos_callback callback); #endif
這是我的 makefile:
shell := /bin/bash .phony: build os = $(shell uname) clean: rm -rf bin rm -rf target build: @echo the os is $(os) mkdir bin cargo build --release ifeq ($(os),linux) cp target/release/libgo_move.so bin/ else cp target/release/libgo_move.dylib bin/ endif cp -a lib/. bin/ go build --ldflags="-l./bin -lgo_move" -o bin/main src/main.go run: export ld_library_path=./bin && ./main
檔案結構如下:
src main.go main.rs bin libgo_move.so (after cargo build) lib move.h cargo.toml makefile
運行 make clean build
後,我得到以下輸出:
cp target/release/libgo_move.so bin/ cp -a lib/. bin/ go build -o bin/main src/main.go # command-line-arguments src/main.go:27:59: could not determine kind of name for C.cosmosCallbackWrapper src/main.go:27:25: could not determine kind of name for C.cosmos_callback src/main.go:27:2: could not determine kind of name for C.set_cosmos_callback
由於某種原因,它找不到 ffi 函數。
正確答案
這是一個愚蠢的錯誤:
/* #cgo CFLAGS: -I./../lib #cgo LDFLAGS: -L./../bin -lgo_move -Wl,-rpath=./bin #include "move.h" */
必須在導入之前進行。僅此而已。
以上是從 Rust 到 Go 的回呼函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

golangisidealforperformance-Critical-clitageAppations and ConcurrentPrompromming,而毛皮刺激性,快速播種和可及性。 1)forhigh-porformanceneeds,pelectgolangduetoitsefefsefefseffifeficefsefeflicefsiveficefsiveandconcurrencyfeatures.2)fordataa-fordataa-fordata-fordata-driventriventriventriventriventrivendissp pynonnononesp

Golang通過goroutine和channel實現高效並發:1.goroutine是輕量級線程,使用go關鍵字啟動;2.channel用於goroutine間安全通信,避免競態條件;3.使用示例展示了基本和高級用法;4.常見錯誤包括死鎖和數據競爭,可用gorun-race檢測;5.性能優化建議減少channel使用,合理設置goroutine數量,使用sync.Pool管理內存。

Golang更適合系統編程和高並發應用,Python更適合數據科學和快速開發。 1)Golang由Google開發,靜態類型,強調簡潔性和高效性,適合高並發場景。 2)Python由GuidovanRossum創造,動態類型,語法簡潔,應用廣泛,適合初學者和數據處理。

Golang在性能和可擴展性方面優於Python。 1)Golang的編譯型特性和高效並發模型使其在高並發場景下表現出色。 2)Python作為解釋型語言,執行速度較慢,但通過工具如Cython可優化性能。

Go語言在並發編程、性能、學習曲線等方面有獨特優勢:1.並發編程通過goroutine和channel實現,輕量高效。 2.編譯速度快,運行性能接近C語言。 3.語法簡潔,學習曲線平緩,生態系統豐富。

Golang和Python的主要區別在於並發模型、類型系統、性能和執行速度。 1.Golang使用CSP模型,適用於高並發任務;Python依賴多線程和GIL,適合I/O密集型任務。 2.Golang是靜態類型,Python是動態類型。 3.Golang編譯型語言執行速度快,Python解釋型語言開發速度快。

Golang通常比C 慢,但Golang在並發編程和開發效率上更具優勢:1)Golang的垃圾回收和並發模型使其在高並發場景下表現出色;2)C 通過手動內存管理和硬件優化獲得更高性能,但開發複雜度較高。

Golang在雲計算和DevOps中的應用廣泛,其優勢在於簡單性、高效性和並發編程能力。 1)在雲計算中,Golang通過goroutine和channel機制高效處理並發請求。 2)在DevOps中,Golang的快速編譯和跨平台特性使其成為自動化工具的首選。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Dreamweaver Mac版
視覺化網頁開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。