burn_communication/
base.rs1use burn_common::future::DynFut;
2use serde::{Deserialize, Serialize};
3use std::fmt::{Debug, Display};
4use std::hash::Hash;
5use std::str::FromStr;
6
7#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)]
9pub struct Address {
10 pub(crate) inner: String,
11}
12
13impl FromStr for Address {
14 type Err = String;
15
16 fn from_str(s: &str) -> Result<Self, Self::Err> {
17 Ok(Self {
18 inner: s.to_string(),
19 })
20 }
21}
22
23impl Display for Address {
24 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25 write!(f, "{}", self.inner)
26 }
27}
28
29pub trait Protocol: Clone + Send + Sync + 'static {
31 type Client: ProtocolClient;
33 type Server: ProtocolServer;
35}
36
37pub trait CommunicationError: Debug + Send + 'static {}
39
40pub trait ProtocolClient: Send + Sync + 'static {
43 type Channel: CommunicationChannel<Error = Self::Error>;
45 type Error: CommunicationError;
47
48 fn connect(address: Address, route: &str) -> DynFut<Option<Self::Channel>>;
56}
57
58#[derive(new)]
60pub struct Message {
61 pub data: bytes::Bytes,
63}
64
65pub trait ProtocolServer: Sized + Send + Sync + 'static {
67 type Channel: CommunicationChannel<Error = Self::Error>;
69 type Error: CommunicationError;
71
72 fn route<C, Fut>(self, path: &str, callback: C) -> Self
75 where
76 C: FnOnce(Self::Channel) -> Fut + Clone + Send + Sync + 'static,
77 Fut: Future<Output = ()> + Send + 'static;
78
79 fn serve<F>(
81 self,
82 shutdown: F,
83 ) -> impl Future<Output = Result<(), Self::Error>> + Send + 'static
84 where
85 F: Future<Output = ()> + Send + 'static;
86}
87
88pub trait CommunicationChannel: Send + 'static {
90 type Error: CommunicationError;
91
92 fn send(
94 &mut self,
95 message: Message,
96 ) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send;
97
98 fn recv(
100 &mut self,
101 ) -> impl std::future::Future<Output = Result<Option<Message>, Self::Error>> + Send;
102
103 fn close(&mut self) -> impl std::future::Future<Output = Result<(), Self::Error>> + Send;
104}