Skip to main content

Frame

Enum Frame 

Source
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

Source

pub fn binary(data: impl Into<Bytes>) -> Self

Construct a Binary frame from anything Bytes converts from.

Source

pub fn len(&self) -> usize

Payload length in bytes (UTF-8 length for text).

Source

pub fn is_empty(&self) -> bool

True when the payload is empty.

Source

pub fn is_text(&self) -> bool

True for Frame::Text.

Source

pub fn is_binary(&self) -> bool

True for Frame::Binary.

Source

pub fn as_bytes(&self) -> &[u8] ⓘ

Payload as bytes (UTF-8 for text), without consuming the frame.

Source

pub fn into_bytes(self) -> Bytes

Take the payload as owned bytes, consuming the frame.

Source

pub fn into_text(self) -> Result<String, WsError>

Interpret the payload as UTF-8 text (binary is rejected, not sniffed).

Trait Implementations§

Source§

impl Clone for Frame

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Frame

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Frame

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for Frame

Source§

impl From<&[u8]> for Frame

Source§

fn from(v: &[u8]) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for Frame

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl From<Bytes> for Frame

Source§

fn from(b: Bytes) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Frame

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for Frame

Source§

fn from(v: Vec<u8>) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Frame

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for Frame

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Frame

Auto Trait Implementations§

§

impl !Freeze for Frame

§

impl RefUnwindSafe for Frame

§

impl Send for Frame

§

impl Sync for Frame

§

impl Unpin for Frame

§

impl UnsafeUnpin for Frame

§

impl UnwindSafe for Frame

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Codec for T

Source§

fn encode(&self) -> String
where Self: Serialize,

Encode self into a JSON string. Read more
Source§

fn encode_result(&self) -> Result<String, WsError>
where Self: Serialize,

Encode with error handling.
Source§

fn decode(s: &str) -> Result<Self, WsError>
where Self: DeserializeOwned,

Decode Self from a JSON string.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.