Skip to content

refactor: expose and use THandlerOutEvent type alias #3368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 26, 2023

Conversation

thomaseizinger
Copy link
Contributor

@thomaseizinger thomaseizinger commented Jan 23, 2023

Description

Previously, we used the full reference to the OutEvent of the ConnectionHandler in all implementations of NetworkBehaviour. Not only is this very verbose, it is also more brittle to changes. With the current implementation plan for #2824, we will be removing the IntoConnectionHandler abstraction. Using a type-alias to refer to the OutEvent makes the migration much easier.

Notes

This will reduce the diff in #3254.

Links to any relevant issues

Open Questions

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • A changelog entry has been made in the appropriate crates

@thomaseizinger thomaseizinger requested review from jxs and mxinden January 23, 2023 05:48
@@ -297,7 +297,7 @@ pub enum PoolEvent<THandler: IntoConnectionHandler> {
id: ConnectionId,
peer_id: PeerId,
/// The produced event.
event: THandlerOutEvent<THandler>,
event: <<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I temporarily inlined this one so that I can delete the 2nd type alias named THandlerOutEvent defined in swarm::behaviour.

This will be simplified to ConnectionHandler::OutEvent in #3254.

@@ -122,7 +119,7 @@ pub(crate) type THandlerOutEvent<THandler> =
/// }
/// }
/// ```
pub trait NetworkBehaviour: 'static {
pub trait NetworkBehaviour: Sized + 'static {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This requires a Sized bound now but I'd honestly not know why a NetworkBehaviourwouldn't be sized ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is the requirement to be Sized related to this change?

Copy link
Contributor Author

@thomaseizinger thomaseizinger Jan 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is because I am now using Self within a function signature via a type-alias. To be frank, I just followed the compiler 🙃

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to avoid casually introducing trait bounds without careful consideration. Is there any way around introducing this trait bound? Is this pull request worth doing given the added complexity.

I would have to dig deeper here. Based on intuition, adding Sized, Send or Sync unintentionally has not proven to be good ideas in the past.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point! I never looked deeply into it but apparently, requiring Sized prohibits the use as a trait object: https://doc.rust-lang.org/std/marker/trait.Sized.html

I'll try to work around it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out it is not needed, lol 😂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point! I never looked deeply into it but apparently, requiring Sized prohibits the use as a trait object:

I'll try to work around it.

yeah, that's why you can't have Box<dyn Clone> for example

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for investigating. Great to see it not being needed.

@mergify
Copy link
Contributor

mergify bot commented Jan 24, 2023

This pull request has merge conflicts. Could you please resolve them @thomaseizinger? 🙏

Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the introduction of Sized, this looks good to me.

@@ -122,7 +119,7 @@ pub(crate) type THandlerOutEvent<THandler> =
/// }
/// }
/// ```
pub trait NetworkBehaviour: 'static {
pub trait NetworkBehaviour: Sized + 'static {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to avoid casually introducing trait bounds without careful consideration. Is there any way around introducing this trait bound? Is this pull request worth doing given the added complexity.

I would have to dig deeper here. Based on intuition, adding Sized, Send or Sync unintentionally has not proven to be good ideas in the past.

Copy link
Member

@jxs jxs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the follow-up.

@mergify mergify bot merged commit 4de54f0 into master Jan 26, 2023
@mergify mergify bot deleted the feat/more-type-aliases branch January 26, 2023 11:55
umgefahren pushed a commit to umgefahren/rust-libp2p that referenced this pull request Mar 8, 2024
Previously, we used the full reference to the `OutEvent` of the `ConnectionHandler` in all implementations of `NetworkBehaviour`. Not only is this very verbose, it is also more brittle to changes. With the current implementation plan for libp2p#2824, we will be removing the `IntoConnectionHandler` abstraction. Using a type-alias to refer to the `OutEvent` makes the migration much easier.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants