client/common/
delegate_convert.rs

1use std::convert::Into as StdInto;
2
3use jsonrpc_core::{
4    futures::{FutureExt, TryFutureExt},
5    BoxFuture, Error as JsonRpcError, Result as JsonRpcResult,
6};
7
8use crate::rpc::{CoreBoxFuture, CoreError, CoreResult};
9
10pub trait Into<T> {
11    fn into(x: Self) -> T;
12}
13
14impl<T> Into<JsonRpcResult<T>> for JsonRpcResult<T> {
15    fn into(x: Self) -> JsonRpcResult<T> { x }
16}
17
18impl<T: Send + Sync + 'static> Into<BoxFuture<T>> for BoxFuture<T> {
19    fn into(x: Self) -> BoxFuture<T> { x }
20}
21
22impl<T: Send + Sync + 'static> Into<BoxFuture<JsonRpcResult<T>>>
23    for CoreBoxFuture<T>
24{
25    fn into(x: Self) -> BoxFuture<JsonRpcResult<T>> {
26        x.map_err(Into::into).boxed()
27    }
28}
29
30impl Into<JsonRpcError> for CoreError {
31    fn into(e: Self) -> JsonRpcError { e.into() }
32}
33
34pub fn into_jsonrpc_result<T>(r: CoreResult<T>) -> JsonRpcResult<T> {
35    match r {
36        Ok(t) => Ok(t),
37        Err(e) => Err(Into::into(e)),
38    }
39}
40
41impl<T> Into<JsonRpcResult<T>> for CoreResult<T> {
42    fn into(x: Self) -> JsonRpcResult<T> { into_jsonrpc_result(x) }
43}
44
45/// Sometimes an rpc method is implemented asynchronously, then the rpc
46/// trait definition must use BoxFuture for the return type.
47///
48/// This into conversion allow non-async rpc implementation method to
49/// return RpcResult straight-forward. The delegate! macro with  #\[into\]
50/// attribute will automatically call this method to do the return type
51/// conversion.
52impl<T: Send + Sync + 'static> Into<BoxFuture<JsonRpcResult<T>>>
53    for CoreResult<T>
54{
55    fn into(x: Self) -> BoxFuture<JsonRpcResult<T>> {
56        async { into_jsonrpc_result(x) }.boxed()
57    }
58}