@@ -28,11 +28,26 @@ pub trait CustomMessageReader {
28
28
fn read < R : io:: Read > ( & self , message_type : u16 , buffer : & mut R ) -> Result < Option < Self :: CustomMessage > , msgs:: DecodeError > ;
29
29
}
30
30
31
+ // TestEq is a dummy trait which requires PartialEq when built in testing, and otherwise is
32
+ // blanket-implemented for all types.
33
+
34
+ #[ cfg( test) ]
35
+ pub trait TestEq : PartialEq { }
36
+ #[ cfg( test) ]
37
+ impl < T : PartialEq > TestEq for T { }
38
+
39
+ #[ cfg( not( test) ) ]
40
+ pub ( crate ) trait TestEq { }
41
+ #[ cfg( not( test) ) ]
42
+ impl < T > TestEq for T { }
43
+
44
+
31
45
/// A Lightning message returned by [`read()`] when decoding bytes received over the wire. Each
32
46
/// variant contains a message from [`msgs`] or otherwise the message type if unknown.
33
47
#[ allow( missing_docs) ]
34
48
#[ derive( Debug ) ]
35
- pub ( crate ) enum Message < T > where T : core:: fmt:: Debug + Type {
49
+ #[ cfg_attr( test, derive( PartialEq ) ) ]
50
+ pub ( crate ) enum Message < T > where T : core:: fmt:: Debug + Type + TestEq {
36
51
Init ( msgs:: Init ) ,
37
52
Error ( msgs:: ErrorMessage ) ,
38
53
Warning ( msgs:: WarningMessage ) ,
@@ -69,7 +84,7 @@ pub(crate) enum Message<T> where T: core::fmt::Debug + Type {
69
84
Custom ( T ) ,
70
85
}
71
86
72
- impl < T > Message < T > where T : core:: fmt:: Debug + Type {
87
+ impl < T > Message < T > where T : core:: fmt:: Debug + Type + TestEq {
73
88
/// Returns the type that was used to decode the message payload.
74
89
pub fn type_id ( & self ) -> u16 {
75
90
match self {
@@ -252,6 +267,7 @@ mod encode {
252
267
253
268
pub ( crate ) use self :: encode:: Encode ;
254
269
270
+ #[ cfg( not( test) ) ]
255
271
/// Defines a type identifier for sending messages over the wire.
256
272
///
257
273
/// Messages implementing this trait specify a type and must be [`Writeable`].
@@ -260,10 +276,24 @@ pub trait Type: core::fmt::Debug + Writeable {
260
276
fn type_id ( & self ) -> u16 ;
261
277
}
262
278
279
+ #[ cfg( test) ]
280
+ pub trait Type : core:: fmt:: Debug + Writeable + PartialEq {
281
+ fn type_id ( & self ) -> u16 ;
282
+ }
283
+
284
+ #[ cfg( any( feature = "_test_utils" , fuzzing, test) ) ]
285
+ impl Type for ( ) {
286
+ fn type_id ( & self ) -> u16 { unreachable ! ( ) ; }
287
+ }
288
+
289
+ #[ cfg( test) ]
290
+ impl < T : core:: fmt:: Debug + Writeable + PartialEq > Type for T where T : Encode {
291
+ fn type_id ( & self ) -> u16 { T :: TYPE }
292
+ }
293
+
294
+ #[ cfg( not( test) ) ]
263
295
impl < T : core:: fmt:: Debug + Writeable > Type for T where T : Encode {
264
- fn type_id ( & self ) -> u16 {
265
- T :: TYPE
266
- }
296
+ fn type_id ( & self ) -> u16 { T :: TYPE }
267
297
}
268
298
269
299
impl Encode for msgs:: Init {
@@ -471,10 +501,6 @@ mod tests {
471
501
}
472
502
}
473
503
474
- impl Type for ( ) {
475
- fn type_id ( & self ) -> u16 { unreachable ! ( ) ; }
476
- }
477
-
478
504
#[ test]
479
505
fn is_even_message_type ( ) {
480
506
let message = Message :: < ( ) > :: Unknown ( 42 ) ;
0 commit comments