jsonrpc_core

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 4)
3
4
5
6
7
8
9
10
11
12
fn main() {
	let mut io = IoHandler::new();

	io.add_sync_method("say_hello", |_: Params| Ok(Value::String("Hello World!".to_owned())));

	let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
	let response = r#"{"jsonrpc":"2.0","result":"hello","id":1}"#;

	assert_eq!(io.handle_request_sync(request), Some(response.to_owned()));
}
More examples
Hide additional examples
examples/async.rs (line 5)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
fn main() {
	futures_executor::block_on(async {
		let mut io = IoHandler::new();

		io.add_method("say_hello", |_: Params| async {
			Ok(Value::String("Hello World!".to_owned()))
		});

		let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
		let response = r#"{"jsonrpc":"2.0","result":"hello","id":1}"#;

		assert_eq!(io.handle_request(request).await, Some(response.to_owned()));
	});
}
examples/params.rs (line 10)
9
10
11
12
13
14
15
16
17
18
19
20
21
fn main() {
	let mut io = IoHandler::new();

	io.add_method("say_hello", |params: Params| async move {
		let parsed: HelloParams = params.parse().unwrap();
		Ok(Value::String(format!("hello, {}", parsed.name)))
	});

	let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": { "name": "world" }, "id": 1}"#;
	let response = r#"{"jsonrpc":"2.0","result":"hello, world","id":1}"#;

	assert_eq!(io.handle_request_sync(request), Some(response.to_owned()));
}
Source

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>

Source

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

Handle given string request asynchronously.

Examples found in repository?
examples/async.rs (line 14)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
fn main() {
	futures_executor::block_on(async {
		let mut io = IoHandler::new();

		io.add_method("say_hello", |_: Params| async {
			Ok(Value::String("Hello World!".to_owned()))
		});

		let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
		let response = r#"{"jsonrpc":"2.0","result":"hello","id":1}"#;

		assert_eq!(io.handle_request(request).await, Some(response.to_owned()));
	});
}
Source

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

Handle deserialized RPC request asynchronously.

Source

pub fn handle_call( &self, call: Call, ) -> Either<FutureOutput, Either<FutureOutput, Ready<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 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?
examples/basic.rs (line 11)
3
4
5
6
7
8
9
10
11
12
fn main() {
	let mut io = IoHandler::new();

	io.add_sync_method("say_hello", |_: Params| Ok(Value::String("Hello World!".to_owned())));

	let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
	let response = r#"{"jsonrpc":"2.0","result":"hello","id":1}"#;

	assert_eq!(io.handle_request_sync(request), Some(response.to_owned()));
}
More examples
Hide additional examples
examples/params.rs (line 20)
9
10
11
12
13
14
15
16
17
18
19
20
21
fn main() {
	let mut io = IoHandler::new();

	io.add_method("say_hello", |params: Params| async move {
		let parsed: HelloParams = params.parse().unwrap();
		Ok(Value::String(format!("hello, {}", parsed.name)))
	});

	let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": { "name": "world" }, "id": 1}"#;
	let response = r#"{"jsonrpc":"2.0","result":"hello, world","id":1}"#;

	assert_eq!(io.handle_request_sync(request), Some(response.to_owned()));
}

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

Source

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

Adds an alias to a method.

Source

pub fn add_sync_method<F>(&mut self, name: &str, method: F)
where F: RpcMethodSync,

Adds new supported synchronous method.

A backward-compatible wrapper.

Examples found in repository?
examples/basic.rs (line 6)
3
4
5
6
7
8
9
10
11
12
fn main() {
	let mut io = IoHandler::new();

	io.add_sync_method("say_hello", |_: Params| Ok(Value::String("Hello World!".to_owned())));

	let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
	let response = r#"{"jsonrpc":"2.0","result":"hello","id":1}"#;

	assert_eq!(io.handle_request_sync(request), Some(response.to_owned()));
}
Source

pub fn add_method<F>(&mut self, name: &str, method: F)
where F: RpcMethodSimple,

Adds new supported asynchronous method.

