pub struct RpcClient { /* private fields */ }Expand description
Unified RPC client for calling host functions
Implementations§
Source§impl RpcClient
impl RpcClient
Sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
examples/rpc.rs (line 5)
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5 let mut client = RpcClient::new();
6
7 // Example 1: Simple ping
8 println!("=== Example 1: Simple Ping ===");
9 match client.ping() {
10 Ok(response) => println!("Ping response: {}", response),
11 Err(e) => println!("Ping error: {}", e),
12 }
13
14 // Example 2: Echo with different data types
15 println!("\n=== Example 2: Echo Examples ===");
16
17 // Echo string
18 match client.echo("Hello, World!".to_string()) {
19 Ok(response) => println!("Echo string: {}", response),
20 Err(e) => println!("Echo error: {}", e),
21 }
22
23 // Echo number
24 match client.echo(42) {
25 Ok(response) => println!("Echo number: {}", response),
26 Err(e) => println!("Echo error: {}", e),
27 }
28
29 // Echo complex object
30 #[derive(Serialize, Deserialize, Debug)]
31 struct Person {
32 name: String,
33 age: u32,
34 }
35
36 let person = Person {
37 name: "Alice".to_string(),
38 age: 30,
39 };
40
41 match client.echo(person) {
42 Ok(response) => println!("Echo person: {:?}", response),
43 Err(e) => println!("Echo error: {}", e),
44 }
45
46 // Example 3: Get version
47 println!("\n=== Example 3: Get Version ===");
48 match client.version() {
49 Ok(version) => {
50 println!("Version info:");
51 for (key, value) in version {
52 println!(" {}: {}", key, value);
53 }
54 }
55 Err(e) => println!("Version error: {}", e),
56 }
57
58 // Example 4: Generic call with custom types
59 println!("\n=== Example 4: Generic Call ===");
60
61 #[derive(Serialize, Deserialize, Debug)]
62 struct CustomRequest {
63 message: String,
64 count: u32,
65 }
66
67 #[derive(Serialize, Deserialize, Debug)]
68 struct CustomResponse {
69 processed: String,
70 timestamp: u64,
71 }
72
73 let request = CustomRequest {
74 message: "Test message".to_string(),
75 count: 5,
76 };
77
78 // This would fail since "custom.process" doesn't exist in our test implementation
79 match client.call::<CustomRequest, CustomResponse>("custom.process", Some(request)) {
80 Ok(response) => {
81 if let Some(result) = response.result {
82 println!("Custom response: {:?}", result);
83 } else if let Some(error) = response.error {
84 println!("Custom error: {} (code: {})", error.message, error.code);
85 }
86 }
87 Err(e) => println!("Custom call error: {}", e),
88 }
89
90 // Example 5: Error handling
91 println!("\n=== Example 5: Error Handling ===");
92
93 // Try calling a non-existent method
94 match client.call::<(), String>("nonexistent.method", None) {
95 Ok(response) => {
96 if let Some(error) = response.error {
97 println!("Expected error: {} (code: {})", error.message, error.code);
98 }
99 }
100 Err(e) => println!("Call error: {}", e),
101 }
102
103 println!("\nAll examples completed!");
104 Ok(())
105}pub fn with_buffer_size(buffer_size: usize) -> Self
pub fn set_buffer_size(&mut self, buffer_size: usize)
pub fn buffer_size(&self) -> usize
Sourcepub fn call<P: Serialize, R: DeserializeOwned>(
&mut self,
method: &str,
params: Option<P>,
) -> Result<JsonRpcResponse<R>, RpcError>
pub fn call<P: Serialize, R: DeserializeOwned>( &mut self, method: &str, params: Option<P>, ) -> Result<JsonRpcResponse<R>, RpcError>
Examples found in repository?
examples/rpc.rs (line 79)
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5 let mut client = RpcClient::new();
6
7 // Example 1: Simple ping
8 println!("=== Example 1: Simple Ping ===");
9 match client.ping() {
10 Ok(response) => println!("Ping response: {}", response),
11 Err(e) => println!("Ping error: {}", e),
12 }
13
14 // Example 2: Echo with different data types
15 println!("\n=== Example 2: Echo Examples ===");
16
17 // Echo string
18 match client.echo("Hello, World!".to_string()) {
19 Ok(response) => println!("Echo string: {}", response),
20 Err(e) => println!("Echo error: {}", e),
21 }
22
23 // Echo number
24 match client.echo(42) {
25 Ok(response) => println!("Echo number: {}", response),
26 Err(e) => println!("Echo error: {}", e),
27 }
28
29 // Echo complex object
30 #[derive(Serialize, Deserialize, Debug)]
31 struct Person {
32 name: String,
33 age: u32,
34 }
35
36 let person = Person {
37 name: "Alice".to_string(),
38 age: 30,
39 };
40
41 match client.echo(person) {
42 Ok(response) => println!("Echo person: {:?}", response),
43 Err(e) => println!("Echo error: {}", e),
44 }
45
46 // Example 3: Get version
47 println!("\n=== Example 3: Get Version ===");
48 match client.version() {
49 Ok(version) => {
50 println!("Version info:");
51 for (key, value) in version {
52 println!(" {}: {}", key, value);
53 }
54 }
55 Err(e) => println!("Version error: {}", e),
56 }
57
58 // Example 4: Generic call with custom types
59 println!("\n=== Example 4: Generic Call ===");
60
61 #[derive(Serialize, Deserialize, Debug)]
62 struct CustomRequest {
63 message: String,
64 count: u32,
65 }
66
67 #[derive(Serialize, Deserialize, Debug)]
68 struct CustomResponse {
69 processed: String,
70 timestamp: u64,
71 }
72
73 let request = CustomRequest {
74 message: "Test message".to_string(),
75 count: 5,
76 };
77
78 // This would fail since "custom.process" doesn't exist in our test implementation
79 match client.call::<CustomRequest, CustomResponse>("custom.process", Some(request)) {
80 Ok(response) => {
81 if let Some(result) = response.result {
82 println!("Custom response: {:?}", result);
83 } else if let Some(error) = response.error {
84 println!("Custom error: {} (code: {})", error.message, error.code);
85 }
86 }
87 Err(e) => println!("Custom call error: {}", e),
88 }
89
90 // Example 5: Error handling
91 println!("\n=== Example 5: Error Handling ===");
92
93 // Try calling a non-existent method
94 match client.call::<(), String>("nonexistent.method", None) {
95 Ok(response) => {
96 if let Some(error) = response.error {
97 println!("Expected error: {} (code: {})", error.message, error.code);
98 }
99 }
100 Err(e) => println!("Call error: {}", e),
101 }
102
103 println!("\nAll examples completed!");
104 Ok(())
105}Sourcepub fn ping(&mut self) -> Result<String, RpcError>
pub fn ping(&mut self) -> Result<String, RpcError>
Convenience method for ping
Examples found in repository?
examples/rpc.rs (line 9)
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5 let mut client = RpcClient::new();
6
7 // Example 1: Simple ping
8 println!("=== Example 1: Simple Ping ===");
9 match client.ping() {
10 Ok(response) => println!("Ping response: {}", response),
11 Err(e) => println!("Ping error: {}", e),
12 }
13
14 // Example 2: Echo with different data types
15 println!("\n=== Example 2: Echo Examples ===");
16
17 // Echo string
18 match client.echo("Hello, World!".to_string()) {
19 Ok(response) => println!("Echo string: {}", response),
20 Err(e) => println!("Echo error: {}", e),
21 }
22
23 // Echo number
24 match client.echo(42) {
25 Ok(response) => println!("Echo number: {}", response),
26 Err(e) => println!("Echo error: {}", e),
27 }
28
29 // Echo complex object
30 #[derive(Serialize, Deserialize, Debug)]
31 struct Person {
32 name: String,
33 age: u32,
34 }
35
36 let person = Person {
37 name: "Alice".to_string(),
38 age: 30,
39 };
40
41 match client.echo(person) {
42 Ok(response) => println!("Echo person: {:?}", response),
43 Err(e) => println!("Echo error: {}", e),
44 }
45
46 // Example 3: Get version
47 println!("\n=== Example 3: Get Version ===");
48 match client.version() {
49 Ok(version) => {
50 println!("Version info:");
51 for (key, value) in version {
52 println!(" {}: {}", key, value);
53 }
54 }
55 Err(e) => println!("Version error: {}", e),
56 }
57
58 // Example 4: Generic call with custom types
59 println!("\n=== Example 4: Generic Call ===");
60
61 #[derive(Serialize, Deserialize, Debug)]
62 struct CustomRequest {
63 message: String,
64 count: u32,
65 }
66
67 #[derive(Serialize, Deserialize, Debug)]
68 struct CustomResponse {
69 processed: String,
70 timestamp: u64,
71 }
72
73 let request = CustomRequest {
74 message: "Test message".to_string(),
75 count: 5,
76 };
77
78 // This would fail since "custom.process" doesn't exist in our test implementation
79 match client.call::<CustomRequest, CustomResponse>("custom.process", Some(request)) {
80 Ok(response) => {
81 if let Some(result) = response.result {
82 println!("Custom response: {:?}", result);
83 } else if let Some(error) = response.error {
84 println!("Custom error: {} (code: {})", error.message, error.code);
85 }
86 }
87 Err(e) => println!("Custom call error: {}", e),
88 }
89
90 // Example 5: Error handling
91 println!("\n=== Example 5: Error Handling ===");
92
93 // Try calling a non-existent method
94 match client.call::<(), String>("nonexistent.method", None) {
95 Ok(response) => {
96 if let Some(error) = response.error {
97 println!("Expected error: {} (code: {})", error.message, error.code);
98 }
99 }
100 Err(e) => println!("Call error: {}", e),
101 }
102
103 println!("\nAll examples completed!");
104 Ok(())
105}Sourcepub fn echo<T: Serialize + DeserializeOwned>(
&mut self,
data: T,
) -> Result<T, RpcError>
pub fn echo<T: Serialize + DeserializeOwned>( &mut self, data: T, ) -> Result<T, RpcError>
Convenience method for echo
Examples found in repository?
examples/rpc.rs (line 18)
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5 let mut client = RpcClient::new();
6
7 // Example 1: Simple ping
8 println!("=== Example 1: Simple Ping ===");
9 match client.ping() {
10 Ok(response) => println!("Ping response: {}", response),
11 Err(e) => println!("Ping error: {}", e),
12 }
13
14 // Example 2: Echo with different data types
15 println!("\n=== Example 2: Echo Examples ===");
16
17 // Echo string
18 match client.echo("Hello, World!".to_string()) {
19 Ok(response) => println!("Echo string: {}", response),
20 Err(e) => println!("Echo error: {}", e),
21 }
22
23 // Echo number
24 match client.echo(42) {
25 Ok(response) => println!("Echo number: {}", response),
26 Err(e) => println!("Echo error: {}", e),
27 }
28
29 // Echo complex object
30 #[derive(Serialize, Deserialize, Debug)]
31 struct Person {
32 name: String,
33 age: u32,
34 }
35
36 let person = Person {
37 name: "Alice".to_string(),
38 age: 30,
39 };
40
41 match client.echo(person) {
42 Ok(response) => println!("Echo person: {:?}", response),
43 Err(e) => println!("Echo error: {}", e),
44 }
45
46 // Example 3: Get version
47 println!("\n=== Example 3: Get Version ===");
48 match client.version() {
49 Ok(version) => {
50 println!("Version info:");
51 for (key, value) in version {
52 println!(" {}: {}", key, value);
53 }
54 }
55 Err(e) => println!("Version error: {}", e),
56 }
57
58 // Example 4: Generic call with custom types
59 println!("\n=== Example 4: Generic Call ===");
60
61 #[derive(Serialize, Deserialize, Debug)]
62 struct CustomRequest {
63 message: String,
64 count: u32,
65 }
66
67 #[derive(Serialize, Deserialize, Debug)]
68 struct CustomResponse {
69 processed: String,
70 timestamp: u64,
71 }
72
73 let request = CustomRequest {
74 message: "Test message".to_string(),
75 count: 5,
76 };
77
78 // This would fail since "custom.process" doesn't exist in our test implementation
79 match client.call::<CustomRequest, CustomResponse>("custom.process", Some(request)) {
80 Ok(response) => {
81 if let Some(result) = response.result {
82 println!("Custom response: {:?}", result);
83 } else if let Some(error) = response.error {
84 println!("Custom error: {} (code: {})", error.message, error.code);
85 }
86 }
87 Err(e) => println!("Custom call error: {}", e),
88 }
89
90 // Example 5: Error handling
91 println!("\n=== Example 5: Error Handling ===");
92
93 // Try calling a non-existent method
94 match client.call::<(), String>("nonexistent.method", None) {
95 Ok(response) => {
96 if let Some(error) = response.error {
97 println!("Expected error: {} (code: {})", error.message, error.code);
98 }
99 }
100 Err(e) => println!("Call error: {}", e),
101 }
102
103 println!("\nAll examples completed!");
104 Ok(())
105}Sourcepub fn version(&mut self) -> Result<HashMap<String, String>, RpcError>
pub fn version(&mut self) -> Result<HashMap<String, String>, RpcError>
Convenience method for getting version
Examples found in repository?
examples/rpc.rs (line 48)
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5 let mut client = RpcClient::new();
6
7 // Example 1: Simple ping
8 println!("=== Example 1: Simple Ping ===");
9 match client.ping() {
10 Ok(response) => println!("Ping response: {}", response),
11 Err(e) => println!("Ping error: {}", e),
12 }
13
14 // Example 2: Echo with different data types
15 println!("\n=== Example 2: Echo Examples ===");
16
17 // Echo string
18 match client.echo("Hello, World!".to_string()) {
19 Ok(response) => println!("Echo string: {}", response),
20 Err(e) => println!("Echo error: {}", e),
21 }
22
23 // Echo number
24 match client.echo(42) {
25 Ok(response) => println!("Echo number: {}", response),
26 Err(e) => println!("Echo error: {}", e),
27 }
28
29 // Echo complex object
30 #[derive(Serialize, Deserialize, Debug)]
31 struct Person {
32 name: String,
33 age: u32,
34 }
35
36 let person = Person {
37 name: "Alice".to_string(),
38 age: 30,
39 };
40
41 match client.echo(person) {
42 Ok(response) => println!("Echo person: {:?}", response),
43 Err(e) => println!("Echo error: {}", e),
44 }
45
46 // Example 3: Get version
47 println!("\n=== Example 3: Get Version ===");
48 match client.version() {
49 Ok(version) => {
50 println!("Version info:");
51 for (key, value) in version {
52 println!(" {}: {}", key, value);
53 }
54 }
55 Err(e) => println!("Version error: {}", e),
56 }
57
58 // Example 4: Generic call with custom types
59 println!("\n=== Example 4: Generic Call ===");
60
61 #[derive(Serialize, Deserialize, Debug)]
62 struct CustomRequest {
63 message: String,
64 count: u32,
65 }
66
67 #[derive(Serialize, Deserialize, Debug)]
68 struct CustomResponse {
69 processed: String,
70 timestamp: u64,
71 }
72
73 let request = CustomRequest {
74 message: "Test message".to_string(),
75 count: 5,
76 };
77
78 // This would fail since "custom.process" doesn't exist in our test implementation
79 match client.call::<CustomRequest, CustomResponse>("custom.process", Some(request)) {
80 Ok(response) => {
81 if let Some(result) = response.result {
82 println!("Custom response: {:?}", result);
83 } else if let Some(error) = response.error {
84 println!("Custom error: {} (code: {})", error.message, error.code);
85 }
86 }
87 Err(e) => println!("Custom call error: {}", e),
88 }
89
90 // Example 5: Error handling
91 println!("\n=== Example 5: Error Handling ===");
92
93 // Try calling a non-existent method
94 match client.call::<(), String>("nonexistent.method", None) {
95 Ok(response) => {
96 if let Some(error) = response.error {
97 println!("Expected error: {} (code: {})", error.message, error.code);
98 }
99 }
100 Err(e) => println!("Call error: {}", e),
101 }
102
103 println!("\nAll examples completed!");
104 Ok(())
105}Trait Implementations§
Auto Trait Implementations§
impl Freeze for RpcClient
impl RefUnwindSafe for RpcClient
impl Send for RpcClient
impl Sync for RpcClient
impl Unpin for RpcClient
impl UnwindSafe for RpcClient
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