Skip to content

Commit a2738fd

Browse files
authored
swarm-derive/: Derive Debug for generated OutEvent (#2821)
When generating an `OutEvent` `enum` definition for a user, derive `Debug` for that `enum`. Why not derive `Clone`, `PartialEq` and `Eq` for the generated `enum` definition? While it is fine to require all sub-`OutEvent`s to implement `Debug`, the same does not apply to traits like `Clone`. I suggest users that need `Clone` to define their own `OutEvent`.
1 parent d2c5053 commit a2738fd

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

swarm-derive/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
159159
let visibility = &ast.vis;
160160

161161
Some(quote! {
162+
#[derive(::std::fmt::Debug)]
162163
#visibility enum #name #impl_generics
163164
#where_clause
164165
{

swarm-derive/tests/test.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use futures::prelude::*;
2222
use libp2p::swarm::{NetworkBehaviour, SwarmEvent};
2323
use libp2p_swarm_derive::*;
24+
use std::fmt::Debug;
2425

2526
/// Small utility to check that a type implements `NetworkBehaviour`.
2627
#[allow(dead_code)]
@@ -275,7 +276,10 @@ fn custom_event_mismatching_field_names() {
275276
fn bound() {
276277
#[allow(dead_code)]
277278
#[derive(NetworkBehaviour)]
278-
struct Foo<T: Copy + NetworkBehaviour> {
279+
struct Foo<T: Copy + NetworkBehaviour>
280+
where
281+
<T as NetworkBehaviour>::OutEvent: Debug,
282+
{
279283
ping: libp2p::ping::Ping,
280284
bar: T,
281285
}
@@ -288,6 +292,7 @@ fn where_clause() {
288292
struct Foo<T>
289293
where
290294
T: Copy + NetworkBehaviour,
295+
<T as NetworkBehaviour>::OutEvent: Debug,
291296
{
292297
ping: libp2p::ping::Ping,
293298
bar: T,
@@ -489,3 +494,21 @@ fn event_process() {
489494
};
490495
}
491496
}
497+
498+
#[test]
499+
fn generated_out_event_derive_debug() {
500+
#[allow(dead_code)]
501+
#[derive(NetworkBehaviour)]
502+
struct Foo {
503+
ping: libp2p::ping::Ping,
504+
}
505+
506+
fn require_debug<T>()
507+
where
508+
T: NetworkBehaviour,
509+
<T as NetworkBehaviour>::OutEvent: Debug,
510+
{
511+
}
512+
513+
require_debug::<Foo>();
514+
}

0 commit comments

Comments
 (0)