Skip to content

Latest commit

 

History

History
49 lines (43 loc) · 1.8 KB

trait-attrs.md

File metadata and controls

49 lines (43 loc) · 1.8 KB
title date author
Use attribute to control code generation
2023-12-22 07:12:00 -0800
ihciah

Now rust2go supports 3 attributes on trait's async function:

  1. #[send]: the function will be generated as impl Future<Output=..> + Send + Sync.
  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).

For example, here is the original trait:

#[rust2go::r2g]
pub trait DemoCall {
    #[send]
    fn demo_check_async(
        req: &DemoComplicatedRequest,
    ) -> impl std::future::Future<Output = DemoResponse>;
    #[drop_safe]
    fn demo_check_async_safe(
        req: DemoComplicatedRequest,
    ) -> impl std::future::Future<Output = DemoResponse>;
    #[drop_safe_ret]
    fn demo_check_async_safe_with_ret(
        req: DemoComplicatedRequest,
    ) -> impl std::future::Future<Output = DemoResponse>;
}

Here is the generated trait:

pub trait DemoCall {
    unsafe fn demo_check_async(
        req: &DemoComplicatedRequest,
    ) -> impl ::std::future::Future<Output = DemoResponse> + Send + Sync;

    fn demo_check_async_safe(
        req: DemoComplicatedRequest,
    ) -> impl ::std::future::Future<Output = DemoResponse> + 'static;

    fn demo_check_async_safe_with_ret(
        req: DemoComplicatedRequest,
    ) -> impl ::std::future::Future<Output = (DemoResponse, (DemoComplicatedRequest,))> + 'static;
}

Note, if all parameters are with ownership, the generated impl Future will be added with a 'static lifetime automatically. This is useful for spawning tasks.