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