Skip to main content

hyperlane_plugin_websocket/
trait.rs

1use super::*;
2
3/// A trait for types that can be used as broadcast identifiers.
4///
5/// Types implementing this trait must be convertible to a string,
6/// be partially orderable, and be cloneable.
7pub trait BroadcastTypeTrait: ToString + PartialOrd + Clone {
8    /// Returns a direct borrowed string view when this identifier is already UTF-8 text.
9    ///
10    /// # Returns
11    ///
12    /// - `Option<&str>` - Direct borrowed slice, or `None` for non-string identifier types.
13    #[doc(hidden)]
14    #[inline(always)]
15    fn as_broadcast_str(&self) -> Option<&str> {
16        None
17    }
18
19    /// Writes UTF-8 identifier bytes directly into `buf` without `fmt::Write` dispatch or allocation.
20    ///
21    /// # Arguments
22    ///
23    /// - `&mut [u8]` - Destination UTF-8 byte buffer.
24    ///
25    /// # Returns
26    ///
27    /// - `Option<usize>` - Number of valid UTF-8 bytes written, or `None` to fall back to [`Self::write_broadcast_key`].
28    #[doc(hidden)]
29    #[inline(always)]
30    fn write_broadcast_bytes(&self, _buf: &mut [u8]) -> Option<usize> {
31        None
32    }
33
34    /// Writes this identifier directly into `f` without intermediate `String` allocation.
35    ///
36    /// # Arguments
37    ///
38    /// - `&mut dyn Write` - Destination formatting writer.
39    ///
40    /// # Returns
41    ///
42    /// - `fmt::Result` - Formatting outcome.
43    #[doc(hidden)]
44    fn write_broadcast_key(&self, f: &mut dyn Write) -> fmt::Result {
45        f.write_str(&self.to_string())
46    }
47}