ZRPC is a library for creating Remote Procedure Calls (RPC) in Rust, which allows you to easily set up a server and client for data exchange. Below is a quick guide to the main aspects of using ZRPC with code examples.
Key Components of the library
- ZRpcDt Data Type:
ZRpcDtis an enumeration that represents various data types used in RPC calls. It includes:
Int32(i32)— an interger.Float32(f32)— a floating-point number.String(String)— a string.Serialized(Vec<u8>)— serialized data as byte vector.Error(ErrorKind)— an error that may occur during call processing.
Code Example:
- Serialization and Deserialization: Serialization and deserialization methods simplify the transfer of complex types between client and server. Code Example:
User .to_zdt => Serialized
- ErrorKind Enumeration:
ErrorKindis used to define various errors that may occur during RPC execution:
ProcedureNotFound— the called procedure was not found.InvalidParameters— invalid parameters were passed.InternalError— an internal server error.
Code Example:
- Creating a Server:
The server is initialized using
ZRpcServer, which listens for incoming connections.
Code Example:
async
- Creating a Client:
The client is initialized using
ZRpcClient, which establishes a connection to the server.
Code Example:
let mut client = new.await.unwrap;
- Calling Remote Procedures:
To call remote functions, the
callmethod is used. Requests are serialized and sent to the server, after which the client waits for a response.
Code Example:
match client.call.await
Middleware
Extensions
Macros
To initialize a request parameters, you can also use params!() macro:
let _ = client.call);
params! => vec!
Type Casting
Example:
1.to_zdt => Int8
3.14.to_zdt => Float32