pub struct MetaIoHandler<T: Metadata, S: Middleware<T> = Noop> { /* private fields */ }
Expand description
Request handler
By default compatible only with jsonrpc v2
Implementations§
Source§impl<T: Metadata> MetaIoHandler<T>
impl<T: Metadata> MetaIoHandler<T>
Sourcepub fn with_compatibility(compatibility: Compatibility) -> Self
pub fn with_compatibility(compatibility: Compatibility) -> Self
Creates new MetaIoHandler
compatible with specified protocol version.
Source§impl<T: Metadata, S: Middleware<T>> MetaIoHandler<T, S>
impl<T: Metadata, S: Middleware<T>> MetaIoHandler<T, S>
Sourcepub fn new(compatibility: Compatibility, middleware: S) -> Self
pub fn new(compatibility: Compatibility, middleware: S) -> Self
Creates new MetaIoHandler
Sourcepub fn with_middleware(middleware: S) -> Self
pub fn with_middleware(middleware: S) -> Self
Creates new MetaIoHandler
with specified middleware.
Examples found in repository?
examples/middlewares.rs (line 33)
32pub fn main() {
33 let mut io = MetaIoHandler::with_middleware(MyMiddleware::default());
34
35 io.add_msod_with_meta("say_hello", |_params: Params, meta: Meta| {
36 Ok(Value::String(format!("Hello World: {}", meta.0)))
37 });
38
39 let request = r#"{"jsonrpc": "2.0", "msod": "say_hello", "params": [42, 23], "id": 1}"#;
40 let response = r#"{"jsonrpc":"2.0","result":"Hello World: 5","id":1}"#;
41
42 let headers = 5;
43 assert_eq!(
44 io.handle_request(request, Meta(headers)).wait().unwrap(),
45 Some(response.to_owned())
46 );
47}
Sourcepub fn add_msod<F>(&mut self, name: &str, msod: F)where
F: RpcMsodSimple,
pub fn add_msod<F>(&mut self, name: &str, msod: F)where
F: RpcMsodSimple,
Adds new supported asynchronous msod
Examples found in repository?
examples/basic.rs (lines 8-10)
5fn main() {
6 let mut io = IoHandler::new();
7
8 io.add_msod("say_hello", |_: Params| {
9 Ok(Value::String("Hello World!".to_owned()))
10 });
11
12 let request = r#"{"jsonrpc": "2.0", "msod": "say_hello", "params": [42, 23], "id": 1}"#;
13 let response = r#"{"jsonrpc":"2.0","result":"hello","id":1}"#;
14
15 assert_eq!(io.handle_request_sync(request), Some(response.to_owned()));
16}
More examples
examples/async.rs (lines 9-11)
6fn main() {
7 let mut io = IoHandler::new();
8
9 io.add_msod("say_hello", |_: Params| {
10 futures::finished(Value::String("Hello World!".to_owned()))
11 });
12
13 let request = r#"{"jsonrpc": "2.0", "msod": "say_hello", "params": [42, 23], "id": 1}"#;
14 let response = r#"{"jsonrpc":"2.0","result":"hello","id":1}"#;
15
16 assert_eq!(io.handle_request(request).wait().unwrap(), Some(response.to_owned()));
17}
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_msod_with_meta<F>(&mut self, name: &str, msod: F)where
F: RpcMsod<T>,
pub fn add_msod_with_meta<F>(&mut self, name: &str, msod: F)where
F: RpcMsod<T>,
Adds new supported asynchronous msod with metadata support.
Examples found in repository?
examples/meta.rs (lines 13-15)
10pub fn main() {
11 let mut io = MetaIoHandler::default();
12
13 io.add_msod_with_meta("say_hello", |_params: Params, meta: Meta| {
14 Ok(Value::String(format!("Hello World: {}", meta.0)))
15 });
16
17 let request = r#"{"jsonrpc": "2.0", "msod": "say_hello", "params": [42, 23], "id": 1}"#;
18 let response = r#"{"jsonrpc":"2.0","result":"Hello World: 5","id":1}"#;
19
20 let headers = 5;
21 assert_eq!(
22 io.handle_request(request, Meta(headers)).wait().unwrap(),
23 Some(response.to_owned())
24 );
25}
More examples
examples/middlewares.rs (lines 35-37)
32pub fn main() {
33 let mut io = MetaIoHandler::with_middleware(MyMiddleware::default());
34
35 io.add_msod_with_meta("say_hello", |_params: Params, meta: Meta| {
36 Ok(Value::String(format!("Hello World: {}", meta.0)))
37 });
38
39 let request = r#"{"jsonrpc": "2.0", "msod": "say_hello", "params": [42, 23], "id": 1}"#;
40 let response = r#"{"jsonrpc":"2.0","result":"Hello World: 5","id":1}"#;
41
42 let headers = 5;
43 assert_eq!(
44 io.handle_request(request, Meta(headers)).wait().unwrap(),
45 Some(response.to_owned())
46 );
47}
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, msods: F)
pub fn extend_with<F>(&mut self, msods: F)
Extend this MetaIoHandler
with msods 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 msods in your RPC it is much wiser to use
handle_request
instead and deal with asynchronous requests in a non-blocking fashion.
Sourcepub fn handle_request(&self, request: &str, meta: T) -> FutureResult<S::Future>
pub fn handle_request(&self, request: &str, meta: T) -> FutureResult<S::Future>
Handle given request asynchronously.
Examples found in repository?
examples/meta.rs (line 22)
10pub fn main() {
11 let mut io = MetaIoHandler::default();
12
13 io.add_msod_with_meta("say_hello", |_params: Params, meta: Meta| {
14 Ok(Value::String(format!("Hello World: {}", meta.0)))
15 });
16
17 let request = r#"{"jsonrpc": "2.0", "msod": "say_hello", "params": [42, 23], "id": 1}"#;
18 let response = r#"{"jsonrpc":"2.0","result":"Hello World: 5","id":1}"#;
19
20 let headers = 5;
21 assert_eq!(
22 io.handle_request(request, Meta(headers)).wait().unwrap(),
23 Some(response.to_owned())
24 );
25}
More examples
examples/middlewares.rs (line 44)
32pub fn main() {
33 let mut io = MetaIoHandler::with_middleware(MyMiddleware::default());
34
35 io.add_msod_with_meta("say_hello", |_params: Params, meta: Meta| {
36 Ok(Value::String(format!("Hello World: {}", meta.0)))
37 });
38
39 let request = r#"{"jsonrpc": "2.0", "msod": "say_hello", "params": [42, 23], "id": 1}"#;
40 let response = r#"{"jsonrpc":"2.0","result":"Hello World: 5","id":1}"#;
41
42 let headers = 5;
43 assert_eq!(
44 io.handle_request(request, Meta(headers)).wait().unwrap(),
45 Some(response.to_owned())
46 );
47}
Sourcepub fn handle_rpc_request(&self, request: Request, meta: T) -> S::Future
pub fn handle_rpc_request(&self, request: Request, meta: T) -> S::Future
Handle deserialized RPC request.
Trait Implementations§
Source§impl<T: Debug + Metadata, S: Debug + Middleware<T>> Debug for MetaIoHandler<T, S>
impl<T: Debug + Metadata, S: Debug + Middleware<T>> Debug for MetaIoHandler<T, S>
Source§impl<T: Metadata> Default for MetaIoHandler<T>
impl<T: Metadata> Default for MetaIoHandler<T>
Auto Trait Implementations§
impl<T, S> Freeze for MetaIoHandler<T, S>where
S: Freeze,
impl<T, S = Noop> !RefUnwindSafe for MetaIoHandler<T, S>
impl<T, S> Send for MetaIoHandler<T, S>
impl<T, S> Sync for MetaIoHandler<T, S>
impl<T, S> Unpin for MetaIoHandler<T, S>where
S: Unpin,
impl<T, S = Noop> !UnwindSafe for MetaIoHandler<T, S>
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