@@ -68,18 +68,13 @@ pub(crate) enum Message<T> where T: core::fmt::Debug + Type {
68
68
ReplyChannelRange ( msgs:: ReplyChannelRange ) ,
69
69
GossipTimestampFilter ( msgs:: GossipTimestampFilter ) ,
70
70
/// A message that could not be decoded because its type is unknown.
71
- Unknown ( MessageType ) ,
71
+ Unknown ( u16 ) ,
72
72
Custom ( T ) ,
73
73
}
74
74
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
-
79
75
impl < T > Message < T > where T : core:: fmt:: Debug + Type {
80
- #[ allow( dead_code) ] // This method is only used in tests
81
76
/// 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 {
83
78
match self {
84
79
& Message :: Init ( ref msg) => msg. type_id ( ) ,
85
80
& Message :: Error ( ref msg) => msg. type_id ( ) ,
@@ -113,18 +108,10 @@ impl<T> Message<T> where T: core::fmt::Debug + Type {
113
108
& Message :: Custom ( ref msg) => msg. type_id ( ) ,
114
109
}
115
110
}
116
- }
117
111
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.
120
113
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
128
115
}
129
116
}
130
117
@@ -232,7 +219,7 @@ where
232
219
if let Some ( custom) = custom_reader. read ( message_type, buffer) ? {
233
220
Ok ( Message :: Custom ( custom) )
234
221
} else {
235
- Ok ( Message :: Unknown ( MessageType ( message_type) ) )
222
+ Ok ( Message :: Unknown ( message_type) )
236
223
}
237
224
} ,
238
225
}
@@ -245,7 +232,7 @@ where
245
232
///
246
233
/// Returns an I/O error if the write could not be completed.
247
234
pub 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) ?;
249
236
message. write ( buffer)
250
237
}
251
238
@@ -264,12 +251,12 @@ pub(crate) use self::encode::Encode;
264
251
/// Messages implementing this trait specify a type and must be [`Writeable`] to use with [`write()`].
265
252
pub trait Type {
266
253
/// Returns the type identifying the message payload.
267
- fn type_id ( & self ) -> MessageType ;
254
+ fn type_id ( & self ) -> u16 ;
268
255
}
269
256
270
257
impl < 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
273
260
}
274
261
}
275
262
@@ -440,7 +427,7 @@ mod tests {
440
427
let mut reader = io:: Cursor :: new ( buffer) ;
441
428
let message = read ( & mut reader, & IgnoringCustomMessageHandler { } ) . unwrap ( ) ;
442
429
match message {
443
- Message :: Unknown ( MessageType ( :: core:: u16:: MAX ) ) => ( ) ,
430
+ Message :: Unknown ( :: core:: u16:: MAX ) => ( ) ,
444
431
_ => panic ! ( "Expected message type {}; found: {}" , :: core:: u16 :: MAX , message. type_id( ) ) ,
445
432
}
446
433
}
@@ -476,14 +463,14 @@ mod tests {
476
463
477
464
#[ test]
478
465
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( ) ) ;
481
468
}
482
469
483
470
#[ test]
484
471
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( ) ) ;
487
474
}
488
475
489
476
#[ test]
@@ -553,8 +540,8 @@ mod tests {
553
540
const CUSTOM_MESSAGE_TYPE : u16 = 9000 ;
554
541
555
542
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
558
545
}
559
546
}
560
547
@@ -591,7 +578,7 @@ mod tests {
591
578
let decoded_msg = read ( & mut reader, & TestCustomMessageReader { } ) . unwrap ( ) ;
592
579
match decoded_msg {
593
580
Message :: Custom ( custom) => {
594
- assert_eq ! ( custom. type_id( ) . 0 , CUSTOM_MESSAGE_TYPE ) ;
581
+ assert_eq ! ( custom. type_id( ) , CUSTOM_MESSAGE_TYPE ) ;
595
582
assert_eq ! ( custom, TestCustomMessage { } ) ;
596
583
} ,
597
584
_ => panic ! ( "Expected custom message, found message type: {}" , decoded_msg. type_id( ) ) ,
0 commit comments