Attribute Macro toy_rpc::macros::export_impl[][src]

#[export_impl]

Export methods in the impl block with #[export_method] attribute. Methods without the attribute will not be affected. This will also generate client stub.

When using with #[async_trait], place #[async_trait] before #[export_macro].

Example - Export impl block

struct ExampleService { }

#[export_impl]
impl ExampleService {
    #[export_method]
    async fn exported_method(&self, args: ()) -> Result<String, String> {
        Ok("This is an exported method".to_string())
    }

    async fn not_exported_method(&self, args: ()) -> Result<String, String> {
        Ok("This method is NOT exported".to_string())
    }
}

Example - use client stub

mod rpc {
    // service state
    pub struct Foo { 
        pub id: i32
    }       
 
    // service impl
    #[export_impl] 
    impl Foo {
        pub async fn get_id(&self, _: ()) -> Result<i32, String> {
            Ok(self.id)
        }
    }
}
 
use toy_rpc::Client;
use rpc::*;
 
#[async_std::main]
async fn main() {
    let addr = "127.0.0.1:23333";
    let client = Client::dial(addr).await.unwrap();
 
    // assume the `Foo` service is registered as "foo_service" 
    // on the server
    let reply = client.foo("foo_service").get_id(()).await.unwrap();
}