jsonrpsee_server/middleware/rpc/layer/
mod.rs

1// Copyright 2019-2021 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any
4// person obtaining a copy of this software and associated
5// documentation files (the "Software"), to deal in the
6// Software without restriction, including without
7// limitation the rights to use, copy, modify, merge,
8// publish, distribute, sublicense, and/or sell copies of
9// the Software, and to permit persons to whom the Software
10// is furnished to do so, subject to the following
11// conditions:
12//
13// The above copyright notice and this permission notice
14// shall be included in all copies or substantial portions
15// of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
18// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
21// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
24// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25// DEALINGS IN THE SOFTWARE.
26
27//! Specific middleware layer implementation provided by jsonrpsee.
28
29pub mod either;
30pub mod logger;
31pub mod rpc_service;
32
33pub use logger::*;
34pub use rpc_service::*;
35
36use std::pin::Pin;
37use std::task::{Context, Poll};
38
39use futures_util::future::{Either, Future};
40use jsonrpsee_core::server::MethodResponse;
41use pin_project::pin_project;
42
43/// Response which may be ready or a future.
44#[derive(Debug)]
45#[pin_project]
46pub struct ResponseFuture<F>(#[pin] futures_util::future::Either<F, std::future::Ready<MethodResponse>>);
47
48impl<F> ResponseFuture<F> {
49	/// Returns a future that resolves to a response.
50	pub fn future(f: F) -> ResponseFuture<F> {
51		ResponseFuture(Either::Left(f))
52	}
53
54	/// Return a response which is already computed.
55	pub fn ready(response: MethodResponse) -> ResponseFuture<F> {
56		ResponseFuture(Either::Right(std::future::ready(response)))
57	}
58}
59
60impl<F: Future<Output = MethodResponse>> Future for ResponseFuture<F> {
61	type Output = MethodResponse;
62
63	fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
64		self.project().0.poll(cx)
65	}
66}