Message

Enum Message 

Source
pub enum Message {
    Text(Bytes),
    Binary(Bytes),
    Ping(Bytes),
    Pong(Bytes),
    Close(Option<CloseReason>),
}
Expand description

WebSocket message (complete, possibly assembled from fragments)

Text messages use Bytes internally for zero-copy efficiency. The payload is UTF-8 validated during parsing, so as_text() is safe.

Variants§

§

Text(Bytes)

Text message (UTF-8 validated, stored as Bytes for zero-copy)

§

Binary(Bytes)

Binary message

§

Ping(Bytes)

Ping message

§

Pong(Bytes)

Pong message

§

Close(Option<CloseReason>)

Close message

Implementations§

Source§

impl Message

Source

pub fn text(s: impl Into<String>) -> Self

Create a text message from a string

Source

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

Create a binary message

Source

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

Create a ping message

Source

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

Create a pong message

Source

pub fn is_close(&self) -> bool

Check if this is a close message

Source

pub fn is_text(&self) -> bool

Check if this is a text message

Source

pub fn is_binary(&self) -> bool

Check if this is a binary message

Source

pub fn is_ping(&self) -> bool

Check if this is a ping message

Source

pub fn is_pong(&self) -> bool

Check if this is a pong message

Source

pub fn is_control(&self) -> bool

Check if this is a control message

Source

pub fn as_text(&self) -> Option<&str>

Get message as text (returns None for non-text messages)

This is zero-copy - it returns a reference to the underlying bytes. The text is guaranteed to be valid UTF-8 as it was validated during parsing.

Source

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

Get message as bytes

Source

pub fn into_text(self) -> Option<String>

Convert to text message (allocates a String)

Returns None for non-text messages.

Source

pub fn text_bytes(&self) -> Option<&Bytes>

Get the underlying Bytes for text messages (zero-copy)

Returns None for non-text messages.

Source

pub fn into_bytes(self) -> Bytes

Convert to binary data

Trait Implementations§

Source§

impl Clone for Message

Source§

fn clone(&self) -> Message

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Message

Source§

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

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

impl From<&[u8]> for Message

Source§

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

Converts to this type from the input type.
Source§

impl From<&str> for Message

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl From<Bytes> for Message

Source§

fn from(b: Bytes) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Message

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for Message

Source§

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

Converts to this type from the input type.
Source§

impl<S> Sink<Message> for CompressedWebSocketStream<S>
where S: AsyncRead + AsyncWrite + Unpin,

Available on crate feature permessage-deflate only.
Source§

type Error = Error

The type of value produced by the sink when an error occurs.
Source§

fn poll_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<()>>

Attempts to prepare the Sink to receive a value. Read more
Source§

fn start_send(self: Pin<&mut Self>, item: Message) -> Result<()>

Begin the process of sending a value to the sink. Each call to this function must be preceded by a successful call to poll_ready which returned Poll::Ready(Ok(())). Read more
Source§

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Flush any remaining output from this sink. Read more
Source§

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Flush any remaining output and close this sink, if necessary. Read more
Source§

impl<S> Sink<Message> for WebSocketStream<S>
where S: AsyncRead + AsyncWrite + Unpin,

Source§

type Error = Error

The type of value produced by the sink when an error occurs.
Source§

fn poll_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<()>>

Attempts to prepare the Sink to receive a value. Read more
Source§

fn start_send(self: Pin<&mut Self>, item: Message) -> Result<()>

Begin the process of sending a value to the sink. Each call to this function must be preceded by a successful call to poll_ready which returned Poll::Ready(Ok(())). Read more
Source§

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Flush any remaining output from this sink. Read more
Source§

fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Flush any remaining output and close this sink, if necessary. Read more

Auto Trait Implementations§

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> 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> Same for T

Source§

type Output = T

Should always be Self
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.