package util /* #include #define Error const char* #define RustDataPtr void* typedef struct { unsigned int len; Error err; } ExecRustWriteResult_t; typedef ExecRustWriteResult_t (*RustWriteFunc) (RustDataPtr RustData, void* buff, unsigned int len); typedef void (*RustWritterDropFunc) (RustDataPtr RustData); static ExecRustWriteResult_t _exec_rust_write_func (RustWriteFunc funcptr, RustDataPtr RustData, void* buff, unsigned int len) { return (funcptr)(RustData, buff, len); } static void _exec_rust_writter_drop_func (RustWritterDropFunc funcptr, RustDataPtr RustData) { (funcptr)(RustData); } */ import ( "C" ) import ( "errors" "nosql_db_sdk_ffi/util/ptrproxy" "runtime" "unsafe" ) type RustIOWritter struct { wrapperPtr C.RustWriteFunc writterPtr RustWritterPtrGo dropFuncPtr C.RustWritterDropFunc } //export ffi_rust_IO_writter_new func ffi_rust_IO_writter_new(wrapperFuncPtr C.RustWriteFunc, writterPtr unsafe.Pointer, dropFuncPtr C.RustWritterDropFunc) unsafe.Pointer { rustIOWritter := RustIOWritter{ wrapperFuncPtr, RustWritterPtrGo(writterPtr), dropFuncPtr, } runtime.SetFinalizer(&rustIOWritter, RustIOWritterClose) return ptrproxy.Ref(unsafe.Pointer(&rustIOWritter)) } //export ffi_rust_IO_writter_drop func ffi_rust_IO_writter_drop(unsafe_writter_ptr unsafe.Pointer) { ptrproxy.Free(unsafe.Pointer(unsafe_writter_ptr)) } func RustIOWritterClose(obj *RustIOWritter) { C._exec_rust_writter_drop_func(obj.dropFuncPtr, unsafe.Pointer(obj.writterPtr)) } func (cw RustIOWritter) Write(p []byte) (n int, err error) { len := len(p) buff := C.CBytes(p) result := C._exec_rust_write_func(cw.wrapperPtr, unsafe.Pointer(cw.writterPtr), buff, C.uint(len)) C.free(buff) if result.err != nil { return int(result.len), nil } return 0, errors.New(C.GoString(result.err)) }