@@ -62,20 +62,15 @@ pub(crate) enum Message<T> where T: core::fmt::Debug + Type {
6262 ReplyChannelRange ( msgs:: ReplyChannelRange ) ,
6363 GossipTimestampFilter ( msgs:: GossipTimestampFilter ) ,
6464 /// A message that could not be decoded because its type is unknown.
65- Unknown ( MessageType ) ,
65+ Unknown ( u16 ) ,
6666 /// A message that was produced by a [`CustomMessageReader`] and is to be handled by a
6767 /// [`::ln::peer_handler::CustomMessageHandler`].
6868 Custom ( T ) ,
6969}
7070
71- /// A number identifying a message to determine how it is encoded on the wire.
72- #[ derive( Clone , Copy , Debug ) ]
73- pub struct MessageType ( u16 ) ;
74-
7571impl < T > Message < T > where T : core:: fmt:: Debug + Type {
76- #[ allow( dead_code) ] // This method is only used in tests
7772 /// Returns the type that was used to decode the message payload.
78- pub fn type_id ( & self ) -> MessageType {
73+ pub fn type_id ( & self ) -> u16 {
7974 match self {
8075 & Message :: Init ( ref msg) => msg. type_id ( ) ,
8176 & Message :: Error ( ref msg) => msg. type_id ( ) ,
@@ -109,18 +104,10 @@ impl<T> Message<T> where T: core::fmt::Debug + Type {
109104 & Message :: Custom ( ref msg) => msg. type_id ( ) ,
110105 }
111106 }
112- }
113107
114- impl MessageType {
115- /// Returns whether the message type is even, indicating both endpoints must support it.
108+ /// Returns whether the message's type is even, indicating both endpoints must support it.
116109 pub fn is_even ( & self ) -> bool {
117- ( self . 0 & 1 ) == 0
118- }
119- }
120-
121- impl :: core:: fmt:: Display for MessageType {
122- fn fmt ( & self , f : & mut :: core:: fmt:: Formatter ) -> :: core:: fmt:: Result {
123- write ! ( f, "{}" , self . 0 )
110+ ( self . type_id ( ) & 1 ) == 0
124111 }
125112}
126113
@@ -228,7 +215,7 @@ where
228215 if let Some ( custom) = custom_reader. read ( message_type, buffer) ? {
229216 Ok ( Message :: Custom ( custom) )
230217 } else {
231- Ok ( Message :: Unknown ( MessageType ( message_type) ) )
218+ Ok ( Message :: Unknown ( message_type) )
232219 }
233220 } ,
234221 }
@@ -241,7 +228,7 @@ where
241228///
242229/// Returns an I/O error if the write could not be completed.
243230pub ( crate ) fn write < M : Type + Writeable , W : Writer > ( message : & M , buffer : & mut W ) -> Result < ( ) , io:: Error > {
244- message. type_id ( ) . 0 . write ( buffer) ?;
231+ message. type_id ( ) . write ( buffer) ?;
245232 message. write ( buffer)
246233}
247234
@@ -260,12 +247,12 @@ pub(crate) use self::encode::Encode;
260247/// Messages implementing this trait specify a type and must be [`Writeable`].
261248pub trait Type {
262249 /// Returns the type identifying the message payload.
263- fn type_id ( & self ) -> MessageType ;
250+ fn type_id ( & self ) -> u16 ;
264251}
265252
266253impl < T > Type for T where T : Encode {
267- fn type_id ( & self ) -> MessageType {
268- MessageType ( T :: TYPE )
254+ fn type_id ( & self ) -> u16 {
255+ T :: TYPE
269256 }
270257}
271258
@@ -436,7 +423,7 @@ mod tests {
436423 let mut reader = io:: Cursor :: new ( buffer) ;
437424 let message = read ( & mut reader, & IgnoringMessageHandler { } ) . unwrap ( ) ;
438425 match message {
439- Message :: Unknown ( MessageType ( :: core:: u16:: MAX ) ) => ( ) ,
426+ Message :: Unknown ( :: core:: u16:: MAX ) => ( ) ,
440427 _ => panic ! ( "Expected message type {}; found: {}" , :: core:: u16 :: MAX , message. type_id( ) ) ,
441428 }
442429 }
@@ -472,14 +459,14 @@ mod tests {
472459
473460 #[ test]
474461 fn is_even_message_type ( ) {
475- let message = Message :: < ( ) > :: Unknown ( MessageType ( 42 ) ) ;
476- assert ! ( message. type_id ( ) . is_even( ) ) ;
462+ let message = Message :: < ( ) > :: Unknown ( 42 ) ;
463+ assert ! ( message. is_even( ) ) ;
477464 }
478465
479466 #[ test]
480467 fn is_odd_message_type ( ) {
481- let message = Message :: < ( ) > :: Unknown ( MessageType ( 43 ) ) ;
482- assert ! ( !message. type_id ( ) . is_even( ) ) ;
468+ let message = Message :: < ( ) > :: Unknown ( 43 ) ;
469+ assert ! ( !message. is_even( ) ) ;
483470 }
484471
485472 #[ test]
@@ -549,8 +536,8 @@ mod tests {
549536 const CUSTOM_MESSAGE_TYPE : u16 = 9000 ;
550537
551538 impl Type for TestCustomMessage {
552- fn type_id ( & self ) -> MessageType {
553- MessageType ( CUSTOM_MESSAGE_TYPE )
539+ fn type_id ( & self ) -> u16 {
540+ CUSTOM_MESSAGE_TYPE
554541 }
555542 }
556543
@@ -587,7 +574,7 @@ mod tests {
587574 let decoded_msg = read ( & mut reader, & TestCustomMessageReader { } ) . unwrap ( ) ;
588575 match decoded_msg {
589576 Message :: Custom ( custom) => {
590- assert_eq ! ( custom. type_id( ) . 0 , CUSTOM_MESSAGE_TYPE ) ;
577+ assert_eq ! ( custom. type_id( ) , CUSTOM_MESSAGE_TYPE ) ;
591578 assert_eq ! ( custom, TestCustomMessage { } ) ;
592579 } ,
593580 _ => panic ! ( "Expected custom message, found message type: {}" , decoded_msg. type_id( ) ) ,
0 commit comments