feat: replace ProtocolName with AsRef<str>#3746
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
ProtocolName to return &strProtocolName with AsRef<str>
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
|
@thomaseizinger updating https://github.com/mxinden/kademlia-exporter/ right now. Was it a deliberate choice to not implement |
The need to abstract over the creation of What is your usecase? Why can you not use the existing, much more descriptive constructor? |
|
A user can provide a stream protocol name via a configuration file. I am parsing the stream protocol name as a string and would like to call
Works as well. Just a bit more verbose. |
Perhaps
A matter of taste for sure, feel free to send a PR! I tend to avoid calling conversion functions explicitly because they are mostly noise. |
Previously, a protocol could be any sequence of bytes as long as it started with `/`. Now, we directly parse a protocol as `String` which enforces it to be valid UTF8. To notify users of this change, we delete the `ProtocolName` trait. The new requirement is that users need to provide a type that implements `AsRef<str>`. We also add a `StreamProtocol` newtype in `libp2p-swarm` which provides an easy way for users to ensure their protocol strings are compliant. The newtype enforces that protocol strings start with `/`. `StreamProtocol` also implements `AsRef<str>`, meaning users can directly use it in their upgrades. `multistream-select` by itself only changes marginally with this patch. The only thing we enforce in the type-system is that protocols must implement `AsRef<str>`. Resolves: libp2p#2831. Pull-Request: libp2p#3746.
Description
Previously, a protocol could be any sequence of bytes as long as it started with
/. Now, we directly parse a protocol asStringwhich enforces it to be valid UTF8.To notify users of this change, we delete the
ProtocolNametrait. The new requirement is that users need to provide a type that implementsAsRef<str>.We also add a
StreamProtocolnewtype inlibp2p-swarmwhich provides an easy way for users to ensure their protocol strings are compliant. The newtype enforces that protocol strings start with/.StreamProtocolalso implementsAsRef<str>, meaning users can directly use it in their upgrades.multistream-selectby itself only changes marginally with this patch. The only thing we enforce in the type-system is that protocols must implementAsRef<str>.Resolves: #2831.
Notes & open questions
This is an alternative to #3745.
I am hoping that with this change, users will already accustom to use the
StreamProtocolnewtype. Regarding #2863, I think it makes the most sense to remove all these traits in one go and directly have the handler return a list ofStreamProtocols.Dependencies
support_floodsubsetting #3837EitherIter#3841InfoIterChain#3842Change checklist