Struct toy_rpc::client::Client[][src]

pub struct Client<T, Mode> { /* fields omitted */ }

RPC Client

Implementations

impl Client<Arc<Mutex<Box<dyn ClientCodec>>>, NotConnected>[src]

The following impl block is controlled by feature flag. It is enabled if and only if exactly one of the the following feature flag is turned on

  • serde_bincode
  • serde_json
  • serde_cbor
  • serde_rmp

pub fn with_stream(
    stream: TcpStream
) -> Client<Arc<Mutex<Box<dyn ClientCodec>>>, Connected>
[src]

Creates an RPC Client over socket with a specified async_std::net::TcpStream and the default codec

This is enabled if and only if exactly one of the the following feature flag is turned on

  • serde_bincode
  • serde_json
  • serde_cbor
  • serde_rmp

Example

use async_std::net::TcpStream;

#[async_std::main]
async fn main() {
    let stream = TcpStream::connect("127.0.0.1:8888").await.unwrap();
    let client = Client::with_stream(stream);
}

pub async fn dial(
    addr: impl ToSocketAddrs
) -> Result<Client<Arc<Mutex<Box<dyn ClientCodec>>>, Connected>, Error>
[src]

Connects the an RPC server over socket at the specified network address

This is enabled if and only if exactly one of the the following feature flag is turned on

  • serde_bincode
  • serde_json
  • serde_cbor
  • serde_rmp

Example

use toy_rpc::Client;

#[async_std::main]
async fn main() {
    let addr = "127.0.0.1";
    let client = Client::dial(addr).await;
}

impl Client<Arc<Mutex<Box<dyn ClientCodec>>>, NotConnected>[src]

pub fn with_codec<C>(
    codec: C
) -> Client<Arc<Mutex<Box<dyn ClientCodec>>>, Connected> where
    C: ClientCodec + Send + Sync + 'static, 
[src]

Creates an RPC 'Client` over socket with a specified codec

Example

use async_std::net::TcpStream;
use toy_rpc::codec::bincode::Codec;
use toy_rpc::Client;

#[async_std::main]
async fn main() {
    let addr = "127.0.0.1:8888";
    let stream = TcpStream::connect(addr).await.unwrap();
    let codec = Codec::new(stream);
    let client = Client::with_codec(codec);
}

impl Client<Arc<Mutex<Box<dyn ClientCodec>>>, Connected>[src]

pub fn call<Req, Res>(
    &self,
    service_method: impl ToString,
    args: Req
) -> Result<Res, Error> where
    Req: Serialize + Send + Sync,
    Res: DeserializeOwned
[src]

Invokes the named function and wait synchronously

This function internally calls task::block_on to wait for the response. Do NOT use this function inside another task::block_on.async_std

Example

use toy_rpc::client::Client;
use toy_rpc::error::Error;

#[async_std::main]
async fn main() {
    let addr = "127.0.0.1:8888";
    let client = Client::dial(addr).await.unwrap();

    let args = "arguments";
    let reply: Result<String, Error> = client.call("echo_service.echo", &args);
    println!("{:?}", reply);
}

pub fn spawn_task<Req, Res>(
    &self,
    service_method: impl ToString + Send + 'static,
    args: Req
) -> JoinHandle<Result<Res, Error>> where
    Req: Serialize + Send + Sync + 'static,
    Res: DeserializeOwned + Send + 'static, 
[src]

Invokes the named function asynchronously by spawning a new task and returns the JoinHandle

use async_std::task;

use toy_rpc::client::Client;
use toy_rpc::error::Error;

#[async_std::main]
async fn main() {
    let addr = "127.0.0.1:8888";
    let client = Client::dial(addr).await.unwrap();

    let args = "arguments";
    let handle: task::JoinHandle<Result<Res, Error>> = client.spawn_task("echo_service.echo", args);
    let reply: Result<String, Error> = handle.await;
    println!("{:?}", reply);
}

pub async fn async_call<Req, Res>(
    &self,
    service_method: impl ToString,
    args: Req
) -> Result<Res, Error> where
    Req: Serialize + Send + Sync,
    Res: DeserializeOwned
[src]

Invokes the named function asynchronously

Example

use async_std::task;

use toy_rpc::client::Client;
use toy_rpc::error::Error;

#[async_std::main]
async fn main() {
    let addr = "127.0.0.1:8888";
    let client = Client::dial(addr).await.unwrap();

    let args = "arguments";
    let reply: Result<String, Error> = client.async_call("echo_service.echo", &args).await;
    println!("{:?}", reply);
}

impl Client<Arc<Mutex<Box<dyn ClientCodec>>>, NotConnected>[src]

The following impl block is controlled by feature flag. It is enabled if and only if exactly one of the the following feature flag is turned on

  • serde_bincode
  • serde_json
  • serde_cbor
  • serde_rmp

pub async fn dial_http(
    addr: &'static str
) -> Result<Client<Arc<Mutex<Box<dyn ClientCodec>>>, Connected>, Error>
[src]

Connects to an HTTP RPC server at the specified network address using the defatul codec

If a network path were to be supplpied, the network path must end with a slash "/"

This is enabled if and only if exactly one of the the following feature flag is turned on

  • serde_bincode
  • serde_json
  • serde_cbor
  • serde_rmp

Example

use toy_rpc::client::Client;
use toy_rpc::error::Error;

#[async_std::main]
async fn main() {
    let addr = "http://127.0.0.1:8888/rpc/";
    let client = Client::dial_http(addr).await.unwrap();
}

TODO: check if the path ends with a slash TODO: try send and recv trait object

pub async fn dial_websocket(
    addr: &'static str
) -> Result<Client<Arc<Mutex<Box<dyn ClientCodec>>>, Connected>, Error>
[src]

Auto Trait Implementations

impl<T, Mode> !RefUnwindSafe for Client<T, Mode>[src]

impl<T, Mode> Send for Client<T, Mode> where
    Mode: Send,
    T: Send
[src]

impl<T, Mode> Sync for Client<T, Mode> where
    Mode: Sync,
    T: Sync
[src]

impl<T, Mode> Unpin for Client<T, Mode> where
    Mode: Unpin,
    T: Unpin
[src]

impl<T, Mode> !UnwindSafe for Client<T, Mode>[src]

Blanket Implementations

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

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

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

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

impl<T> Instrument for T[src]

impl<T> Instrument for T[src]

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

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.

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.

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