pub enum Frame {
Text(String),
Binary(Bytes),
}Expand description
A wire message: the union of WebSocket text and binary data frames.
Conversions are zero-copy where the transport already owns the data:
String, &str, and Bytes all move into a Frame without
re-encoding. Vec<u8> and borrowed &[u8] copy once.
Frame implements Serialize/Deserialize, so it can ride the generic
crate::hub::BroadcastHub<Frame>; in JSON form binary payloads are
represented as byte arrays and — per the security posture below — are
only ever decoded back into Frame::Binary, never parsed as text.
§Example
use ws_kit::codec::Frame;
let text = Frame::from("hello"); // &str — one UTF-8 copy
let owned = Frame::from(String::from("hi")); // String — zero-copy move
let bin = Frame::from(vec![0u8, 1, 2]); // Vec<u8> — becomes Binary
assert!(matches!(text, Frame::Text(_)));
assert!(matches!(bin, Frame::Binary(_)));Variants§
Text(String)
A UTF-8 text frame (WebSocket opcode 0x1).
Binary(Bytes)
An opaque binary frame (WebSocket opcode 0x2). Never parsed as text/JSON by ws-kit — see the security posture.
Implementations§
Source§impl Frame
impl Frame
Sourcepub fn binary(data: impl Into<Bytes>) -> Self
pub fn binary(data: impl Into<Bytes>) -> Self
Construct a Binary frame from anything Bytes converts from.
Sourcepub fn is_text(&self) -> bool
pub fn is_text(&self) -> bool
True for Frame::Text.
Sourcepub fn is_binary(&self) -> bool
pub fn is_binary(&self) -> bool
True for Frame::Binary.
Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Payload as bytes (UTF-8 for text), without consuming the frame.
Sourcepub fn into_bytes(self) -> Bytes
pub fn into_bytes(self) -> Bytes
Take the payload as owned bytes, consuming the frame.