@@ -62,20 +62,15 @@ pub(crate) enum Message<T> where T: core::fmt::Debug + Type {
62
62
ReplyChannelRange ( msgs:: ReplyChannelRange ) ,
63
63
GossipTimestampFilter ( msgs:: GossipTimestampFilter ) ,
64
64
/// A message that could not be decoded because its type is unknown.
65
- Unknown ( MessageType ) ,
65
+ Unknown ( u16 ) ,
66
66
/// A message that was produced by a [`CustomMessageReader`] and is to be handled by a
67
67
/// [`peer_handler::CustomMessageHandler`].
68
68
Custom ( T ) ,
69
69
}
70
70
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
-
75
71
impl < T > Message < T > where T : core:: fmt:: Debug + Type {
76
- #[ allow( dead_code) ] // This method is only used in tests
77
72
/// 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 {
79
74
match self {
80
75
& Message :: Init ( ref msg) => msg. type_id ( ) ,
81
76
& Message :: Error ( ref msg) => msg. type_id ( ) ,
@@ -109,18 +104,10 @@ impl<T> Message<T> where T: core::fmt::Debug + Type {
109
104
& Message :: Custom ( ref msg) => msg. type_id ( ) ,
110
105
}
111
106
}
112
- }
113
107
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.
116
109
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
124
111
}
125
112
}
126
113
@@ -228,7 +215,7 @@ where
228
215
if let Some ( custom) = custom_reader. read ( message_type, buffer) ? {
229
216
Ok ( Message :: Custom ( custom) )
230
217
} else {
231
- Ok ( Message :: Unknown ( MessageType ( message_type) ) )
218
+ Ok ( Message :: Unknown ( message_type) )
232
219
}
233
220
} ,
234
221
}
@@ -240,8 +227,8 @@ where
240
227
/// # Errors
241
228
///
242
229
/// Returns an I/O error if the write could not be completed.
243
- pub ( crate ) fn write < M : Type + Writeable , W : Writer > ( message : & M , buffer : & mut W ) -> Result < ( ) , io:: Error > {
244
- message. type_id ( ) . 0 . write ( buffer) ?;
230
+ pub fn write < M : Type + Writeable , W : Writer > ( message : & M , buffer : & mut W ) -> Result < ( ) , io:: Error > {
231
+ message. type_id ( ) . write ( buffer) ?;
245
232
message. write ( buffer)
246
233
}
247
234
@@ -260,12 +247,12 @@ pub(crate) use self::encode::Encode;
260
247
/// Messages implementing this trait specify a type and must be [`Writeable`].
261
248
pub trait Type {
262
249
/// Returns the type identifying the message payload.
263
- fn type_id ( & self ) -> MessageType ;
250
+ fn type_id ( & self ) -> u16 ;
264
251
}
265
252
266
253
impl < 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
269
256
}
270
257
}
271
258
@@ -436,7 +423,7 @@ mod tests {
436
423
let mut reader = io:: Cursor :: new ( buffer) ;
437
424
let message = read ( & mut reader, & IgnoringMessageHandler { } ) . unwrap ( ) ;
438
425
match message {
439
- Message :: Unknown ( MessageType ( :: core:: u16:: MAX ) ) => ( ) ,
426
+ Message :: Unknown ( :: core:: u16:: MAX ) => ( ) ,
440
427
_ => panic ! ( "Expected message type {}; found: {}" , :: core:: u16 :: MAX , message. type_id( ) ) ,
441
428
}
442
429
}
@@ -472,14 +459,14 @@ mod tests {
472
459
473
460
#[ test]
474
461
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( ) ) ;
477
464
}
478
465
479
466
#[ test]
480
467
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( ) ) ;
483
470
}
484
471
485
472
#[ test]
@@ -549,8 +536,8 @@ mod tests {
549
536
const CUSTOM_MESSAGE_TYPE : u16 = 9000 ;
550
537
551
538
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
554
541
}
555
542
}
556
543
@@ -587,7 +574,7 @@ mod tests {
587
574
let decoded_msg = read ( & mut reader, & TestCustomMessageReader { } ) . unwrap ( ) ;
588
575
match decoded_msg {
589
576
Message :: Custom ( custom) => {
590
- assert_eq ! ( custom. type_id( ) . 0 , CUSTOM_MESSAGE_TYPE ) ;
577
+ assert_eq ! ( custom. type_id( ) , CUSTOM_MESSAGE_TYPE ) ;
591
578
assert_eq ! ( custom, TestCustomMessage { } ) ;
592
579
} ,
593
580
_ => panic ! ( "Expected custom message, found message type: {}" , decoded_msg. type_id( ) ) ,
0 commit comments