Skip to content

Commit

Permalink
swarm-derive/: Derive Debug for generated OutEvent (#2821)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
mxinden committed Aug 17, 2022
1 parent d2c5053 commit a2738fd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions swarm-derive/src/lib.rs
Expand Up @@ -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
{
Expand Down
25 changes: 24 additions & 1 deletion swarm-derive/tests/test.rs
Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
}
Expand All @@ -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,
Expand Down Expand Up @@ -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>();
}

0 comments on commit a2738fd

Please sign in to comment.