@@ -68,18 +68,13 @@ pub(crate) enum Message<T> where T: core::fmt::Debug + Type {
6868 ReplyChannelRange ( msgs:: ReplyChannelRange ) ,
6969 GossipTimestampFilter ( msgs:: GossipTimestampFilter ) ,
7070 /// A message that could not be decoded because its type is unknown.
71- Unknown ( MessageType ) ,
71+ Unknown ( u16 ) ,
7272 Custom ( T ) ,
7373}
7474
75- /// A number identifying a message to determine how it is encoded on the wire.
76- #[ derive( Clone , Copy , Debug ) ]
77- pub struct MessageType ( u16 ) ;
78-
7975impl < T > Message < T > where T : core:: fmt:: Debug + Type {
80- #[ allow( dead_code) ] // This method is only used in tests
8176 /// Returns the type that was used to decode the message payload.
82- pub fn type_id ( & self ) -> MessageType {
77+ pub fn type_id ( & self ) -> u16 {
8378 match self {
8479 & Message :: Init ( ref msg) => msg. type_id ( ) ,
8580 & Message :: Error ( ref msg) => msg. type_id ( ) ,
@@ -113,18 +108,10 @@ impl<T> Message<T> where T: core::fmt::Debug + Type {
113108 & Message :: Custom ( ref msg) => msg. type_id ( ) ,
114109 }
115110 }
116- }
117111
118- impl MessageType {
119- /// Returns whether the message type is even, indicating both endpoints must support it.
112+ /// Returns whether the message's type is even, indicating both endpoints must support it.
120113 pub fn is_even ( & self ) -> bool {
121- ( self . 0 & 1 ) == 0
122- }
123- }
124-
125- impl :: core:: fmt:: Display for MessageType {
126- fn fmt ( & self , f : & mut :: core:: fmt:: Formatter ) -> :: core:: fmt:: Result {
127- write ! ( f, "{}" , self . 0 )
114+ ( self . type_id ( ) & 1 ) == 0
128115 }
129116}
130117
@@ -232,7 +219,7 @@ where
232219 if let Some ( custom) = custom_reader. read ( message_type, buffer) ? {
233220 Ok ( Message :: Custom ( custom) )
234221 } else {
235- Ok ( Message :: Unknown ( MessageType ( message_type) ) )
222+ Ok ( Message :: Unknown ( message_type) )
236223 }
237224 } ,
238225 }
@@ -245,7 +232,7 @@ where
245232///
246233/// Returns an I/O error if the write could not be completed.
247234pub fn write < M : Type + Writeable , W : Writer > ( message : & M , buffer : & mut W ) -> Result < ( ) , io:: Error > {
248- message. type_id ( ) . 0 . write ( buffer) ?;
235+ message. type_id ( ) . write ( buffer) ?;
249236 message. write ( buffer)
250237}
251238
@@ -264,12 +251,12 @@ pub(crate) use self::encode::Encode;
264251/// Messages implementing this trait specify a type and must be [`Writeable`] to use with [`write()`].
265252pub trait Type {
266253 /// Returns the type identifying the message payload.
267- fn type_id ( & self ) -> MessageType ;
254+ fn type_id ( & self ) -> u16 ;
268255}
269256
270257impl < T > Type for T where T : Encode {
271- fn type_id ( & self ) -> MessageType {
272- MessageType ( T :: TYPE )
258+ fn type_id ( & self ) -> u16 {
259+ T :: TYPE
273260 }
274261}
275262
@@ -440,7 +427,7 @@ mod tests {
440427 let mut reader = io:: Cursor :: new ( buffer) ;
441428 let message = read ( & mut reader, & IgnoringCustomMessageHandler { } ) . unwrap ( ) ;
442429 match message {
443- Message :: Unknown ( MessageType ( :: core:: u16:: MAX ) ) => ( ) ,
430+ Message :: Unknown ( :: core:: u16:: MAX ) => ( ) ,
444431 _ => panic ! ( "Expected message type {}; found: {}" , :: core:: u16 :: MAX , message. type_id( ) ) ,
445432 }
446433 }
@@ -476,14 +463,14 @@ mod tests {
476463
477464 #[ test]
478465 fn is_even_message_type ( ) {
479- let message = Message :: < ( ) > :: Unknown ( MessageType ( 42 ) ) ;
480- assert ! ( message. type_id ( ) . is_even( ) ) ;
466+ let message = Message :: < ( ) > :: Unknown ( 42 ) ;
467+ assert ! ( message. is_even( ) ) ;
481468 }
482469
483470 #[ test]
484471 fn is_odd_message_type ( ) {
485- let message = Message :: < ( ) > :: Unknown ( MessageType ( 43 ) ) ;
486- assert ! ( !message. type_id ( ) . is_even( ) ) ;
472+ let message = Message :: < ( ) > :: Unknown ( 43 ) ;
473+ assert ! ( !message. is_even( ) ) ;
487474 }
488475
489476 #[ test]
@@ -553,8 +540,8 @@ mod tests {
553540 const CUSTOM_MESSAGE_TYPE : u16 = 9000 ;
554541
555542 impl Type for TestCustomMessage {
556- fn type_id ( & self ) -> MessageType {
557- MessageType ( CUSTOM_MESSAGE_TYPE )
543+ fn type_id ( & self ) -> u16 {
544+ CUSTOM_MESSAGE_TYPE
558545 }
559546 }
560547
@@ -591,7 +578,7 @@ mod tests {
591578 let decoded_msg = read ( & mut reader, & TestCustomMessageReader { } ) . unwrap ( ) ;
592579 match decoded_msg {
593580 Message :: Custom ( custom) => {
594- assert_eq ! ( custom. type_id( ) . 0 , CUSTOM_MESSAGE_TYPE ) ;
581+ assert_eq ! ( custom. type_id( ) , CUSTOM_MESSAGE_TYPE ) ;
595582 assert_eq ! ( custom, TestCustomMessage { } ) ;
596583 } ,
597584 _ => panic ! ( "Expected custom message, found message type: {}" , decoded_msg. type_id( ) ) ,
0 commit comments