mqtt5_protocol/
transport.rs

1use crate::error::Result;
2
3#[cfg(not(target_arch = "wasm32"))]
4pub trait Transport: Send + Sync {
5    /// Establishes a connection
6    ///
7    /// # Errors
8    ///
9    /// Returns an error if the connection cannot be established
10    fn connect(&mut self) -> impl std::future::Future<Output = Result<()>> + Send;
11
12    /// Reads data into the provided buffer
13    ///
14    /// # Errors
15    ///
16    /// Returns an error if the read operation fails
17    fn read(&mut self, buf: &mut [u8]) -> impl std::future::Future<Output = Result<usize>> + Send;
18
19    /// Writes data from the provided buffer
20    ///
21    /// # Errors
22    ///
23    /// Returns an error if the write operation fails
24    fn write(&mut self, buf: &[u8]) -> impl std::future::Future<Output = Result<()>> + Send;
25
26    /// Closes the connection
27    ///
28    /// # Errors
29    ///
30    /// Returns an error if the connection cannot be closed cleanly
31    fn close(&mut self) -> impl std::future::Future<Output = Result<()>> + Send;
32
33    /// Checks if the transport is connected
34    fn is_connected(&self) -> bool {
35        false
36    }
37}
38
39#[cfg(target_arch = "wasm32")]
40pub trait Transport {
41    /// Establishes a connection
42    ///
43    /// # Errors
44    ///
45    /// Returns an error if the connection cannot be established
46    fn connect(&mut self) -> impl std::future::Future<Output = Result<()>>;
47
48    /// Reads data into the provided buffer
49    ///
50    /// # Errors
51    ///
52    /// Returns an error if the read operation fails
53    fn read(&mut self, buf: &mut [u8]) -> impl std::future::Future<Output = Result<usize>>;
54
55    /// Writes data from the provided buffer
56    ///
57    /// # Errors
58    ///
59    /// Returns an error if the write operation fails
60    fn write(&mut self, buf: &[u8]) -> impl std::future::Future<Output = Result<()>>;
61
62    /// Closes the connection
63    ///
64    /// # Errors
65    ///
66    /// Returns an error if the connection cannot be closed cleanly
67    fn close(&mut self) -> impl std::future::Future<Output = Result<()>>;
68
69    /// Checks if the transport is connected
70    fn is_connected(&self) -> bool {
71        false
72    }
73}