Line data Source code
1 : use core::net::IpAddr;
2 : use std::sync::Arc;
3 :
4 : use pq_proto::CancelKeyData;
5 : use tokio::sync::Mutex;
6 : use uuid::Uuid;
7 :
8 : pub trait CancellationPublisherMut: Send + Sync + 'static {
9 : #[allow(async_fn_in_trait)]
10 : async fn try_publish(
11 : &mut self,
12 : cancel_key_data: CancelKeyData,
13 : session_id: Uuid,
14 : peer_addr: IpAddr,
15 : ) -> anyhow::Result<()>;
16 : }
17 :
18 : pub trait CancellationPublisher: Send + Sync + 'static {
19 : #[allow(async_fn_in_trait)]
20 : async fn try_publish(
21 : &self,
22 : cancel_key_data: CancelKeyData,
23 : session_id: Uuid,
24 : peer_addr: IpAddr,
25 : ) -> anyhow::Result<()>;
26 : }
27 :
28 : impl CancellationPublisher for () {
29 0 : async fn try_publish(
30 0 : &self,
31 0 : _cancel_key_data: CancelKeyData,
32 0 : _session_id: Uuid,
33 0 : _peer_addr: IpAddr,
34 0 : ) -> anyhow::Result<()> {
35 0 : Ok(())
36 0 : }
37 : }
38 :
39 : impl<P: CancellationPublisher> CancellationPublisherMut for P {
40 0 : async fn try_publish(
41 0 : &mut self,
42 0 : cancel_key_data: CancelKeyData,
43 0 : session_id: Uuid,
44 0 : peer_addr: IpAddr,
45 0 : ) -> anyhow::Result<()> {
46 0 : <P as CancellationPublisher>::try_publish(self, cancel_key_data, session_id, peer_addr)
47 0 : .await
48 0 : }
49 : }
50 :
51 : impl<P: CancellationPublisher> CancellationPublisher for Option<P> {
52 0 : async fn try_publish(
53 0 : &self,
54 0 : cancel_key_data: CancelKeyData,
55 0 : session_id: Uuid,
56 0 : peer_addr: IpAddr,
57 0 : ) -> anyhow::Result<()> {
58 0 : if let Some(p) = self {
59 0 : p.try_publish(cancel_key_data, session_id, peer_addr).await
60 : } else {
61 0 : Ok(())
62 : }
63 0 : }
64 : }
65 :
66 : impl<P: CancellationPublisherMut> CancellationPublisher for Arc<Mutex<P>> {
67 0 : async fn try_publish(
68 0 : &self,
69 0 : cancel_key_data: CancelKeyData,
70 0 : session_id: Uuid,
71 0 : peer_addr: IpAddr,
72 0 : ) -> anyhow::Result<()> {
73 0 : self.lock()
74 0 : .await
75 0 : .try_publish(cancel_key_data, session_id, peer_addr)
76 0 : .await
77 0 : }
78 : }
|