Struct IoHandler

Source
pub struct IoHandler<M: Metadata = ()>(/* private fields */);
Expand description

Simplified IoHandler with no Metadata associated with each request.

Implementations§

Source§

impl IoHandler

Source

pub fn new() -> Self

Creates new IoHandler without any metadata.

Examples found in repository?
examples/basic.rs (line 6)
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
Hide additional examples
examples/async.rs (line 7)
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}
Source

pub fn with_compatibility(compatibility: Compatibility) -> Self

Creates new IoHandler without any metadata compatible with specified protocol version.

Source§

impl<M: Metadata> IoHandler<M>

Source

pub fn handle_request(&self, request: &str) -> FutureResult<FutureResponse>

Handle given string request asynchronously.

Examples found in repository?
examples/async.rs (line 16)
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}
Source

pub fn handle_rpc_request(&self, request: Request) -> FutureResponse

Handle deserialized RPC request asynchronously.

Source

pub fn handle_call( &self, call: Call, ) -> Either<Box<dyn Future<Item = Option<Output>, Error = ()> + Send>, FutureResult<Option<Output>, ()>>

Handle single Call asynchronously.

Source

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 msods 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?
examples/basic.rs (line 15)
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}

Methods from Deref<Target = MetaIoHandler<M>>§

Source

pub fn add_alias(&mut self, alias: &str, other: &str)

Adds an alias to a msod.

Source

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
Hide additional 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}
Source

pub fn add_notification<F>(&mut self, name: &str, notification: F)

Adds new supported notification

Source

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
Hide additional 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}
Source

pub fn add_notification_with_meta<F>(&mut self, name: &str, notification: F)
where F: RpcNotification<T>,

Adds new supported notification with metadata support.

Source

pub fn extend_with<F>(&mut self, msods: F)

Extend this MetaIoHandler with msods defined elsewhere.

Source

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.

Source

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
Hide additional 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}
Source

pub fn handle_rpc_request(&self, request: Request, meta: T) -> S::Future

Handle deserialized RPC request.

Source

pub fn handle_call( &self, call: Call, meta: T, ) -> Either<Box<dyn Future<Item = Option<Output>, Error = ()> + Send>, FutureResult<Option<Output>, ()>>

Handle single call asynchronously.

Trait Implementations§

Source§

impl<M: Debug + Metadata> Debug for IoHandler<M>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<M: Default + Metadata> Default for IoHandler<M>

Source§

fn default() -> IoHandler<M>

Returns the “default value” for a type. Read more
Source§

impl<M: Metadata> Deref for IoHandler<M>

Source§

type Target = MetaIoHandler<M>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<M: Metadata> DerefMut for IoHandler<M>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl From<IoHandler> for MetaIoHandler<()>

Source§

fn from(io: IoHandler) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<M> Freeze for IoHandler<M>

§

impl<M = ()> !RefUnwindSafe for IoHandler<M>

§

impl<M> Send for IoHandler<M>

§

impl<M> Sync for IoHandler<M>

§

impl<M> Unpin for IoHandler<M>

§

impl<M = ()> !UnwindSafe for IoHandler<M>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.