Struct toy_rpc::server::builder::ServerBuilder [−][src]
pub struct ServerBuilder { /* fields omitted */ }
Expand description
Server builder
Implementations
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]
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; }
Register a Service
instance. This allows registering multiple instances
of the same type on the server.
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for ServerBuilder
impl Send for ServerBuilder
impl Sync for ServerBuilder
impl Unpin for ServerBuilder
impl !UnwindSafe for ServerBuilder
Blanket Implementations
Mutably borrows from an owned value. Read more
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
type Output = T
type Output = T
Should always be Self
pub fn vzip(self) -> V