1 use jsonrpc_derive::rpc;
2 use serde::{Serialize, Deserialize};
3 
4 use std::sync::Arc;
5 use jsonrpc_core::Result;
6 use jsonrpc_pubsub::{typed::Subscriber, SubscriptionId, Session, PubSubHandler};
7 
8 #[rpc]
9 pub trait Rpc<T> {
10 	type Metadata;
11 
12 	/// Hello subscription
13 	#[pubsub(subscription = "hello", subscribe, name = "hello_subscribe", alias("hello_sub"))]
subscribe(&self, _: Self::Metadata, _: Subscriber<T>)14 	fn subscribe(&self, _: Self::Metadata, _: Subscriber<T>);
15 
16 	/// Unsubscribe from hello subscription.
17 	#[pubsub(subscription = "hello", unsubscribe, name = "hello_unsubscribe")]
unsubscribe(&self, a: Option<Self::Metadata>, b: SubscriptionId) -> Result<bool>18 	fn unsubscribe(&self, a: Option<Self::Metadata>, b: SubscriptionId) -> Result<bool>;
19 }
20 
21 #[derive(Serialize, Deserialize)]
22 struct SerializeAndDeserialize {
23 	foo: String,
24 }
25 
26 struct RpcImpl;
27 impl Rpc<SerializeAndDeserialize> for RpcImpl {
28 	type Metadata = Arc<Session>;
29 
subscribe(&self, _: Self::Metadata, _: Subscriber<SerializeAndDeserialize>)30 	fn subscribe(&self, _: Self::Metadata, _: Subscriber<SerializeAndDeserialize>) {
31 		unimplemented!();
32 	}
33 
unsubscribe(&self, _: Option<Self::Metadata>, _: SubscriptionId) -> Result<bool>34 	fn unsubscribe(&self, _: Option<Self::Metadata>, _: SubscriptionId) -> Result<bool> {
35 		unimplemented!();
36 	}
37 }
38 
main()39 fn main() {
40 	let mut io = PubSubHandler::default();
41 	let rpc = RpcImpl;
42 	io.extend_with(rpc.to_delegate());
43 }
44