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`.
This commit is contained in:
Max Inden
2022-08-17 08:40:32 +02:00
committed by GitHub
parent d2c50530e9
commit a2738fd555
2 changed files with 25 additions and 1 deletions

View File

@ -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
{

View File

@ -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<T: Copy + NetworkBehaviour> {
struct Foo<T: Copy + NetworkBehaviour>
where
<T as NetworkBehaviour>::OutEvent: Debug,
{
ping: libp2p::ping::Ping,
bar: T,
}
@ -288,6 +292,7 @@ fn where_clause() {
struct Foo<T>
where
T: Copy + NetworkBehaviour,
<T as 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<T>()
where
T: NetworkBehaviour,
<T as NetworkBehaviour>::OutEvent: Debug,
{
}
require_debug::<Foo>();
}