Skip to content

Commit b4dbb2f

Browse files
committed
Make conversion from native message to Message private
Signed-off-by: Yuki Kishimoto <yukikishimoto@protonmail.com>
1 parent cdc49c3 commit b4dbb2f

2 files changed

Lines changed: 16 additions & 18 deletions

File tree

src/message.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ pub enum Message {
4242
}
4343

4444
impl Message {
45+
#[cfg(not(target_arch = "wasm32"))]
46+
pub(crate) fn from_native(msg: TungsteniteMessage) -> Self {
47+
match msg {
48+
TungsteniteMessage::Text(text) => Self::Text(text.to_string()),
49+
TungsteniteMessage::Binary(data) => Self::Binary(data.to_vec()),
50+
TungsteniteMessage::Ping(data) => Self::Ping(data.to_vec()),
51+
TungsteniteMessage::Pong(data) => Self::Pong(data.to_vec()),
52+
TungsteniteMessage::Close(frame) => Self::Close(frame.map(|f| f.into())),
53+
// SAFETY: from tungstenite docs: "you're not going to get this value while reading the message".
54+
// SAFETY: this conversion is used only in Stream trait, so when reading the messages.
55+
TungsteniteMessage::Frame(..) => unreachable!(),
56+
}
57+
}
58+
4559
/// Get the length of the WebSocket message.
4660
#[inline]
4761
pub fn len(&self) -> usize {
@@ -120,19 +134,3 @@ impl From<TungsteniteCloseFrame> for CloseFrame {
120134
}
121135
}
122136
}
123-
124-
#[cfg(not(target_arch = "wasm32"))]
125-
impl From<TungsteniteMessage> for Message {
126-
fn from(msg: TungsteniteMessage) -> Self {
127-
match msg {
128-
TungsteniteMessage::Text(text) => Self::Text(text.to_string()),
129-
TungsteniteMessage::Binary(data) => Self::Binary(data.to_vec()),
130-
TungsteniteMessage::Ping(data) => Self::Ping(data.to_vec()),
131-
TungsteniteMessage::Pong(data) => Self::Pong(data.to_vec()),
132-
TungsteniteMessage::Close(frame) => Self::Close(frame.map(|f| f.into())),
133-
// SAFETY: from tungstenite docs: "you're not going to get this value while reading the message".
134-
// SAFETY: this conversion is used only in Stream trait, so when reading the messages.
135-
TungsteniteMessage::Frame(..) => unreachable!(),
136-
}
137-
}
138-
}

src/socket.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ impl Stream for WebSocket {
8585
#[cfg(not(target_arch = "wasm32"))]
8686
Self::Tokio(s) => Pin::new(s)
8787
.poll_next(cx)
88-
.map(|i| i.map(|res| res.map(|msg| msg.into())))
88+
.map(|i| i.map(|res| res.map(Message::from_native)))
8989
.map_err(Into::into),
9090
#[cfg(all(feature = "tor", not(target_arch = "wasm32")))]
9191
Self::Tor(s) => Pin::new(s)
9292
.poll_next(cx)
93-
.map(|i| i.map(|res| res.map(|msg| msg.into())))
93+
.map(|i| i.map(|res| res.map(Message::from_native)))
9494
.map_err(Into::into),
9595
#[cfg(target_arch = "wasm32")]
9696
Self::Wasm(s) => Pin::new(s).poll_next(cx).map_err(Into::into),

0 commit comments

Comments
 (0)