From 9ba6a6e8d566c7813cfa651c237c1f619df1c393 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Tue, 16 Aug 2022 15:10:37 +0900 Subject: [PATCH 1/2] swarm-derive/: Derive Debug for generated OutEvent 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 I think it is fine to require all sub-`OutEvent`s to implement `Debug`, I don't think the same applies 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 | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/swarm-derive/src/lib.rs b/swarm-derive/src/lib.rs index 8a42220246a..6ce4a8187a5 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 { #(#fields),* } diff --git a/swarm-derive/tests/test.rs b/swarm-derive/tests/test.rs index 09048e5e801..0eb86efe8fb 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)] @@ -476,3 +477,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::(); +} From 06fabf014acd2990d2c1e12f757e59be00c5865e Mon Sep 17 00:00:00 2001 From: Max Inden Date: Wed, 17 Aug 2022 15:07:42 +0900 Subject: [PATCH 2/2] swarm-derive/tests: Require Debug for OutEvent --- swarm-derive/tests/test.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/swarm-derive/tests/test.rs b/swarm-derive/tests/test.rs index 2cc462a0bcf..f4a313198a9 100644 --- a/swarm-derive/tests/test.rs +++ b/swarm-derive/tests/test.rs @@ -276,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, } @@ -289,6 +292,7 @@ fn where_clause() { struct Foo where T: Copy + NetworkBehaviour, + ::OutEvent: Debug, { ping: libp2p::ping::Ping, bar: T,