Skip to content

feat(swarm): impl FromStr for StreamProtocol#3951

Closed
mxinden wants to merge 1 commit intolibp2p:masterfrom
mxinden:stream-protocol-from-str
Closed

feat(swarm): impl FromStr for StreamProtocol#3951
mxinden wants to merge 1 commit intolibp2p:masterfrom
mxinden:stream-protocol-from-str

Conversation

@mxinden
Copy link
Member

@mxinden mxinden commented May 16, 2023

Description

Allows using StreamProtocol e.g. as a clap argument.

Notes & open questions

Follow-up to #3746 (comment).

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

Allows using `StreamProtocol` e.g. as a `clap` argument.
Comment on lines -42 to +43
inner: Either::Right(Arc::from(protocol)), // FIXME: Can we somehow reuse the allocation from the owned string?
inner: Either::Right(Arc::from(protocol)),
Copy link
Member Author

Choose a reason for hiding this comment

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

Leftover from a refactor, right @thomaseizinger?

Copy link
Contributor

Choose a reason for hiding this comment

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

No, this still applies. Arc will allocate again despite the String already having an allocation.

type Err = InvalidProtocol;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::try_from_owned(s.into())
Copy link
Contributor

Choose a reason for hiding this comment

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

This results in one more allocation than necessary because we could also construct an Arc<str> from a string slice and don't have to go via an owned string.

We would duplicate the check then but perhaps you can refactor that check out in a separate function?

Copy link
Contributor

Choose a reason for hiding this comment

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

Or perhaps we can widen the accepted type on try_from_owned to something impl? But then we'd have to rename it as well ..

@thomaseizinger
Copy link
Contributor

Friendly ping @mxinden.

@mxinden
Copy link
Member Author

mxinden commented Jun 5, 2023

Closing here. I don't have a direct need for this any longer. In case someone does, please open as a new pull request.

@mxinden mxinden closed this Jun 5, 2023
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

Successfully merging this pull request may close these issues.

2 participants