Client

Struct Client 

Source
pub struct Client { /* private fields */ }
Expand description

An RPC client This is the main struct that should be used for implementing an RPC client.

Implementations§

Source§

impl Client

Source

pub fn new(addr: &str) -> Result<Self, Error>

Creates a new client that connects to an RPC server

§Arguments:
  • addr The address the TCP client should connect to this should be in the form “host:port”
§Example:
use rpc_toy::Client;
let client = Client::new("127.0.0.1:3001");
Examples found in repository?
examples/client.rs (line 6)
5pub fn main() {
6    let mut client = Client::new("127.0.0.1:3001").unwrap();
7    let one = serde_json::to_value(1u32).unwrap();
8    let two = serde_json::to_value(2u32).unwrap();
9    let args = vec![one, two];
10    let res = client.call("Add", &args).unwrap();
11    let three: u32 = serde_json::from_value(res.unwrap()).unwrap();
12    assert_eq!(three, 3);
13    let world = client.call("hello", &vec![]).unwrap();
14    println!(
15        "Hello, {}!",
16        serde_json::from_value::<String>(world.unwrap()).unwrap()
17    );
18    let nothing = client.call("void_fn", &vec![]).unwrap();
19    assert!(nothing.is_none());
20}
Source

pub fn call( &mut self, fn_name: &str, args: &[Value], ) -> Result<Option<Value>, Error>

Invokes an RPC, this is the mechanism to “call” functions on a remote server

§Arguments:
  • fn_name: The name of the function to call NOTE: The server MUST have registered this function, otherwise (currently) expect weird stuff to happen :)
  • args a slice of serde_json::Values. This represents the arguments that will be passed onto the server’s functions
§Returns:
  • a Result<Option<serde_json::Value>>>, which is Ok if nothing errored out the Option will be None if this is a void function, otherwise it will be Some(value) where value is a serde_json::Value representing the return value of the function
§Example:
use rpc_toy::Client;
let mut client = Client::new("127.0.0.1:3001").unwrap();
let one = serde_json::to_value(1u32).unwrap();
let two = serde_json::to_value(2u32).unwrap();
let args = vec![one, two];
let res = client.call("Add", &args).unwrap();
let three: u32 = serde_json::from_value(res.unwrap()).unwrap();
assert_eq!(three, 3);
Examples found in repository?
examples/client.rs (line 10)
5pub fn main() {
6    let mut client = Client::new("127.0.0.1:3001").unwrap();
7    let one = serde_json::to_value(1u32).unwrap();
8    let two = serde_json::to_value(2u32).unwrap();
9    let args = vec![one, two];
10    let res = client.call("Add", &args).unwrap();
11    let three: u32 = serde_json::from_value(res.unwrap()).unwrap();
12    assert_eq!(three, 3);
13    let world = client.call("hello", &vec![]).unwrap();
14    println!(
15        "Hello, {}!",
16        serde_json::from_value::<String>(world.unwrap()).unwrap()
17    );
18    let nothing = client.call("void_fn", &vec![]).unwrap();
19    assert!(nothing.is_none());
20}

Auto Trait Implementations§

§

impl Freeze for Client

§

impl RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl UnwindSafe for Client

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.