Skip to content

Commit a9f7ee5

Browse files
committed
Add From and Into for tungstenite's websocket message
1 parent c772419 commit a9f7ee5

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/filters/ws.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ impl Stream for WebSocket {
207207

208208
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
209209
match ready!(Pin::new(&mut self.inner).poll_next(cx)) {
210-
Some(Ok(item)) => Poll::Ready(Some(Ok(Message::from_tungstenite(item)))),
210+
Some(Ok(item)) => Poll::Ready(Some(Ok(Message::from(item)))),
211211
Some(Err(e)) => {
212212
tracing::debug!("websocket poll error: {}", e);
213213
Poll::Ready(Some(Err(crate::Error::new(e))))
@@ -231,7 +231,7 @@ impl Sink<Message> for WebSocket {
231231
}
232232

233233
fn start_send(mut self: Pin<&mut Self>, item: Message) -> Result<(), Self::Error> {
234-
match Pin::new(&mut self.inner).start_send(item.into_tungstenite()) {
234+
match Pin::new(&mut self.inner).start_send(item.into()) {
235235
Ok(()) => Ok(()),
236236
Err(e) => {
237237
tracing::debug!("websocket start_send error: {}", e);
@@ -379,8 +379,10 @@ impl Message {
379379
Message::Close(None) => Vec::new(),
380380
}
381381
}
382+
}
382383

383-
fn from_tungstenite(message: protocol::Message) -> Self {
384+
impl From<protocol::Message> for Message {
385+
fn from(message: protocol::Message) -> Self {
384386
use protocol::Message::*;
385387

386388
match message {
@@ -392,11 +394,13 @@ impl Message {
392394
Close(None) => Message::Close(None),
393395
}
394396
}
397+
}
395398

396-
fn into_tungstenite(self) -> protocol::Message {
399+
impl From<Message> for protocol::Message {
400+
fn from(message: Message) -> Self {
397401
use protocol::Message::*;
398402

399-
match self {
403+
match message {
400404
Message::Text(string) => Text(string),
401405
Message::Binary(bytes) => Binary(bytes),
402406
Message::Ping(bytes) => Ping(bytes),

0 commit comments

Comments
 (0)