Examples found in repository?
examples/async.rs (lines 7-9)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
fn main() {
	futures_executor::block_on(async {
		let mut io = IoHandler::new();

		io.add_method("say_hello", |_: Params| async {
			Ok(Value::String("Hello World!".to_owned()))
		});

		let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
		let response = r#"{"jsonrpc":"2.0","result":"hello","id":1}"#;

		assert_eq!(io.handle_request(request).await, Some(response.to_owned()));
	});
}
More examples
Hide additional examples
examples/params.rs (lines 12-15)
9
10
11
12
13
14
15
16
17
18
19
20
21
fn main() {
	let mut io = IoHandler::new();

	io.add_method("say_hello", |params: Params| async move {
		let parsed: HelloParams = params.parse().unwrap();
		Ok(Value::String(format!("hello, {}", parsed.name)))
	});

	let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": { "name": "world" }, "id": 1}"#;
	let response = r#"{"jsonrpc":"2.0","result":"hello, world","id":1}"#;

	assert_eq!(io.handle_request_sync(request), Some(response.to_owned()));
}
Source

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

Adds new supported notification

Source

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?
examples/meta.rs (lines 10-12)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
pub fn main() {
	let mut io = MetaIoHandler::default();

	io.add_method_with_meta("say_hello", |_params: Params, meta: Meta| async move {
		Ok(Value::String(format!("Hello World: {}", meta.0)))
	});

	let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
	let response = r#"{"jsonrpc":"2.0","result":"Hello World: 5","id":1}"#;

	let headers = 5;
	assert_eq!(
		io.handle_request_sync(request, Meta(headers)),
		Some(response.to_owned())
	);
}
More examples
Hide additional examples
examples/middlewares.rs (lines 36-38)
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
pub fn main() {
	let mut io = MetaIoHandler::with_middleware(MyMiddleware::default());

	io.add_method_with_meta("say_hello", |_params: Params, meta: Meta| async move {
		Ok(Value::String(format!("Hello World: {}", meta.0)))
	});

	let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
	let response = r#"{"jsonrpc":"2.0","result":"Hello World: 5","id":1}"#;

	let headers = 5;
	assert_eq!(
		io.handle_request_sync(request, Meta(headers)),
		Some(response.to_owned())
	);
}
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, methods: F)
where F: IntoIterator<Item = (String, RemoteProcedure<T>)>,

Extend this MetaIoHandler with methods 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 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?
examples/meta.rs (line 19)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
pub fn main() {
	let mut io = MetaIoHandler::default();

	io.add_method_with_meta("say_hello", |_params: Params, meta: Meta| async move {
		Ok(Value::String(format!("Hello World: {}", meta.0)))
	});

	let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
	let response = r#"{"jsonrpc":"2.0","result":"Hello World: 5","id":1}"#;

	let headers = 5;
	assert_eq!(
		io.handle_request_sync(request, Meta(headers)),
		Some(response.to_owned())
	);
}
More examples
Hide additional examples
examples/middlewares.rs (line 45)
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
pub fn main() {
	let mut io = MetaIoHandler::with_middleware(MyMiddleware::default());

	io.add_method_with_meta("say_hello", |_params: Params, meta: Meta| async move {
		Ok(Value::String(format!("Hello World: {}", meta.0)))
	});

	let request = r#"{"jsonrpc": "2.0", "method": "say_hello", "params": [42, 23], "id": 1}"#;
	let response = r#"{"jsonrpc":"2.0","result":"Hello World: 5","id":1}"#;

	let headers = 5;
	assert_eq!(
		io.handle_request_sync(request, Meta(headers)),
		Some(response.to_owned())
	);
}
Source

pub fn handle_request( &self, request: &str, meta: T, ) -> FutureResult<S::Future, S::CallFuture>

Handle given request asynchronously.

Source

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

Handle deserialized RPC request.

Source

pub fn handle_call( &self, call: Call, meta: T, ) -> Either<S::CallFuture, Either<FutureOutput, Ready<Option<Output>>>>

Handle single call asynchronously.

Source

pub fn iter(&self) -> impl Iterator<Item = (&String, &RemoteProcedure<T>)>

Returns an iterator visiting all methods in arbitrary order.

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> IoHandler<M>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
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.
Source§

impl<T: Metadata> IntoIterator for IoHandler<T>

Source§

type Item = <MetaIoHandler<T> as IntoIterator>::Item

The type of the elements being iterated over.
Source§

type IntoIter = <MetaIoHandler<T> as IntoIterator>::IntoIter

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

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

Source§

fn augment<S: Middleware<M>>(self, handler: &mut MetaIoHandler<M, S>)

Extend given handler with additional methods.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.