@@ -114,7 +114,7 @@ use std::{
114114use tokio_tungstenite:: {
115115 tungstenite:: {
116116 self as ts,
117- protocol:: { self , WebSocketConfig } ,
117+ protocol:: { self , frame :: { Payload , Utf8Payload } , WebSocketConfig } ,
118118 } ,
119119 WebSocketStream ,
120120} ;
@@ -591,24 +591,24 @@ pub struct CloseFrame<'t> {
591591#[ derive( Debug , Eq , PartialEq , Clone ) ]
592592pub enum Message {
593593 /// A text WebSocket message
594- Text ( String ) ,
594+ Text ( Utf8Payload ) ,
595595 /// A binary WebSocket message
596- Binary ( Vec < u8 > ) ,
596+ Binary ( Payload ) ,
597597 /// A ping message with the specified payload
598598 ///
599599 /// The payload here must have a length less than 125 bytes.
600600 ///
601601 /// Ping messages will be automatically responded to by the server, so you do not have to worry
602602 /// about dealing with them yourself.
603- Ping ( Vec < u8 > ) ,
603+ Ping ( Payload ) ,
604604 /// A pong message with the specified payload
605605 ///
606606 /// The payload here must have a length less than 125 bytes.
607607 ///
608608 /// Pong messages will be automatically sent to the client if a ping message is received, so
609609 /// you do not have to worry about constructing them yourself unless you want to implement a
610610 /// [unidirectional heartbeat](https://tools.ietf.org/html/rfc6455#section-5.5.3).
611- Pong ( Vec < u8 > ) ,
611+ Pong ( Payload ) ,
612612 /// A close message with the optional close frame.
613613 ///
614614 /// You may "uncleanly" close a WebSocket connection at any time
@@ -666,8 +666,8 @@ impl Message {
666666 /// Consume the WebSocket and return it as binary data.
667667 pub fn into_data ( self ) -> Vec < u8 > {
668668 match self {
669- Self :: Text ( string) => string. into_bytes ( ) ,
670- Self :: Binary ( data) | Self :: Ping ( data) | Self :: Pong ( data) => data,
669+ Self :: Text ( string) => string. to_string ( ) . into_bytes ( ) ,
670+ Self :: Binary ( data) | Self :: Ping ( data) | Self :: Pong ( data) => data. as_slice ( ) . to_vec ( ) ,
671671 Self :: Close ( None ) => Vec :: new ( ) ,
672672 Self :: Close ( Some ( frame) ) => frame. reason . into_owned ( ) . into_bytes ( ) ,
673673 }
@@ -676,8 +676,8 @@ impl Message {
676676 /// Attempt to consume the WebSocket message and convert it to a String.
677677 pub fn into_text ( self ) -> Result < String , Error > {
678678 match self {
679- Self :: Text ( string) => Ok ( string) ,
680- Self :: Binary ( data) | Self :: Ping ( data) | Self :: Pong ( data) => Ok ( String :: from_utf8 ( data)
679+ Self :: Text ( string) => Ok ( string. to_string ( ) ) ,
680+ Self :: Binary ( data) | Self :: Ping ( data) | Self :: Pong ( data) => Ok ( String :: from_utf8 ( data. as_slice ( ) . to_vec ( ) )
681681 . map_err ( |err| err. utf8_error ( ) )
682682 . map_err ( Error :: new) ?) ,
683683 Self :: Close ( None ) => Ok ( String :: new ( ) ) ,
@@ -689,9 +689,9 @@ impl Message {
689689 /// this will try to convert binary data to utf8.
690690 pub fn to_text ( & self ) -> Result < & str , Error > {
691691 match * self {
692- Self :: Text ( ref string) => Ok ( string) ,
692+ Self :: Text ( ref string) => Ok ( string. as_str ( ) ) ,
693693 Self :: Binary ( ref data) | Self :: Ping ( ref data) | Self :: Pong ( ref data) => {
694- Ok ( std:: str:: from_utf8 ( data) . map_err ( Error :: new) ?)
694+ Ok ( std:: str:: from_utf8 ( data. as_slice ( ) ) . map_err ( Error :: new) ?)
695695 }
696696 Self :: Close ( None ) => Ok ( "" ) ,
697697 Self :: Close ( Some ( ref frame) ) => Ok ( & frame. reason ) ,
@@ -701,7 +701,7 @@ impl Message {
701701
702702impl From < String > for Message {
703703 fn from ( string : String ) -> Self {
704- Message :: Text ( string)
704+ Message :: Text ( string. into ( ) )
705705 }
706706}
707707
@@ -719,7 +719,7 @@ impl<'b> From<&'b [u8]> for Message {
719719
720720impl From < Vec < u8 > > for Message {
721721 fn from ( data : Vec < u8 > ) -> Self {
722- Message :: Binary ( data)
722+ Message :: Binary ( data. into ( ) )
723723 }
724724}
725725
0 commit comments