13
13
//! The [`Message`] enum returned by [`read()`] wraps the decoded message or the message type (if
14
14
//! unknown) to use with pattern matching.
15
15
//!
16
- //! Messages implementing the [`Encode `] trait define a message type and can be sent over the wire
16
+ //! Messages implementing the [`Type `] trait define a message type and can be sent over the wire
17
17
//! using [`write()`].
18
18
//!
19
19
//! [BOLT #1]: https://github.com/lightningnetwork/lightning-rfc/blob/master/01-messaging.md
@@ -26,7 +26,7 @@ use util::ser::{Readable, Writeable, Writer};
26
26
/// decoders.
27
27
pub trait CustomMessageReader {
28
28
/// The type of the message decoded by the implementation.
29
- type CustomMessage : core:: fmt:: Debug + TypedMessage + Writeable ;
29
+ type CustomMessage : core:: fmt:: Debug + Type + Writeable ;
30
30
/// Decodes a custom message to `CustomMessageType`. If the given message type is known to the implementation and
31
31
/// the message could be decoded, must return `Ok(Some(message))`. If the message type
32
32
/// is unknown to the implementation, must return `Ok(None)`. If a decoding error
@@ -38,7 +38,7 @@ pub trait CustomMessageReader {
38
38
/// variant contains a message from [`msgs`] or otherwise the message type if unknown.
39
39
#[ allow( missing_docs) ]
40
40
#[ derive( Debug ) ]
41
- pub ( crate ) enum Message < T > where T : core:: fmt:: Debug + TypedMessage {
41
+ pub ( crate ) enum Message < T > where T : core:: fmt:: Debug + Type {
42
42
Init ( msgs:: Init ) ,
43
43
Error ( msgs:: ErrorMessage ) ,
44
44
Ping ( msgs:: Ping ) ,
@@ -76,7 +76,7 @@ pub(crate) enum Message<T> where T: core::fmt::Debug + TypedMessage {
76
76
#[ derive( Clone , Copy , Debug ) ]
77
77
pub struct MessageType ( u16 ) ;
78
78
79
- impl < T > Message < T > where T : core:: fmt:: Debug + TypedMessage {
79
+ impl < T > Message < T > where T : core:: fmt:: Debug + Type {
80
80
#[ allow( dead_code) ] // This method is only used in tests
81
81
/// Returns the type that was used to decode the message payload.
82
82
pub fn type_id ( & self ) -> MessageType {
@@ -110,7 +110,7 @@ impl<T> Message<T> where T: core::fmt::Debug + TypedMessage {
110
110
& Message :: ReplyChannelRange ( ref msg) => msg. type_id ( ) ,
111
111
& Message :: GossipTimestampFilter ( ref msg) => msg. type_id ( ) ,
112
112
& Message :: Unknown ( type_id) => type_id,
113
- & Message :: Custom ( ref msg) => MessageType ( msg. msg_type ( ) ) ,
113
+ & Message :: Custom ( ref msg) => msg. type_id ( ) ,
114
114
}
115
115
}
116
116
}
@@ -139,7 +139,7 @@ pub(crate) fn read<R: io::Read, T, H: core::ops::Deref>(
139
139
custom_reader : & H
140
140
) -> Result < Message < T > , msgs:: DecodeError >
141
141
where
142
- T : core:: fmt:: Debug + TypedMessage + Writeable ,
142
+ T : core:: fmt:: Debug + Type + Writeable ,
143
143
H :: Target : CustomMessageReader < CustomMessage = T >
144
144
{
145
145
let message_type = <u16 as Readable >:: read ( buffer) ?;
@@ -244,39 +244,32 @@ where
244
244
/// # Errors
245
245
///
246
246
/// Returns an I/O error if the write could not be completed.
247
- pub fn write < M : TypedMessage + Writeable , W : Writer > ( message : & M , buffer : & mut W ) -> Result < ( ) , io:: Error > {
248
- message. msg_type ( ) . write ( buffer) ?;
247
+ pub fn write < M : Type + Writeable , W : Writer > ( message : & M , buffer : & mut W ) -> Result < ( ) , io:: Error > {
248
+ message. type_id ( ) . 0 . write ( buffer) ?;
249
249
message. write ( buffer)
250
250
}
251
251
252
- pub ( crate ) mod encode {
253
- use super :: * ;
254
- /// Defines a type-identified encoding for sending messages over the wire.
255
- ///
256
- /// Messages implementing this trait specify a type and must be [`Writeable`] to use with [`write()`].
252
+ mod encode {
253
+ /// Defines a constant type identifier for reading messages from the wire.
257
254
pub trait Encode {
258
255
/// The type identifying the message payload.
259
256
const TYPE : u16 ;
260
-
261
- /// Returns the type identifying the message payload. Convenience method for accessing
262
- /// [`Self::TYPE`].
263
- fn type_id ( & self ) -> MessageType {
264
- MessageType ( Self :: TYPE )
265
- }
266
257
}
267
258
}
268
259
269
260
pub ( crate ) use self :: encode:: Encode ;
270
261
271
- /// A message that has an associated type id.
272
- pub trait TypedMessage {
273
- /// The type id for the implementing message.
274
- fn msg_type ( & self ) -> u16 ;
262
+ /// Defines a type identifier for sending messages over the wire.
263
+ ///
264
+ /// Messages implementing this trait specify a type and must be [`Writeable`] to use with [`write()`].
265
+ pub trait Type {
266
+ /// Returns the type identifying the message payload.
267
+ fn type_id ( & self ) -> MessageType ;
275
268
}
276
269
277
- impl < T > TypedMessage for T where T : Encode {
278
- fn msg_type ( & self ) -> u16 {
279
- T :: TYPE
270
+ impl < T > Type for T where T : Encode {
271
+ fn type_id ( & self ) -> MessageType {
272
+ MessageType ( T :: TYPE )
280
273
}
281
274
}
282
275
@@ -559,9 +552,9 @@ mod tests {
559
552
560
553
const CUSTOM_MESSAGE_TYPE : u16 = 9000 ;
561
554
562
- impl TypedMessage for TestCustomMessage {
563
- fn msg_type ( & self ) -> u16 {
564
- CUSTOM_MESSAGE_TYPE
555
+ impl Type for TestCustomMessage {
556
+ fn type_id ( & self ) -> MessageType {
557
+ MessageType ( CUSTOM_MESSAGE_TYPE )
565
558
}
566
559
}
567
560
@@ -598,7 +591,7 @@ mod tests {
598
591
let decoded_msg = read ( & mut reader, & TestCustomMessageReader { } ) . unwrap ( ) ;
599
592
match decoded_msg {
600
593
Message :: Custom ( custom) => {
601
- assert_eq ! ( custom. msg_type ( ) , CUSTOM_MESSAGE_TYPE ) ;
594
+ assert_eq ! ( custom. type_id ( ) . 0 , CUSTOM_MESSAGE_TYPE ) ;
602
595
assert_eq ! ( custom, TestCustomMessage { } ) ;
603
596
} ,
604
597
_ => panic ! ( "Expected custom message, found message type: {}" , decoded_msg. type_id( ) ) ,
0 commit comments