Skip to content
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

Drop custom Either types #2650

Closed
6 of 7 tasks
thomaseizinger opened this issue May 16, 2022 · 10 comments
Closed
6 of 7 tasks

Drop custom Either types #2650

thomaseizinger opened this issue May 16, 2022 · 10 comments

Comments

@thomaseizinger
Copy link
Contributor

thomaseizinger commented May 16, 2022

Description

We have a lot of custom Either types within libp2p-core, some of which (I think) could go away by implementing our traits directly on the Either type that we are already depending on.

Motivation

Less code to maintain.

Checklist

Are you planning to do it yourself in a pull request?

No / Maybe. Could be a good first issue. What do you think @mxinden?

@thomaseizinger thomaseizinger added difficulty:easy getting-started Issues that can be tackled if you don't know the internals of libp2p very well labels May 16, 2022
@mxinden
Copy link
Member

mxinden commented May 17, 2022

Sounds good to me. Also, should we be using either::Either or futures::future::Either or both?

@thomaseizinger
Copy link
Contributor Author

Sounds good to me. Also, should we be using either::Either or futures::future::Either or both?

I am not sure how well maintained either::Either is but feels like the more appropriate one, given that Future is part of std now?

I've had a brief look and for example the Error impl in either::Either is not overriding all methods so it may need a bit of upstream work until we can completely replace our types.

I think as part of this ticket, one would need to do a bit more research to see if we can drop future::Either for example. Would be interesting to know, why they have their own Either type in the first place.

@maschad
Copy link
Member

maschad commented May 19, 2022

examining it naively the future::Either could be dropped as it seems to serve as a convenience enum type, which could be replaced by what you mentioned earlier by implementing our traits directly on the Either type or implementing IntoFuture to be awaited.

@thomaseizinger
Copy link
Contributor Author

I've had a brief look and for example the Error impl in either::Either is not overriding all methods so it may need a bit of upstream work until we can completely replace our types.

This is fixed now: rayon-rs/either#69

There is also a new release coming: rayon-rs/either#73

@thomaseizinger
Copy link
Contributor Author

I've opened an issue regarding pin projection which is another blocker for some of our Either use cases: rayon-rs/either#76

@thomaseizinger thomaseizinger changed the title Refactoring: Drop custom Either types where possible Drop custom Either types where possible Jan 11, 2023
@thomaseizinger
Copy link
Contributor Author

I opened an issue with either to see if we can get AsyncRead + AsyncWrite traits. Then we could drop the EitherOutput type: rayon-rs/either#79

@thomaseizinger thomaseizinger changed the title Drop custom Either types where possible Drop custom Either types Jan 11, 2023
@thomaseizinger
Copy link
Contributor Author

I opened an issue with either to see if we can get AsyncRead + AsyncWrite traits. Then we could drop the EitherOutput type: bluss/either#79

Update: As suggested in the linked issue, we could simply use the futures::Either type for all cases where we need implementations of AsyncRead etc.

I think that is a sensible way forward. Using the same type everywhere would be better but using two already existing ones is already an improvement over defining our own types in my opinion.

@thomaseizinger
Copy link
Contributor Author

I opened an issue with either to see if we can get AsyncRead + AsyncWrite traits. Then we could drop the EitherOutput type: bluss/either#79

Update: As suggested in the linked issue, we could simply use the futures::Either type for all cases where we need implementations of AsyncRead etc.

I think that is a sensible way forward. Using the same type everywhere would be better but using two already existing ones is already an improvement over defining our own types in my opinion.

Unfortunately, this doesn't quite work.

  1. Our EitherFuture type only works on TryFutures and directly transposes the result.
  2. We use our EitherFuture type in multiple places. In some of them, the resulting Output should be an Either that implements futures traits and in some it should be the regular Either.
  3. The futures::Either type implements Future such that both futures need to evaluate to the same type which is a bit silly IMO and probably the reason we have our own type.

The bottom line is that I think we should convince either that they should provide a Future etc implementation that doesn't try to be clever but simply sets all associated types like Future::Output and Stream::Item to Either.

@thomaseizinger
Copy link
Contributor Author

The bottom line is that I think we should convince either that they should provide a Future etc implementation that doesn't try to be clever but simply sets all associated types like Future::Output and Stream::Item to Either.

That is also not possible because they already implement Future in the exact same way as futures::Either. Unfortunately, this means we are stuck with a custom EitherFuture type that behaves the way we need it. The good news is that we might only need it for SelectUpgrade if we end up doing #3347.

@thomaseizinger thomaseizinger removed help wanted getting-started Issues that can be tackled if you don't know the internals of libp2p very well difficulty:easy labels May 8, 2023
@thomaseizinger
Copy link
Contributor Author

EitherFuture is here to stay for a while so I am considering this to be completed because everything that is actionable has been done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants