From a2738fd5551c4ab0f234e2d33dd5500768bbaabb Mon Sep 17 00:00:00 2001 From: Max Inden Date: Wed, 17 Aug 2022 08:40:32 +0200 Subject: [PATCH] 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`. --- swarm-derive/src/lib.rs | 1 + swarm-derive/tests/test.rs | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/swarm-derive/src/lib.rs b/swarm-derive/src/lib.rs index 2ac9f978657..51305e38190 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -159,6 +159,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { let visibility = &ast.vis; Some(quote! { + #[derive(::std::fmt::Debug)] #visibility enum #name #impl_generics #where_clause { diff --git a/swarm-derive/tests/test.rs b/swarm-derive/tests/test.rs index d23dd8ed863..f4a313198a9 100644 --- a/swarm-derive/tests/test.rs +++ b/swarm-derive/tests/test.rs @@ -21,6 +21,7 @@ use futures::prelude::*; use libp2p::swarm::{NetworkBehaviour, SwarmEvent}; use libp2p_swarm_derive::*; +use std::fmt::Debug; /// Small utility to check that a type implements `NetworkBehaviour`. #[allow(dead_code)] @@ -275,7 +276,10 @@ fn custom_event_mismatching_field_names() { fn bound() { #[allow(dead_code)] #[derive(NetworkBehaviour)] - struct Foo { + struct Foo + where + ::OutEvent: Debug, + { ping: libp2p::ping::Ping, bar: T, } @@ -288,6 +292,7 @@ fn where_clause() { struct Foo where T: Copy + NetworkBehaviour, + ::OutEvent: Debug, { ping: libp2p::ping::Ping, bar: T, @@ -489,3 +494,21 @@ fn event_process() { }; } } + +#[test] +fn generated_out_event_derive_debug() { + #[allow(dead_code)] + #[derive(NetworkBehaviour)] + struct Foo { + ping: libp2p::ping::Ping, + } + + fn require_debug() + where + T: NetworkBehaviour, + ::OutEvent: Debug, + { + } + + require_debug::(); +}