Struct toy_rpc::server::ServerBuilder [−][src]
pub struct ServerBuilder { /* fields omitted */ }
Expand description
Server builder
Implementations
impl ServerBuilder
[src]
impl ServerBuilder
[src]pub fn register<S>(self, service: Arc<S>) -> Self where
S: RegisterService + Send + Sync + 'static,
[src]
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]
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; }
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
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[src]
pub fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<T> Instrument for T
[src]
impl<T> Instrument for T
[src]fn instrument(self, span: Span) -> Instrumented<Self>
[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]
fn in_current_span(self) -> Instrumented<Self>
[src]impl<T> Instrument for T
[src]
impl<T> Instrument for T
[src]fn instrument(self, span: Span) -> Instrumented<Self>
[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]
fn in_current_span(self) -> Instrumented<Self>
[src]impl<T> Same<T> for T
impl<T> Same<T> for T
type Output = T
type Output = T
Should always be Self
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,