hermes_five/io/transports/
mod.rs

1use crate::errors::Error;
2use crate::io::private::TraitToAny;
3use dyn_clone::DynClone;
4use std::fmt::{Debug, Display};
5use std::time::Duration;
6
7mod serial;
8pub use serial::Serial;
9
10/// Only used for tests to downcast the transport layer.
11pub(crate) mod private {
12    use std::any::Any;
13
14    pub trait TraitToAny: 'static {
15        fn as_any(&self) -> &dyn Any;
16    }
17
18    impl<T: 'static> TraitToAny for T {
19        fn as_any(&self) -> &dyn Any {
20            self
21        }
22    }
23}
24
25dyn_clone::clone_trait_object!(IoTransport);
26
27#[cfg_attr(feature = "serde", typetag::serde(tag = "type"))]
28pub trait IoTransport: Debug + Display + DynClone + Send + Sync + TraitToAny {
29    /// Opens communication (in a blocking way) using the transport layer.
30    ///
31    /// # Notes
32    ///  The method is sync and may block until the connection is established.
33    fn open(&mut self) -> Result<(), Error>;
34
35    /// Gracefully shuts down the transport layer.
36    fn close(&mut self) -> Result<(), Error>;
37
38    /// Sets a timeout for the transport layer
39    ///
40    /// # Notes
41    /// This function is optional and may not be supported by all transport layers.
42    fn set_timeout(&mut self, duration: Duration) -> Result<(), Error>;
43
44    /// Write bytes to the internal connection. For more details see [`std::io::Write::write`].
45    ///
46    /// # Notes
47    /// This function blocks until the write operation is complete. Ensure proper error handling in calling code.
48    fn write(&mut self, buf: &[u8]) -> Result<(), Error>;
49
50    /// Reads from the internal connection. For more details see [`std::io::Read::read_exact`].
51    ///
52    /// # Notes
53    /// This function blocks until the buffer is filled or an error occurs. Ensure proper error handling in calling code.
54    fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>;
55}