Skip to content

Commit

Permalink
feat: change go_ptr to go_pass_struct and upgrade major version
Browse files Browse the repository at this point in the history
  • Loading branch information
ihciah committed Dec 9, 2024
1 parent 64ea306 commit f4f1ffc
Show file tree
Hide file tree
Showing 16 changed files with 242 additions and 228 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/trait-attrs.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Now rust2go supports 3 attributes on trait's async function:
2. `#[drop_safe]`: this makes the function safe, but requires all paramters passing ownership.
3. `#[drop_safe_ret]`: to make the function safe, it requires passing ownership; this attribute allow users to get the paramters ownership back.
4. `#[mem]` or `#[shm]`: make this function implemented based on shared memory, whose performance is highly improved(but it requires linux now).
5. `#[go_ptr]`: make the generated go side code use pointer instead of value at parameters. This is useful when the parameter is large. This does not affect the rust side code.
5. `#[go_pass_struct]`: make the generated go side code use pointer instead of value at parameters. This is useful when the parameter is large. This does not affect the rust side code.

For example, here is the original trait:
```rust
Expand Down
44 changes: 19 additions & 25 deletions examples/example-monoio-mem/go/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ package main
#include <stdint.h>
#include <stdlib.h>
typedef struct StringRef {
const uint8_t *ptr;
uintptr_t len;
} StringRef;
typedef struct DemoResponseRef {
bool pass;
} DemoResponseRef;
typedef struct ListRef {
const void *ptr;
uintptr_t len;
Expand All @@ -27,21 +18,30 @@ typedef struct DemoComplicatedRequestRef {
struct ListRef balabala;
} DemoComplicatedRequestRef;
typedef struct DemoResponseRef {
bool pass;
} DemoResponseRef;
typedef struct StringRef {
const uint8_t *ptr;
uintptr_t len;
} StringRef;
typedef struct DemoUserRef {
struct StringRef name;
uint8_t age;
} DemoUserRef;
// hack from: https://stackoverflow.com/a/69904977
__attribute__((weak))
inline void DemoCall_demo_check_async_cb(const void *f_ptr, struct DemoResponseRef resp, const void *slot) {
((void (*)(struct DemoResponseRef, const void*))f_ptr)(resp, slot);
inline void DemoCall_demo_check_async_cb(const void *f_ptr, struct DemoResponseRef* resp, const void *slot) {
((void (*)(struct DemoResponseRef*, const void*))f_ptr)(resp, slot);
}
// hack from: https://stackoverflow.com/a/69904977
__attribute__((weak))
inline void DemoCall_demo_check_async_safe_cb(const void *f_ptr, struct DemoResponseRef resp, const void *slot) {
((void (*)(struct DemoResponseRef, const void*))f_ptr)(resp, slot);
inline void DemoCall_demo_check_async_safe_cb(const void *f_ptr, struct DemoResponseRef* resp, const void *slot) {
((void (*)(struct DemoResponseRef*, const void*))f_ptr)(resp, slot);
}
typedef struct QueueMeta {
Expand All @@ -57,11 +57,10 @@ typedef struct QueueMeta {
*/
import "C"
import (
"reflect"
"unsafe"

mem_ring "github.com/ihciah/rust2go/mem-ring"
"github.com/panjf2000/ants/v2"
"reflect"
"unsafe"
)

var DemoCallImpl DemoCall
Expand All @@ -79,27 +78,27 @@ func ringHandleDemoCall1(ptr unsafe.Pointer, pool *ants.MultiPool, post_func fun
req := *(*C.DemoComplicatedRequestRef)(ptr)
ptr = unsafe.Pointer(uintptr(ptr) + unsafe.Sizeof(req))
req_ := newDemoComplicatedRequest(req)
go func() {
pool.Submit(func() {
resp := DemoCallImpl.demo_check_async(req_)
resp_ref_size := uint(unsafe.Sizeof(C.DemoResponseRef{}))
resp_ref, buffer := cvt_ref_cap(cntDemoResponse, refDemoResponse, resp_ref_size)(&resp)
offset := uint(len(buffer))
buffer = append(buffer, unsafe.Slice((*byte)(unsafe.Pointer(&resp_ref)), resp_ref_size)...)
post_func(resp, buffer, offset)
}()
})
}
func ringHandleDemoCall2(ptr unsafe.Pointer, pool *ants.MultiPool, post_func func(interface{}, []byte, uint)) {
req := *(*C.DemoComplicatedRequestRef)(ptr)
ptr = unsafe.Pointer(uintptr(ptr) + unsafe.Sizeof(req))
req_ := newDemoComplicatedRequest(req)
go func() {
pool.Submit(func() {
resp := DemoCallImpl.demo_check_async_safe(req_)
resp_ref_size := uint(unsafe.Sizeof(C.DemoResponseRef{}))
resp_ref, buffer := cvt_ref_cap(cntDemoResponse, refDemoResponse, resp_ref_size)(&resp)
offset := uint(len(buffer))
buffer = append(buffer, unsafe.Slice((*byte)(unsafe.Pointer(&resp_ref)), resp_ref_size)...)
post_func(resp, buffer, offset)
}()
})
}

//export RingsInitDemoCall
Expand Down Expand Up @@ -410,10 +409,5 @@ func ringsInit(crr, crw C.QueueMeta, fns []func(unsafe.Pointer, *ants.MultiPool,
slab.Pop(p.UserData)
}
})
// , &mem_ring.SleepWaiter{
// Interval: time.Millisecond,
// Max: time.Millisecond * 100,
// }

}
func main() {}
47 changes: 26 additions & 21 deletions examples/example-monoio/go/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,32 @@ typedef struct DemoUserRef {
// hack from: https://stackoverflow.com/a/69904977
__attribute__((weak))
inline void DemoCall_demo_check_cb(const void *f_ptr, struct DemoResponseRef resp, const void *slot) {
((void (*)(struct DemoResponseRef, const void*))f_ptr)(resp, slot);
inline void DemoCall_demo_check_cb(const void *f_ptr, struct DemoResponseRef* resp, const void *slot) {
((void (*)(struct DemoResponseRef*, const void*))f_ptr)(resp, slot);
}
// hack from: https://stackoverflow.com/a/69904977
__attribute__((weak))
inline void DemoCall_demo_check_async_cb(const void *f_ptr, struct DemoResponseRef resp, const void *slot) {
((void (*)(struct DemoResponseRef, const void*))f_ptr)(resp, slot);
inline void DemoCall_demo_check_async_cb(const void *f_ptr, struct DemoResponseRef* resp, const void *slot) {
((void (*)(struct DemoResponseRef*, const void*))f_ptr)(resp, slot);
}
// hack from: https://stackoverflow.com/a/69904977
__attribute__((weak))
inline void DemoCall_demo_check_async_safe_cb(const void *f_ptr, struct DemoResponseRef resp, const void *slot) {
((void (*)(struct DemoResponseRef, const void*))f_ptr)(resp, slot);
inline void DemoCall_demo_check_async_safe_cb(const void *f_ptr, struct DemoResponseRef* resp, const void *slot) {
((void (*)(struct DemoResponseRef*, const void*))f_ptr)(resp, slot);
}
// hack from: https://stackoverflow.com/a/69904977
__attribute__((weak))
inline void DemoCall_demo_get_n_cb(const void *f_ptr, int32_t resp, const void *slot) {
((void (*)(int32_t, const void*))f_ptr)(resp, slot);
inline void DemoCall_demo_get_n_cb(const void *f_ptr, int32_t* resp, const void *slot) {
((void (*)(int32_t*, const void*))f_ptr)(resp, slot);
}
// hack from: https://stackoverflow.com/a/69904977
__attribute__((weak))
inline void DemoCall_demo_sum_cb(const void *f_ptr, int32_t resp, const void *slot) {
((void (*)(int32_t, const void*))f_ptr)(resp, slot);
inline void DemoCall_demo_sum_cb(const void *f_ptr, int32_t* resp, const void *slot) {
((void (*)(int32_t*, const void*))f_ptr)(resp, slot);
}
*/
import "C"
Expand All @@ -72,26 +72,27 @@ import (
var DemoCallImpl DemoCall

type DemoCall interface {
demo_oneway(req DemoUser)
demo_check(req DemoComplicatedRequest) DemoResponse
demo_oneway(req *DemoUser)
demo_check(req *DemoComplicatedRequest) DemoResponse
demo_check_async(req *DemoComplicatedRequest) DemoResponse
demo_check_async_safe(req DemoComplicatedRequest) DemoResponse
demo_check_async_safe(req *DemoComplicatedRequest) DemoResponse
demo_get_n() int32
demo_sum(a int32, b int32) int32
}

//export CDemoCall_demo_oneway
func CDemoCall_demo_oneway(req C.DemoUserRef) {
_new_req := newDemoUser(req)
DemoCallImpl.demo_oneway(_new_req)
DemoCallImpl.demo_oneway(&_new_req)
}

//export CDemoCall_demo_check
func CDemoCall_demo_check(req C.DemoComplicatedRequestRef, slot *C.void, cb *C.void) {
_new_req := newDemoComplicatedRequest(req)
resp := DemoCallImpl.demo_check(_new_req)
resp := DemoCallImpl.demo_check(&_new_req)
resp_ref, buffer := cvt_ref(cntDemoResponse, refDemoResponse)(&resp)
C.DemoCall_demo_check_cb(unsafe.Pointer(cb), resp_ref, unsafe.Pointer(slot))
C.DemoCall_demo_check_cb(unsafe.Pointer(cb), &resp_ref, unsafe.Pointer(slot))
runtime.KeepAlive(resp_ref)
runtime.KeepAlive(resp)
runtime.KeepAlive(buffer)
}
Expand All @@ -102,7 +103,8 @@ func CDemoCall_demo_check_async(req C.DemoComplicatedRequestRef, slot *C.void, c
go func() {
resp := DemoCallImpl.demo_check_async(&_new_req)
resp_ref, buffer := cvt_ref(cntDemoResponse, refDemoResponse)(&resp)
C.DemoCall_demo_check_async_cb(unsafe.Pointer(cb), resp_ref, unsafe.Pointer(slot))
C.DemoCall_demo_check_async_cb(unsafe.Pointer(cb), &resp_ref, unsafe.Pointer(slot))
runtime.KeepAlive(resp_ref)
runtime.KeepAlive(resp)
runtime.KeepAlive(buffer)
}()
Expand All @@ -112,9 +114,10 @@ func CDemoCall_demo_check_async(req C.DemoComplicatedRequestRef, slot *C.void, c
func CDemoCall_demo_check_async_safe(req C.DemoComplicatedRequestRef, slot *C.void, cb *C.void) {
_new_req := newDemoComplicatedRequest(req)
go func() {
resp := DemoCallImpl.demo_check_async_safe(_new_req)
resp := DemoCallImpl.demo_check_async_safe(&_new_req)
resp_ref, buffer := cvt_ref(cntDemoResponse, refDemoResponse)(&resp)
C.DemoCall_demo_check_async_safe_cb(unsafe.Pointer(cb), resp_ref, unsafe.Pointer(slot))
C.DemoCall_demo_check_async_safe_cb(unsafe.Pointer(cb), &resp_ref, unsafe.Pointer(slot))
runtime.KeepAlive(resp_ref)
runtime.KeepAlive(resp)
runtime.KeepAlive(buffer)
}()
Expand All @@ -124,7 +127,8 @@ func CDemoCall_demo_check_async_safe(req C.DemoComplicatedRequestRef, slot *C.vo
func CDemoCall_demo_get_n(slot *C.void, cb *C.void) {
resp := DemoCallImpl.demo_get_n()
resp_ref, buffer := cvt_ref(cntC_int32_t, refC_int32_t)(&resp)
C.DemoCall_demo_get_n_cb(unsafe.Pointer(cb), resp_ref, unsafe.Pointer(slot))
C.DemoCall_demo_get_n_cb(unsafe.Pointer(cb), &resp_ref, unsafe.Pointer(slot))
runtime.KeepAlive(resp_ref)
runtime.KeepAlive(resp)
runtime.KeepAlive(buffer)
}
Expand All @@ -135,7 +139,8 @@ func CDemoCall_demo_sum(a C.int32_t, b C.int32_t, slot *C.void, cb *C.void) {
_new_b := newC_int32_t(b)
resp := DemoCallImpl.demo_sum(_new_a, _new_b)
resp_ref, buffer := cvt_ref(cntC_int32_t, refC_int32_t)(&resp)
C.DemoCall_demo_sum_cb(unsafe.Pointer(cb), resp_ref, unsafe.Pointer(slot))
C.DemoCall_demo_sum_cb(unsafe.Pointer(cb), &resp_ref, unsafe.Pointer(slot))
runtime.KeepAlive(resp_ref)
runtime.KeepAlive(resp)
runtime.KeepAlive(buffer)
}
Expand Down
6 changes: 3 additions & 3 deletions examples/example-monoio/go/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ func init() {
DemoCallImpl = Demo{}
}

func (Demo) demo_oneway(req DemoUser) {
func (Demo) demo_oneway(req *DemoUser) {
fmt.Printf("[Go-oneway] Golang received name: %s, age: %d\n", req.name, req.age)
}

func (Demo) demo_check(req DemoComplicatedRequest) DemoResponse {
func (Demo) demo_check(req *DemoComplicatedRequest) DemoResponse {
fmt.Printf("[Go-call] Golang received req: %d users\n", len(req.users))
fmt.Printf("[Go-call] Golang returned result\n")
return DemoResponse{pass: true}
Expand All @@ -28,7 +28,7 @@ func (Demo) demo_check_async(req *DemoComplicatedRequest) DemoResponse {
return DemoResponse{pass: true}
}

func (Demo) demo_check_async_safe(req DemoComplicatedRequest) DemoResponse {
func (Demo) demo_check_async_safe(req *DemoComplicatedRequest) DemoResponse {
fmt.Printf("[Go-call async drop_safe] Golang received req, will sleep 1s\n")
time.Sleep(1 * time.Second)
resp := DemoResponse{pass: req.balabala[0] == 1}
Expand Down
2 changes: 1 addition & 1 deletion examples/example-monoio/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ pub struct DemoResponse {
pub trait DemoCall {
fn demo_oneway(req: &DemoUser);
fn demo_check(req: &DemoComplicatedRequest) -> DemoResponse;
#[go_ptr]
fn demo_check_async(
req: &DemoComplicatedRequest,
) -> impl std::future::Future<Output = DemoResponse>;
Expand All @@ -59,5 +58,6 @@ pub trait DemoCall {
req: DemoComplicatedRequest,
) -> impl std::future::Future<Output = DemoResponse>;
fn demo_get_n() -> i32;
#[go_pass_struct]
fn demo_sum(a: i32, b: i32) -> i32;
}
Loading

0 comments on commit f4f1ffc

Please sign in to comment.