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

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

Server builder

Implementations

impl ServerBuilder[src]

pub fn new() -> Self[src]

Creates a new ServerBuilder

pub fn register<S>(self, service: Arc<S>) -> Self where
    S: RegisterService + Send + Sync + 'static, 
[src]

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;
}

pub fn register_with_name<S>(self, name: &'static str, service: Arc<S>) -> Self where
    S: RegisterService + Send + Sync + 'static, 
[src]

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;
}

pub fn register_service<S>(
    self,
    name: &'static str,
    service: Service<S>
) -> Self where
    S: Send + Sync + 'static, 
[src]

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

pub fn build(self) -> Server[src]

Creates an RPC Server

Trait Implementations

impl Default for ServerBuilder[src]

fn default() -> Self[src]

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

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T> Instrument for T[src]

fn instrument(self, span: Span) -> Instrumented<Self>[src]

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

fn in_current_span(self) -> Instrumented<Self>[src]

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

impl<T> Instrument for T[src]

fn instrument(self, span: Span) -> Instrumented<Self>[src]

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

fn in_current_span(self) -> Instrumented<Self>[src]

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

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

pub fn vzip(self) -> V