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
impl Client
Sourcepub fn new(addr: &str) -> Result<Self, Error>
pub fn new(addr: &str) -> Result<Self, Error>
Creates a new client that connects to an RPC server
§Arguments:
addrThe 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}Sourcepub fn call(
&mut self,
fn_name: &str,
args: &[Value],
) -> Result<Option<Value>, Error>
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 :)argsa slice ofserde_json::Values. This represents the arguments that will be passed onto the server’s functions
§Returns:
- a
Result<Option<serde_json::Value>>>, which isOkif nothing errored out theOptionwill beNoneif this is a void function, otherwise it will beSome(value)wherevalueis aserde_json::Valuerepresenting 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more