pub enum Message {
Text(Bytes),
Binary(Bytes),
Ping(Bytes),
Pong(Bytes),
Close(Option<CloseFrame>),
}Expand description
A WebSocket message.
Backend-agnostic representation handed to consumers of the
nautilus-network transport layer. Each backend provides From impls
between its native Message type and this enum.
Text is documented to carry UTF-8 by contract but the type does not
enforce it. Backends that already validate (such as tokio-tungstenite)
produce valid bytes; an in-house HFT backend may skip validation for
performance and rely on the consumer’s parser to catch malformed bytes.
Use Self::as_text to view the payload as &str.
Variants§
Text(Bytes)
Text message. Payload is UTF-8 by contract, not by type guarantee.
Binary(Bytes)
Binary message.
Ping(Bytes)
Ping control frame. Payload bounded to 125 bytes by RFC 6455.
Pong(Bytes)
Pong control frame. Payload bounded to 125 bytes by RFC 6455.
Close(Option<CloseFrame>)
Close control frame with optional close frame payload.
Implementations§
Source§impl Message
impl Message
Sourcepub fn as_text(&self) -> Option<&str>
pub fn as_text(&self) -> Option<&str>
Borrow a text message as &str if the payload is valid UTF-8.
Validates on each call; for hot paths where the producer is trusted,
callers can read the bytes directly via Self::as_bytes and feed
them to a parser that catches malformed input.
Sourcepub const fn is_control(&self) -> bool
pub const fn is_control(&self) -> bool
Returns true for control frames (ping, pong, close).
Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Returns the message payload as a byte slice.
For close frames, returns the reason payload as bytes.
Sourcepub fn into_bytes(self) -> Bytes
pub fn into_bytes(self) -> Bytes
Consumes the message and returns its payload as Bytes.
For close frames, returns an empty Bytes.
Trait Implementations§
Source§impl From<Message> for Message
impl From<Message> for Message
Source§fn from(value: Message) -> Self
fn from(value: Message) -> Self
Convert a neutral Message into a sockudo [SockudoMessage].
Conversion is infallible: both enums carry payloads as bytes::Bytes across
all variants. Sockudo validates UTF-8 on Text frames at parse time, not at
send time, so feeding it non-UTF-8 bytes via [Self::Text] is the caller’s
responsibility.
Source§impl<S> Sink<Message> for SockudoTransport<S>where
S: AsyncRead + AsyncWrite + Unpin,
impl<S> Sink<Message> for SockudoTransport<S>where
S: AsyncRead + AsyncWrite + Unpin,
Source§type Error = TransportError
type Error = TransportError
Source§fn poll_ready(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>>
fn poll_ready( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Result<(), Self::Error>>
Sink to receive a value. Read moreSource§fn start_send(self: Pin<&mut Self>, item: Message) -> Result<(), Self::Error>
fn start_send(self: Pin<&mut Self>, item: Message) -> Result<(), Self::Error>
poll_ready which returned Poll::Ready(Ok(())). Read moreSource§impl<S> Sink<Message> for TungsteniteTransport<S>where
S: AsyncRead + AsyncWrite + Unpin,
impl<S> Sink<Message> for TungsteniteTransport<S>where
S: AsyncRead + AsyncWrite + Unpin,
Source§type Error = TransportError
type Error = TransportError
Source§fn poll_ready(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>>
fn poll_ready( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Result<(), Self::Error>>
Sink to receive a value. Read moreSource§fn start_send(self: Pin<&mut Self>, item: Message) -> Result<(), Self::Error>
fn start_send(self: Pin<&mut Self>, item: Message) -> Result<(), Self::Error>
poll_ready which returned Poll::Ready(Ok(())). Read moreSource§impl TryFrom<Message> for Message
impl TryFrom<Message> for Message
Source§fn try_from(value: Message) -> Result<Self, Self::Error>
fn try_from(value: Message) -> Result<Self, Self::Error>
Convert a neutral Message into a tungstenite [tungstenite::Message].
Validates the Text payload as UTF-8 because tungstenite refuses to
transmit a Text frame whose body is not valid UTF-8. Other variants
are infallible.
§Errors
Returns TransportError::InvalidUtf8 if a Text payload is not
valid UTF-8.
Source§type Error = TransportError
type Error = TransportError
impl Eq for Message
impl StructuralPartialEq for Message
Auto Trait Implementations§
impl !Freeze for Message
impl RefUnwindSafe for Message
impl Send for Message
impl Sync for Message
impl Unpin for Message
impl UnsafeUnpin for Message
impl UnwindSafe for Message
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more