pub struct MessageHandler;Expand description
A handler for serializing and deserializing messages.
Provides utility functions to handle message encoding and decoding in JSON and CBOR formats.
Implementations§
Source§impl MessageHandler
impl MessageHandler
Sourcepub fn serialize<T: Serialize>(
data: &T,
format: MessageFormat,
) -> Result<Vec<u8>, String>
pub fn serialize<T: Serialize>( data: &T, format: MessageFormat, ) -> Result<Vec<u8>, String>
Serializes the given data into the specified format.
§Arguments
data- The data to serialize.format- The format to serialize the data into (MessageFormat::JsonorMessageFormat::Cbor).
§Returns
A Result containing the serialized data as a Vec<u8> on success, or an error message as a String on failure.
§Examples
use websocket_toolkit::messages::{MessageHandler, MessageFormat};
let message = "Hello, WebSocket!";
let serialized = MessageHandler::serialize(&message, MessageFormat::Json).unwrap();
assert!(!serialized.is_empty());Sourcepub fn deserialize<'a, T: Deserialize<'a>>(
data: &'a [u8],
format: MessageFormat,
) -> Result<Option<T>, String>
pub fn deserialize<'a, T: Deserialize<'a>>( data: &'a [u8], format: MessageFormat, ) -> Result<Option<T>, String>
Deserializes the given byte slice into the specified type.
§Arguments
data- The byte slice containing the serialized data.format- The format of the serialized data (MessageFormat::JsonorMessageFormat::Cbor).
§Returns
A Result containing the deserialized data as an Option<T> on success, or an error message as a String on failure.
§Examples
use websocket_toolkit::messages::{MessageHandler, MessageFormat};
let serialized = b"\"Hello, WebSocket!\"";
let deserialized: Option<String> = MessageHandler::deserialize(serialized, MessageFormat::Json).unwrap();
assert_eq!(deserialized, Some("Hello, WebSocket!".to_string()));Auto Trait Implementations§
impl Freeze for MessageHandler
impl RefUnwindSafe for MessageHandler
impl Send for MessageHandler
impl Sync for MessageHandler
impl Unpin for MessageHandler
impl UnsafeUnpin for MessageHandler
impl UnwindSafe for MessageHandler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more