@@ -20,7 +20,7 @@ use util::ser::{Readable, Writeable, Writer};
20
20
/// decoders.
21
21
pub trait CustomMessageReader {
22
22
/// The type of the message decoded by the implementation.
23
- type CustomMessage : core:: fmt:: Debug + TypedMessage + Writeable ;
23
+ type CustomMessage : core:: fmt:: Debug + Type + Writeable ;
24
24
/// Decodes a custom message to `CustomMessageType`. If the given message type is known to the
25
25
/// implementation and the message could be decoded, must return `Ok(Some(message))`. If the
26
26
/// message type is unknown to the implementation, must return `Ok(None)`. If a decoding error
@@ -32,7 +32,7 @@ pub trait CustomMessageReader {
32
32
/// variant contains a message from [`msgs`] or otherwise the message type if unknown.
33
33
#[ allow( missing_docs) ]
34
34
#[ derive( Debug ) ]
35
- pub ( crate ) enum Message < T > where T : core:: fmt:: Debug + TypedMessage {
35
+ pub ( crate ) enum Message < T > where T : core:: fmt:: Debug + Type {
36
36
Init ( msgs:: Init ) ,
37
37
Error ( msgs:: ErrorMessage ) ,
38
38
Ping ( msgs:: Ping ) ,
@@ -72,7 +72,7 @@ pub(crate) enum Message<T> where T: core::fmt::Debug + TypedMessage {
72
72
#[ derive( Clone , Copy , Debug ) ]
73
73
pub struct MessageType ( u16 ) ;
74
74
75
- impl < T > Message < T > where T : core:: fmt:: Debug + TypedMessage {
75
+ impl < T > Message < T > where T : core:: fmt:: Debug + Type {
76
76
#[ allow( dead_code) ] // This method is only used in tests
77
77
/// Returns the type that was used to decode the message payload.
78
78
pub fn type_id ( & self ) -> MessageType {
@@ -106,7 +106,7 @@ impl<T> Message<T> where T: core::fmt::Debug + TypedMessage {
106
106
& Message :: ReplyChannelRange ( ref msg) => msg. type_id ( ) ,
107
107
& Message :: GossipTimestampFilter ( ref msg) => msg. type_id ( ) ,
108
108
& Message :: Unknown ( type_id) => type_id,
109
- & Message :: Custom ( ref msg) => MessageType ( msg. msg_type ( ) ) ,
109
+ & Message :: Custom ( ref msg) => msg. type_id ( ) ,
110
110
}
111
111
}
112
112
}
@@ -135,7 +135,7 @@ pub(crate) fn read<R: io::Read, T, H: core::ops::Deref>(
135
135
custom_reader : & H ,
136
136
) -> Result < Message < T > , msgs:: DecodeError >
137
137
where
138
- T : core:: fmt:: Debug + TypedMessage + Writeable ,
138
+ T : core:: fmt:: Debug + Type + Writeable ,
139
139
H :: Target : CustomMessageReader < CustomMessage = T > ,
140
140
{
141
141
let message_type = <u16 as Readable >:: read ( buffer) ?;
@@ -240,39 +240,32 @@ where
240
240
/// # Errors
241
241
///
242
242
/// Returns an I/O error if the write could not be completed.
243
- pub ( crate ) fn write < M : TypedMessage + Writeable , W : Writer > ( message : & M , buffer : & mut W ) -> Result < ( ) , io:: Error > {
244
- message. msg_type ( ) . write ( buffer) ?;
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) ?;
245
245
message. write ( buffer)
246
246
}
247
247
248
- pub ( crate ) mod encode {
249
- use super :: * ;
250
- /// Defines a type-identified encoding for sending messages over the wire.
251
- ///
252
- /// Messages implementing this trait specify a type and must be [`Writeable`] to use with [`write()`].
248
+ mod encode {
249
+ /// Defines a constant type identifier for reading messages from the wire.
253
250
pub trait Encode {
254
251
/// The type identifying the message payload.
255
252
const TYPE : u16 ;
256
-
257
- /// Returns the type identifying the message payload. Convenience method for accessing
258
- /// [`Self::TYPE`].
259
- fn type_id ( & self ) -> MessageType {
260
- MessageType ( Self :: TYPE )
261
- }
262
253
}
263
254
}
264
255
265
256
pub ( crate ) use self :: encode:: Encode ;
266
257
267
- /// A message that has an associated type id.
268
- pub trait TypedMessage {
269
- /// The type id for the implementing message.
270
- fn msg_type ( & self ) -> u16 ;
258
+ /// Defines a type identifier for sending messages over the wire.
259
+ ///
260
+ /// Messages implementing this trait specify a type and must be [`Writeable`].
261
+ pub trait Type {
262
+ /// Returns the type identifying the message payload.
263
+ fn type_id ( & self ) -> MessageType ;
271
264
}
272
265
273
- impl < T > TypedMessage for T where T : Encode {
274
- fn msg_type ( & self ) -> u16 {
275
- T :: TYPE
266
+ impl < T > Type for T where T : Encode {
267
+ fn type_id ( & self ) -> MessageType {
268
+ MessageType ( T :: TYPE )
276
269
}
277
270
}
278
271
@@ -555,9 +548,9 @@ mod tests {
555
548
556
549
const CUSTOM_MESSAGE_TYPE : u16 = 9000 ;
557
550
558
- impl TypedMessage for TestCustomMessage {
559
- fn msg_type ( & self ) -> u16 {
560
- CUSTOM_MESSAGE_TYPE
551
+ impl Type for TestCustomMessage {
552
+ fn type_id ( & self ) -> MessageType {
553
+ MessageType ( CUSTOM_MESSAGE_TYPE )
561
554
}
562
555
}
563
556
@@ -591,7 +584,7 @@ mod tests {
591
584
let decoded_msg = read ( & mut reader, & & TestCustomMessageReader { } ) . unwrap ( ) ;
592
585
match decoded_msg {
593
586
Message :: Custom ( custom) => {
594
- assert_eq ! ( custom. msg_type ( ) , CUSTOM_MESSAGE_TYPE ) ;
587
+ assert_eq ! ( custom. type_id ( ) . 0 , CUSTOM_MESSAGE_TYPE ) ;
595
588
assert_eq ! ( custom, TestCustomMessage { } ) ;
596
589
} ,
597
590
_ => panic ! ( "Expected custom message, found message type: {}" , decoded_msg. type_id( ) ) ,
0 commit comments