pub struct IoHandler<M: Metadata = ()>(/* private fields */);
Expand description
Simplified IoHandler
with no Metadata
associated with each request.
Implementations§
Source§impl IoHandler
impl IoHandler
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates new IoHandler
without any metadata.
Examples found in repository?
More examples
3fn main() {
4 futures_executor::block_on(async {
5 let mut io = IoHandler::new();
6
7 io.add_method("say_hello", |_: Params| async {
8 Ok(Value::String("Hello World!".to_owned()))
9 });
10
11 let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
12 let response = r#"{"jsonrpc":"2.0","result":"hello","id":1}"#;
13
14 assert_eq!(io.handle_request(request).await, Some(response.to_owned()));
15 });
16}
9fn main() {
10 let mut io = IoHandler::new();
11
12 io.add_method("say_hello", |params: Params| async move {
13 let parsed: HelloParams = params.parse().unwrap();
14 Ok(Value::String(format!("hello, {}", parsed.name)))
15 });
16
17 let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": { "name": "world" }, "id": 1}"#;
18 let response = r#"{"jsonrpc":"2.0","result":"hello, world","id":1}"#;
19
20 assert_eq!(io.handle_request_sync(request), Some(response.to_owned()));
21}
Sourcepub fn with_compatibility(compatibility: Compatibility) -> Self
pub fn with_compatibility(compatibility: Compatibility) -> Self
Creates new IoHandler
without any metadata compatible with specified protocol version.
Source§impl<M: Metadata + Default> IoHandler<M>
impl<M: Metadata + Default> IoHandler<M>
Sourcepub fn handle_request(
&self,
request: &str,
) -> FutureResult<FutureResponse, FutureOutput>
pub fn handle_request( &self, request: &str, ) -> FutureResult<FutureResponse, FutureOutput>
Handle given string request asynchronously.
Examples found in repository?
3fn main() {
4 futures_executor::block_on(async {
5 let mut io = IoHandler::new();
6
7 io.add_method("say_hello", |_: Params| async {
8 Ok(Value::String("Hello World!".to_owned()))
9 });
10
11 let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
12 let response = r#"{"jsonrpc":"2.0","result":"hello","id":1}"#;
13
14 assert_eq!(io.handle_request(request).await, Some(response.to_owned()));
15 });
16}
Sourcepub fn handle_rpc_request(
&self,
request: Request,
) -> FutureRpcResult<FutureResponse, FutureOutput>
pub fn handle_rpc_request( &self, request: Request, ) -> FutureRpcResult<FutureResponse, FutureOutput>
Handle deserialized RPC request asynchronously.
Sourcepub fn handle_call(
&self,
call: Call,
) -> Either<FutureOutput, Either<FutureOutput, Ready<Option<Output>>>> ⓘ
pub fn handle_call( &self, call: Call, ) -> Either<FutureOutput, Either<FutureOutput, Ready<Option<Output>>>> ⓘ
Handle single Call asynchronously.
Sourcepub fn handle_request_sync(&self, request: &str) -> Option<String>
pub fn handle_request_sync(&self, request: &str) -> Option<String>
Handle given request synchronously - will block until response is available.
If you have any asynchronous methods in your RPC it is much wiser to use
handle_request
instead and deal with asynchronous requests in a non-blocking fashion.
Examples found in repository?
More examples
9fn main() {
10 let mut io = IoHandler::new();
11
12 io.add_method("say_hello", |params: Params| async move {
13 let parsed: HelloParams = params.parse().unwrap();
14 Ok(Value::String(format!("hello, {}", parsed.name)))
15 });
16
17 let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": { "name": "world" }, "id": 1}"#;
18 let response = r#"{"jsonrpc":"2.0","result":"hello, world","id":1}"#;
19
20 assert_eq!(io.handle_request_sync(request), Some(response.to_owned()));
21}
Methods from Deref<Target = MetaIoHandler<M>>§
Sourcepub fn add_sync_method<F>(&mut self, name: &str, method: F)where
F: RpcMethodSync,
pub fn add_sync_method<F>(&mut self, name: &str, method: F)where
F: RpcMethodSync,
Adds new supported synchronous method.
A backward-compatible wrapper.
Sourcepub fn add_method<F>(&mut self, name: &str, method: F)where
F: RpcMethodSimple,
pub fn add_method<F>(&mut self, name: &str, method: F)where
F: RpcMethodSimple,
Adds new supported asynchronous method.
Examples found in repository?
3fn main() {
4 futures_executor::block_on(async {
5 let mut io = IoHandler::new();
6
7 io.add_method("say_hello", |_: Params| async {
8 Ok(Value::String("Hello World!".to_owned()))
9 });
10
11 let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
12 let response = r#"{"jsonrpc":"2.0","result":"hello","id":1}"#;
13
14 assert_eq!(io.handle_request(request).await, Some(response.to_owned()));
15 });
16}
More examples
9fn main() {
10 let mut io = IoHandler::new();
11
12 io.add_method("say_hello", |params: Params| async move {
13 let parsed: HelloParams = params.parse().unwrap();
14 Ok(Value::String(format!("hello, {}", parsed.name)))
15 });
16
17 let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": { "name": "world" }, "id": 1}"#;
18 let response = r#"{"jsonrpc":"2.0","result":"hello, world","id":1}"#;
19
20 assert_eq!(io.handle_request_sync(request), Some(response.to_owned()));
21}
Sourcepub fn add_notification<F>(&mut self, name: &str, notification: F)where
F: RpcNotificationSimple,
pub fn add_notification<F>(&mut self, name: &str, notification: F)where
F: RpcNotificationSimple,
Adds new supported notification
Sourcepub fn add_method_with_meta<F>(&mut self, name: &str, method: F)where
F: RpcMethod<T>,
pub fn add_method_with_meta<F>(&mut self, name: &str, method: F)where
F: RpcMethod<T>,
Adds new supported asynchronous method with metadata support.
Examples found in repository?
7pub fn main() {
8 let mut io = MetaIoHandler::default();
9
10 io.add_method_with_meta("say_hello", |_params: Params, meta: Meta| async move {
11 Ok(Value::String(format!("Hello World: {}", meta.0)))
12 });
13
14 let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
15 let response = r#"{"jsonrpc":"2.0","result":"Hello World: 5","id":1}"#;
16
17 let headers = 5;
18 assert_eq!(
19 io.handle_request_sync(request, Meta(headers)),
20 Some(response.to_owned())
21 );
22}
More examples
33pub fn main() {
34 let mut io = MetaIoHandler::with_middleware(MyMiddleware::default());
35
36 io.add_method_with_meta("say_hello", |_params: Params, meta: Meta| async move {
37 Ok(Value::String(format!("Hello World: {}", meta.0)))
38 });
39
40 let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
41 let response = r#"{"jsonrpc":"2.0","result":"Hello World: 5","id":1}"#;
42
43 let headers = 5;
44 assert_eq!(
45 io.handle_request_sync(request, Meta(headers)),
46 Some(response.to_owned())
47 );
48}
Sourcepub fn add_notification_with_meta<F>(&mut self, name: &str, notification: F)where
F: RpcNotification<T>,
pub fn add_notification_with_meta<F>(&mut self, name: &str, notification: F)where
F: RpcNotification<T>,
Adds new supported notification with metadata support.
Sourcepub fn extend_with<F>(&mut self, methods: F)
pub fn extend_with<F>(&mut self, methods: F)
Extend this MetaIoHandler
with methods defined elsewhere.
Sourcepub fn handle_request_sync(&self, request: &str, meta: T) -> Option<String>
pub fn handle_request_sync(&self, request: &str, meta: T) -> Option<String>
Handle given request synchronously - will block until response is available.
If you have any asynchronous methods in your RPC it is much wiser to use
handle_request
instead and deal with asynchronous requests in a non-blocking fashion.
Examples found in repository?
7pub fn main() {
8 let mut io = MetaIoHandler::default();
9
10 io.add_method_with_meta("say_hello", |_params: Params, meta: Meta| async move {
11 Ok(Value::String(format!("Hello World: {}", meta.0)))
12 });
13
14 let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
15 let response = r#"{"jsonrpc":"2.0","result":"Hello World: 5","id":1}"#;
16
17 let headers = 5;
18 assert_eq!(
19 io.handle_request_sync(request, Meta(headers)),
20 Some(response.to_owned())
21 );
22}
More examples
33pub fn main() {
34 let mut io = MetaIoHandler::with_middleware(MyMiddleware::default());
35
36 io.add_method_with_meta("say_hello", |_params: Params, meta: Meta| async move {
37 Ok(Value::String(format!("Hello World: {}", meta.0)))
38 });
39
40 let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
41 let response = r#"{"jsonrpc":"2.0","result":"Hello World: 5","id":1}"#;
42
43 let headers = 5;
44 assert_eq!(
45 io.handle_request_sync(request, Meta(headers)),
46 Some(response.to_owned())
47 );
48}
Sourcepub fn handle_request(
&self,
request: &str,
meta: T,
) -> FutureResult<S::Future, S::CallFuture>
pub fn handle_request( &self, request: &str, meta: T, ) -> FutureResult<S::Future, S::CallFuture>
Handle given request asynchronously.
Sourcepub fn handle_rpc_request(
&self,
request: Request,
meta: T,
) -> FutureRpcResult<S::Future, S::CallFuture>
pub fn handle_rpc_request( &self, request: Request, meta: T, ) -> FutureRpcResult<S::Future, S::CallFuture>
Handle deserialized RPC request.
Sourcepub fn handle_call(
&self,
call: Call,
meta: T,
) -> Either<S::CallFuture, Either<FutureOutput, Ready<Option<Output>>>> ⓘ
pub fn handle_call( &self, call: Call, meta: T, ) -> Either<S::CallFuture, Either<FutureOutput, Ready<Option<Output>>>> ⓘ
Handle single call asynchronously.
Sourcepub fn iter(&self) -> impl Iterator<Item = (&String, &RemoteProcedure<T>)>
pub fn iter(&self) -> impl Iterator<Item = (&String, &RemoteProcedure<T>)>
Returns an iterator visiting all methods in arbitrary order.
Trait Implementations§
Source§impl<T: Metadata> IntoIterator for IoHandler<T>
impl<T: Metadata> IntoIterator for IoHandler<T>
Source§type Item = <MetaIoHandler<T> as IntoIterator>::Item
type Item = <MetaIoHandler<T> as IntoIterator>::Item
Source§type IntoIter = <MetaIoHandler<T> as IntoIterator>::IntoIter
type IntoIter = <MetaIoHandler<T> as IntoIterator>::IntoIter
Source§impl<M: Metadata> IoHandlerExtension<M> for IoHandler<M>
impl<M: Metadata> IoHandlerExtension<M> for IoHandler<M>
Source§fn augment<S: Middleware<M>>(self, handler: &mut MetaIoHandler<M, S>)
fn augment<S: Middleware<M>>(self, handler: &mut MetaIoHandler<M, S>)
handler
with additional methods.