@@ -42,6 +42,20 @@ pub enum Message {
4242}
4343
4444impl 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- }
0 commit comments