Make Multiaddr::iter borrow data (#478)

* Use `unsigned-varint` crate.

* Implement `Display` for `Protocol`.

Gives `ToString` for free.

* Use `Cow` in `AddrComponent`.

* Add `AddrComponent::acquire`.

* Document `AddrComponent::acquire`.
This commit is contained in:
Toralf Wittner
2018-09-14 10:21:14 +02:00
committed by GitHub
parent 5980a4538e
commit b2756c8fa9
7 changed files with 169 additions and 140 deletions

View File

@ -284,13 +284,11 @@ pub struct PeerIdTransportOutput<S> {
// If the multiaddress is in the form `/p2p/...`, turn it into a `PeerId`.
// Otherwise, return it as-is.
fn multiaddr_to_peerid(addr: Multiaddr) -> Result<PeerId, Multiaddr> {
let components = addr.iter().collect::<Vec<_>>();
if components.len() < 1 {
return Err(addr);
if addr.iter().next().is_none() {
return Err(addr)
}
match components.last() {
Some(&AddrComponent::P2P(ref peer_id)) => {
match addr.iter().last() {
Some(AddrComponent::P2P(ref peer_id)) => {
match PeerId::from_multihash(peer_id.clone()) {
Ok(peer_id) => Ok(peer_id),
Err(_) => Err(addr),