Struct toy_rpc::server::builder::ServerBuilder[][src]

pub struct ServerBuilder { /* fields omitted */ }
Expand description

Server builder

Implementations

Creates a new ServerBuilder

Registers a new service to the Server with the default name.

Internally the Service object will be built using the supplied service , which is the state of the Service object

Example

use std::sync::Arc;
use async_std::net::TcpListener;
use toy_rpc_macros::{export_impl};
use toy_rpc::server::Server;

struct EchoService { }

#[export_impl]
impl EchoService {
    #[export_method]
    async fn echo(&self, req: String) -> Result<String, String> {
        Ok(req)
    }
}

#[async_std::main]
async fn main() {
    let addr = "127.0.0.1:8080";
     
    let echo_service = Arc::new(EchoService { });
    let server = Server::builder()
        .register(echo_service)
        .build();
     
    let listener = TcpListener::bind(addr).await.unwrap();

    let handle = task::spawn(async move {
        server.accept(listener).await.unwrap();
    });

    handle.await;
}

Register a a service with a name. This allows registering multiple instances of the same type on the server.

Example

use std::sync::Arc;
use async_std::net::TcpListener;
use toy_rpc::macros::export_impl;
use toy_rpc::service::Service;
use toy_rpc::Server;
pub struct Foo { }

#[export_impl]
impl Foo {
    #[export_method]
    pub async fn increment(&self, arg: i32) -> Result<i32, String> {
        Ok(arg + 1)
    }
}

#[async_std::main]
async fn main() {
    let foo1 = Arc::new(Foo { });
    let foo2 = Arc::new(Foo { });

    // construct server
    let server = Server::builder()
        .register(foo1) // this will register `foo1` with the default name `Foo`
        .register_with_name("Foo2", foo2) // this will register `foo2` with the name `Foo2`
        .build();

    let addr = "127.0.0.1:8080";
    let listener = TcpListener::bind(addr).await.unwrap();

    let handle = task::spawn(async move {
        server.accept(listener).await.unwrap();
    });

    handle.await;
}

Register a Service instance. This allows registering multiple instances of the same type on the server.

Creates an RPC Server

Trait Implementations

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.