From acbe1d0386ea13e6c4c08ddce5208905d12bda2d Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Thu, 28 Dec 2017 18:07:49 +0100 Subject: [PATCH] No longer use deprecated function Multiaddr::new --- example/examples/echo-server.rs | 2 +- libp2p-peerstore/README.md | 4 +- libp2p-peerstore/src/lib.rs | 4 +- libp2p-peerstore/src/peer_info.rs | 2 +- libp2p-peerstore/src/peerstore_tests.rs | 14 ++-- libp2p-ping/README.md | 2 +- libp2p-ping/src/lib.rs | 2 +- libp2p-secio/README.md | 2 +- libp2p-secio/src/lib.rs | 2 +- libp2p-swarm/README.md | 2 +- libp2p-swarm/src/lib.rs | 2 +- libp2p-tcp-transport/src/lib.rs | 23 +++--- rust-multiaddr/README.md | 2 +- rust-multiaddr/src/lib.rs | 96 ++++++++++--------------- rust-multiaddr/tests/lib.rs | 26 +++---- 15 files changed, 81 insertions(+), 104 deletions(-) diff --git a/example/examples/echo-server.rs b/example/examples/echo-server.rs index 99e7aa62..d185c364 100644 --- a/example/examples/echo-server.rs +++ b/example/examples/echo-server.rs @@ -117,7 +117,7 @@ fn main() { // We now use the controller to listen on the address. let address = swarm_controller - .listen_on(swarm::Multiaddr::new(&listen_addr).expect("invalid multiaddr")) + .listen_on(listen_addr.parse().expect("invalid multiaddr")) // If the multiaddr protocol exists but is not supported, then we get an error containing // the original multiaddress. .expect("unsupported multiaddr"); diff --git a/libp2p-peerstore/README.md b/libp2p-peerstore/README.md index cbde2e99..f5d3c9cb 100644 --- a/libp2p-peerstore/README.md +++ b/libp2p-peerstore/README.md @@ -34,7 +34,7 @@ let peer_id = vec![1, 2, 3, 4]; // `peer_or_create` mutably borrows the peerstore, so we have to do it in a local scope. let mut peer = peerstore.peer_or_create(&peer_id); peer.set_pub_key(vec![60, 90, 120, 150]); - peer.add_addr(Multiaddr::new("/ip4/10.11.12.13/tcp/20000").unwrap(), + peer.add_addr("/ip4/10.11.12.13/tcp/20000".parse::().unwrap(), Duration::from_millis(5000)); } @@ -43,6 +43,6 @@ let peer_id = vec![1, 2, 3, 4]; let mut peer = peerstore.peer(&peer_id).expect("peer doesn't exist in the peerstore"); assert_eq!(peer.get_pub_key().unwrap(), &[60, 90, 120, 150]); assert_eq!(peer.addrs().collect::>(), - &[Multiaddr::new("/ip4/10.11.12.13/tcp/20000").unwrap()]); + &["/ip4/10.11.12.13/tcp/20000".parse::().unwrap()]); } ``` diff --git a/libp2p-peerstore/src/lib.rs b/libp2p-peerstore/src/lib.rs index 6a329eaa..ea7598c0 100644 --- a/libp2p-peerstore/src/lib.rs +++ b/libp2p-peerstore/src/lib.rs @@ -55,7 +55,7 @@ //! // `peer_or_create` mutably borrows the peerstore, so we have to do it in a local scope. //! let mut peer = peerstore.peer_or_create(&peer_id); //! peer.set_pub_key(vec![60, 90, 120, 150]); -//! peer.add_addr(Multiaddr::new("/ip4/10.11.12.13/tcp/20000").unwrap(), +//! peer.add_addr("/ip4/10.11.12.13/tcp/20000".parse::().unwrap(), //! Duration::from_millis(5000)); //! } //! @@ -64,7 +64,7 @@ //! let mut peer = peerstore.peer(&peer_id).expect("peer doesn't exist in the peerstore"); //! assert_eq!(peer.get_pub_key().unwrap(), &[60, 90, 120, 150]); //! assert_eq!(peer.addrs().collect::>(), -//! &[Multiaddr::new("/ip4/10.11.12.13/tcp/20000").unwrap()]); +//! &["/ip4/10.11.12.13/tcp/20000".parse::().unwrap()]); //! } //! # } //! ``` diff --git a/libp2p-peerstore/src/peer_info.rs b/libp2p-peerstore/src/peer_info.rs index ff23d3af..924952d7 100644 --- a/libp2p-peerstore/src/peer_info.rs +++ b/libp2p-peerstore/src/peer_info.rs @@ -162,7 +162,7 @@ impl<'de> Deserialize<'de> for PeerInfo { let addrs = { let mut out = Vec::with_capacity(interm.addrs.len()); for (addr, since_epoch) in interm.addrs { - let addr = match Multiaddr::new(&addr) { + let addr = match addr.parse::() { Ok(a) => a, Err(err) => return Err(DeserializerError::custom(err)), }; diff --git a/libp2p-peerstore/src/peerstore_tests.rs b/libp2p-peerstore/src/peerstore_tests.rs index 5c54e41f..08dc30a9 100644 --- a/libp2p-peerstore/src/peerstore_tests.rs +++ b/libp2p-peerstore/src/peerstore_tests.rs @@ -62,7 +62,7 @@ macro_rules! peerstore_tests { $($stmt;)* let peer_store = $create_peerstore; let peer_id = multihash::encode(multihash::Hash::SHA2512, &[1, 2, 3]).unwrap(); - let addr = Multiaddr::new("/ip4/0.0.0.0/tcp/0").unwrap(); + let addr = "/ip4/0.0.0.0/tcp/0".parse::().unwrap(); peer_store.peer_or_create(&peer_id).add_addr(addr.clone(), Duration::from_millis(5000)); @@ -76,7 +76,7 @@ macro_rules! peerstore_tests { $($stmt;)* let peer_store = $create_peerstore; let peer_id = multihash::encode(multihash::Hash::SHA2512, &[1, 2, 3]).unwrap(); - let addr = Multiaddr::new("/ip4/0.0.0.0/tcp/0").unwrap(); + let addr = "/ip4/0.0.0.0/tcp/0".parse::().unwrap(); peer_store.peer_or_create(&peer_id).add_addr(addr.clone(), Duration::from_millis(0)); thread::sleep(Duration::from_millis(2)); @@ -90,7 +90,7 @@ macro_rules! peerstore_tests { $($stmt;)* let peer_store = $create_peerstore; let peer_id = multihash::encode(multihash::Hash::SHA2512, &[1, 2, 3]).unwrap(); - let addr = Multiaddr::new("/ip4/0.0.0.0/tcp/0").unwrap(); + let addr = "/ip4/0.0.0.0/tcp/0".parse::().unwrap(); peer_store.peer_or_create(&peer_id).add_addr(addr.clone(), Duration::from_millis(5000)); peer_store.peer(&peer_id).unwrap().clear_addrs(); @@ -105,8 +105,8 @@ macro_rules! peerstore_tests { let peer_store = $create_peerstore; let peer_id = multihash::encode(multihash::Hash::SHA2512, &[1, 2, 3]).unwrap(); - let addr1 = Multiaddr::new("/ip4/0.0.0.0/tcp/0").unwrap(); - let addr2 = Multiaddr::new("/ip4/0.0.0.1/tcp/0").unwrap(); + let addr1 = "/ip4/0.0.0.0/tcp/0".parse::().unwrap(); + let addr2 = "/ip4/0.0.0.1/tcp/0".parse::().unwrap(); peer_store.peer_or_create(&peer_id).add_addr(addr1.clone(), Duration::from_millis(5000)); peer_store.peer_or_create(&peer_id).add_addr(addr2.clone(), Duration::from_millis(5000)); @@ -124,8 +124,8 @@ macro_rules! peerstore_tests { let peer_store = $create_peerstore; let peer_id = multihash::encode(multihash::Hash::SHA2512, &[1, 2, 3]).unwrap(); - let addr1 = Multiaddr::new("/ip4/0.0.0.0/tcp/0").unwrap(); - let addr2 = Multiaddr::new("/ip4/0.0.0.1/tcp/0").unwrap(); + let addr1 = "/ip4/0.0.0.0/tcp/0".parse::().unwrap(); + let addr2 = "/ip4/0.0.0.1/tcp/0".parse::().unwrap(); peer_store.peer_or_create(&peer_id).add_addr(addr1.clone(), Duration::from_millis(5000)); peer_store.peer_or_create(&peer_id).add_addr(addr2.clone(), Duration::from_millis(5000)); diff --git a/libp2p-ping/README.md b/libp2p-ping/README.md index 620235c7..a88f5987 100644 --- a/libp2p-ping/README.md +++ b/libp2p-ping/README.md @@ -43,7 +43,7 @@ let mut core = tokio_core::reactor::Core::new().unwrap(); let ping_finished_future = libp2p_tcp_transport::TcpConfig::new(core.handle()) .with_upgrade(Ping) - .dial(libp2p_swarm::Multiaddr::new("127.0.0.1:12345").unwrap()).unwrap_or_else(|_| panic!()) + .dial("127.0.0.1:12345".parse::().unwrap()).unwrap_or_else(|_| panic!()) .and_then(|(mut pinger, service)| { pinger.ping().map_err(|_| panic!()).select(service).map_err(|_| panic!()) }); diff --git a/libp2p-ping/src/lib.rs b/libp2p-ping/src/lib.rs index 98c30f29..21c64ea7 100644 --- a/libp2p-ping/src/lib.rs +++ b/libp2p-ping/src/lib.rs @@ -67,7 +67,7 @@ //! //! let ping_finished_future = libp2p_tcp_transport::TcpConfig::new(core.handle()) //! .with_upgrade(Ping) -//! .dial(libp2p_swarm::Multiaddr::new("127.0.0.1:12345").unwrap()).unwrap_or_else(|_| panic!()) +//! .dial("127.0.0.1:12345".parse::().unwrap()).unwrap_or_else(|_| panic!()) //! .and_then(|(mut pinger, service)| { //! pinger.ping().map_err(|_| panic!()).select(service).map_err(|_| panic!()) //! }); diff --git a/libp2p-secio/README.md b/libp2p-secio/README.md index a39e8752..37b07204 100644 --- a/libp2p-secio/README.md +++ b/libp2p-secio/README.md @@ -37,7 +37,7 @@ let transport = TcpConfig::new(core.handle()) } }); -let future = transport.dial(Multiaddr::new("/ip4/127.0.0.1/tcp/12345").unwrap()) +let future = transport.dial("/ip4/127.0.0.1/tcp/12345".parse::().unwrap()) .unwrap_or_else(|_| panic!("Unable to dial node")) .and_then(|connection| { // Sends "hello world" on the connection, will be encrypted. diff --git a/libp2p-secio/src/lib.rs b/libp2p-secio/src/lib.rs index bcece16f..95c0cbcc 100644 --- a/libp2p-secio/src/lib.rs +++ b/libp2p-secio/src/lib.rs @@ -58,7 +58,7 @@ //! } //! }); //! -//! let future = transport.dial(Multiaddr::new("/ip4/127.0.0.1/tcp/12345").unwrap()) +//! let future = transport.dial("/ip4/127.0.0.1/tcp/12345".parse::().unwrap()) //! .unwrap_or_else(|_| panic!("Unable to dial node")) //! .and_then(|connection| { //! // Sends "hello world" on the connection, will be encrypted. diff --git a/libp2p-swarm/README.md b/libp2p-swarm/README.md index cd13dde4..62ebae13 100644 --- a/libp2p-swarm/README.md +++ b/libp2p-swarm/README.md @@ -124,7 +124,7 @@ let ping_finished_future = libp2p_tcp_transport::TcpConfig::new(core.handle()) .with_upgrade(Ping) // TODO: right now the only available protocol is ping, but we want to replace it with // something that is more simple to use - .dial(libp2p_swarm::Multiaddr::new("127.0.0.1:12345").unwrap()).unwrap_or_else(|_| panic!()) + .dial("127.0.0.1:12345".parse::().unwrap()).unwrap_or_else(|_| panic!()) .and_then(|(mut pinger, service)| { pinger.ping().map_err(|_| panic!()).select(service).map_err(|_| panic!()) }); diff --git a/libp2p-swarm/src/lib.rs b/libp2p-swarm/src/lib.rs index fd5041f5..2171f665 100644 --- a/libp2p-swarm/src/lib.rs +++ b/libp2p-swarm/src/lib.rs @@ -148,7 +148,7 @@ //! .with_upgrade(Ping) //! // TODO: right now the only available protocol is ping, but we want to replace it with //! // something that is more simple to use -//! .dial(libp2p_swarm::Multiaddr::new("127.0.0.1:12345").unwrap()).unwrap_or_else(|_| panic!()) +//! .dial("127.0.0.1:12345".parse::().unwrap()).unwrap_or_else(|_| panic!()) //! .and_then(|(mut pinger, service)| { //! pinger.ping().map_err(|_| panic!()).select(service).map_err(|_| panic!()) //! }); diff --git a/libp2p-tcp-transport/src/lib.rs b/libp2p-tcp-transport/src/lib.rs index cc311769..f7e9ed85 100644 --- a/libp2p-tcp-transport/src/lib.rs +++ b/libp2p-tcp-transport/src/lib.rs @@ -168,33 +168,32 @@ mod tests { fn multiaddr_to_tcp_conversion() { use std::net::Ipv6Addr; - assert!(multiaddr_to_socketaddr(&Multiaddr::new("/ip4/127.0.0.1/udp/1234").unwrap()).is_err()); + assert!(multiaddr_to_socketaddr(&"/ip4/127.0.0.1/udp/1234".parse::().unwrap()).is_err()); assert_eq!( - multiaddr_to_socketaddr(&Multiaddr::new("/ip4/127.0.0.1/tcp/12345").unwrap()), + multiaddr_to_socketaddr(&"/ip4/127.0.0.1/tcp/12345".parse::().unwrap()), Ok(SocketAddr::new( IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 12345, )) ); assert_eq!( - multiaddr_to_socketaddr(&Multiaddr::new("/ip4/255.255.255.255/tcp/8080").unwrap()), + multiaddr_to_socketaddr(&"/ip4/255.255.255.255/tcp/8080".parse::().unwrap()), Ok(SocketAddr::new( IpAddr::V4(Ipv4Addr::new(255, 255, 255, 255)), 8080, )) ); assert_eq!( - multiaddr_to_socketaddr(&Multiaddr::new("/ip6/::1/tcp/12345").unwrap()), + multiaddr_to_socketaddr(&"/ip6/::1/tcp/12345".parse::().unwrap()), Ok(SocketAddr::new( IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 12345, )) ); assert_eq!( - multiaddr_to_socketaddr(&Multiaddr::new( - "/ip6/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/tcp/8080", - ).unwrap()), + multiaddr_to_socketaddr(&"/ip6/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/tcp/8080" + .parse::().unwrap()), Ok(SocketAddr::new( IpAddr::V6(Ipv6Addr::new( 65535, @@ -217,7 +216,7 @@ mod tests { std::thread::spawn(move || { let mut core = Core::new().unwrap(); - let addr = Multiaddr::new("/ip4/127.0.0.1/tcp/12345").unwrap(); + let addr = "/ip4/127.0.0.1/tcp/12345".parse::().unwrap(); let tcp = TcpConfig::new(core.handle()); let handle = core.handle(); let listener = tcp.listen_on(addr).unwrap().0.for_each(|(sock, _)| { @@ -238,7 +237,7 @@ mod tests { core.run(listener).unwrap(); }); std::thread::sleep(std::time::Duration::from_millis(100)); - let addr = Multiaddr::new("/ip4/127.0.0.1/tcp/12345").unwrap(); + let addr = "/ip4/127.0.0.1/tcp/12345".parse::().unwrap(); let mut core = Core::new().unwrap(); let tcp = TcpConfig::new(core.handle()); // Obtain a future socket through dialing @@ -261,7 +260,7 @@ mod tests { let core = Core::new().unwrap(); let tcp = TcpConfig::new(core.handle()); - let addr = Multiaddr::new("/ip4/127.0.0.1/tcp/0").unwrap(); + let addr = "/ip4/127.0.0.1/tcp/0".parse::().unwrap(); assert!(addr.to_string().contains("tcp/0")); let (_, new_addr) = tcp.listen_on(addr).unwrap(); @@ -273,7 +272,7 @@ mod tests { let core = Core::new().unwrap(); let tcp = TcpConfig::new(core.handle()); - let addr = Multiaddr::new("/ip6/::1/tcp/0").unwrap(); + let addr: Multiaddr = "/ip6/::1/tcp/0".parse().unwrap(); assert!(addr.to_string().contains("tcp/0")); let (_, new_addr) = tcp.listen_on(addr).unwrap(); @@ -285,7 +284,7 @@ mod tests { let core = Core::new().unwrap(); let tcp = TcpConfig::new(core.handle()); - let addr = Multiaddr::new("/ip4/127.0.0.1/tcp/12345/tcp/12345").unwrap(); + let addr = "/ip4/127.0.0.1/tcp/12345/tcp/12345".parse::().unwrap(); assert!(tcp.listen_on(addr).is_err()); } } diff --git a/rust-multiaddr/README.md b/rust-multiaddr/README.md index d611edc7..56e5a80f 100644 --- a/rust-multiaddr/README.md +++ b/rust-multiaddr/README.md @@ -38,7 +38,7 @@ extern crate multiaddr; use multiaddr::{Multiaddr, ToMultiaddr}; -let address = Multiaddr::new("/ip4/127.0.0.1/udp/1234").unwrap(); +let address = "/ip4/127.0.0.1/udp/1234".parse::().unwrap(); // or directly from a string let other = "/ip4/127.0.0.1".to_multiaddr().unwrap(); diff --git a/rust-multiaddr/src/lib.rs b/rust-multiaddr/src/lib.rs index 617556ee..f80adb2d 100644 --- a/rust-multiaddr/src/lib.rs +++ b/rust-multiaddr/src/lib.rs @@ -38,7 +38,7 @@ impl fmt::Display for Multiaddr { /// ``` /// use multiaddr::Multiaddr; /// - /// let address = Multiaddr::new("/ip4/127.0.0.1/udt").unwrap(); + /// let address: Multiaddr = "/ip4/127.0.0.1/udt".parse().unwrap(); /// assert_eq!(address.to_string(), "/ip4/127.0.0.1/udt"); /// ``` /// @@ -52,51 +52,6 @@ impl fmt::Display for Multiaddr { } impl Multiaddr { - /// Create a new multiaddr based on a string representation, like - /// `/ip4/127.0.0.1/udp/1234`. - /// - /// # Examples - /// - /// Simple construction - /// - /// ``` - /// use multiaddr::Multiaddr; - /// - /// let address = Multiaddr::new("/ip4/127.0.0.1/udp/1234").unwrap(); - /// assert_eq!(address.to_bytes(), [ - /// 4, 127, 0, 0, 1, - /// 17, 4, 210 - /// ]); - /// ``` - /// - #[deprecated(note = "Use `string.parse()` instead")] - pub fn new(input: &str) -> Result { - let mut bytes = Vec::new(); - - let mut parts = input.split('/'); - // A multiaddr must start with `/` - if !parts.next().ok_or(Error::InvalidMultiaddr)?.is_empty() { - return Err(Error::InvalidMultiaddr); - } - - while let Some(part) = parts.next() { - let protocol: ProtocolId = part.parse()?; - let addr_component = match protocol.size() { - ProtocolArgSize::Fixed { bytes: 0 } => { - protocol.parse_data("")? // TODO: bad design - }, - _ => { - let data = parts.next().ok_or(Error::MissingAddress)?; - protocol.parse_data(data)? - }, - }; - - addr_component.write_bytes(&mut bytes).expect("writing to a Vec never fails"); - } - - Ok(Multiaddr { bytes: bytes }) - } - /// Return a copy to disallow changing the bytes directly pub fn to_bytes(&self) -> Vec { self.bytes.to_owned() @@ -116,7 +71,7 @@ impl Multiaddr { /// ``` /// use multiaddr::{Multiaddr, ProtocolId}; /// - /// let address = Multiaddr::new("/ip4/127.0.0.1").unwrap(); + /// let address: Multiaddr = "/ip4/127.0.0.1".parse().unwrap(); /// assert_eq!(address.protocol(), vec![ProtocolId::IP4]); /// ``` /// @@ -133,9 +88,9 @@ impl Multiaddr { /// ``` /// use multiaddr::Multiaddr; /// - /// let address = Multiaddr::new("/ip4/127.0.0.1").unwrap(); + /// let address: Multiaddr = "/ip4/127.0.0.1".parse().unwrap(); /// let nested = address.encapsulate("/udt").unwrap(); - /// assert_eq!(nested, Multiaddr::new("/ip4/127.0.0.1/udt").unwrap()); + /// assert_eq!(nested, "/ip4/127.0.0.1/udt".parse().unwrap()); /// ``` /// pub fn encapsulate(&self, input: T) -> Result { @@ -154,9 +109,9 @@ impl Multiaddr { /// ``` /// use multiaddr::{Multiaddr, ToMultiaddr}; /// - /// let address = Multiaddr::new("/ip4/127.0.0.1/udt/sctp/5678").unwrap(); + /// let address: Multiaddr = "/ip4/127.0.0.1/udt/sctp/5678".parse().unwrap(); /// let unwrapped = address.decapsulate("/udt").unwrap(); - /// assert_eq!(unwrapped, Multiaddr::new("/ip4/127.0.0.1").unwrap()); + /// assert_eq!(unwrapped, "/ip4/127.0.0.1".parse().unwrap()); /// /// assert_eq!( /// address.decapsulate("/udt").unwrap(), @@ -281,8 +236,31 @@ impl FromStr for Multiaddr { type Err = Error; #[inline] - fn from_str(s: &str) -> Result { - Multiaddr::new(s) + fn from_str(input: &str) -> Result { + let mut bytes = Vec::new(); + + let mut parts = input.split('/'); + // A multiaddr must start with `/` + if !parts.next().ok_or(Error::InvalidMultiaddr)?.is_empty() { + return Err(Error::InvalidMultiaddr); + } + + while let Some(part) = parts.next() { + let protocol: ProtocolId = part.parse()?; + let addr_component = match protocol.size() { + ProtocolArgSize::Fixed { bytes: 0 } => { + protocol.parse_data("")? // TODO: bad design + }, + _ => { + let data = parts.next().ok_or(Error::MissingAddress)?; + protocol.parse_data(data)? + }, + }; + + addr_component.write_bytes(&mut bytes).expect("writing to a Vec never fails"); + } + + Ok(Multiaddr { bytes: bytes }) } } @@ -337,14 +315,14 @@ impl ToMultiaddr for SocketAddr { impl ToMultiaddr for SocketAddrV4 { fn to_multiaddr(&self) -> Result { - Multiaddr::new(&format!("/ip4/{}/tcp/{}", self.ip(), self.port())) + format!("/ip4/{}/tcp/{}", self.ip(), self.port()).parse() } } impl ToMultiaddr for SocketAddrV6 { fn to_multiaddr(&self) -> Result { // TODO: Should how should we handle `flowinfo` and `scope_id`? - Multiaddr::new(&format!("/ip6/{}/tcp/{}", self.ip(), self.port())) + format!("/ip6/{}/tcp/{}", self.ip(), self.port()).parse() } } @@ -359,25 +337,25 @@ impl ToMultiaddr for IpAddr { impl ToMultiaddr for Ipv4Addr { fn to_multiaddr(&self) -> Result { - Multiaddr::new(&format!("/ip4/{}", &self)) + format!("/ip4/{}", &self).parse() } } impl ToMultiaddr for Ipv6Addr { fn to_multiaddr(&self) -> Result { - Multiaddr::new(&format!("/ip6/{}", &self)) + format!("/ip6/{}", &self).parse() } } impl ToMultiaddr for String { fn to_multiaddr(&self) -> Result { - Multiaddr::new(self) + self.parse() } } impl<'a> ToMultiaddr for &'a str { fn to_multiaddr(&self) -> Result { - Multiaddr::new(self) + self.parse() } } diff --git a/rust-multiaddr/tests/lib.rs b/rust-multiaddr/tests/lib.rs index 6525e23f..fc4604d1 100644 --- a/rust-multiaddr/tests/lib.rs +++ b/rust-multiaddr/tests/lib.rs @@ -17,20 +17,20 @@ fn protocol_to_name() { fn assert_bytes(source: &str, target: &str, protocols: Vec) -> () { - let address = Multiaddr::new(source).unwrap(); + let address = source.parse::().unwrap(); assert_eq!(hex::encode(address.to_bytes().as_slice()), target); - assert_eq!(address.protocol(), protocols); + assert_eq!(address.iter().map(|addr| addr.protocol_id()).collect::>(), protocols); } fn ma_valid(source: &str, target: &str, protocols: Vec) -> () { assert_bytes(source, target, protocols); - assert_eq!(Multiaddr::new(source).unwrap().to_string(), source); + assert_eq!(source.parse::().unwrap().to_string(), source); } #[test] fn multiaddr_eq() { - let m1 = Multiaddr::new("/ip4/127.0.0.1/udp/1234").unwrap(); - let m2 = Multiaddr::new("/ip4/127.0.0.1/tcp/1234").unwrap(); - let m3 = Multiaddr::new("/ip4/127.0.0.1/tcp/1234").unwrap(); + let m1 = "/ip4/127.0.0.1/udp/1234".parse::().unwrap(); + let m2 = "/ip4/127.0.0.1/tcp/1234".parse::().unwrap(); + let m3 = "/ip4/127.0.0.1/tcp/1234".parse::().unwrap(); assert_ne!(m1, m2); assert_ne!(m2, m1); @@ -135,7 +135,7 @@ fn construct_fail() { "/p2p-circuit/50"]; for address in &addresses { - assert!(Multiaddr::new(address).is_err(), address.to_string()); + assert!(address.parse::().is_err(), address.to_string()); } } @@ -143,17 +143,17 @@ fn construct_fail() { #[test] fn to_multiaddr() { assert_eq!(Ipv4Addr::new(127, 0, 0, 1).to_multiaddr().unwrap(), - Multiaddr::new("/ip4/127.0.0.1").unwrap()); + "/ip4/127.0.0.1".parse::().unwrap()); assert_eq!(Ipv6Addr::new(0x2601, 0x9, 0x4f81, 0x9700, 0x803e, 0xca65, 0x66e8, 0xc21) .to_multiaddr() .unwrap(), - Multiaddr::new("/ip6/2601:9:4f81:9700:803e:ca65:66e8:c21").unwrap()); + "/ip6/2601:9:4f81:9700:803e:ca65:66e8:c21".parse::().unwrap()); assert_eq!("/ip4/127.0.0.1/tcp/1234".to_string().to_multiaddr().unwrap(), - Multiaddr::new("/ip4/127.0.0.1/tcp/1234").unwrap()); + "/ip4/127.0.0.1/tcp/1234".parse::().unwrap()); assert_eq!("/ip6/2601:9:4f81:9700:803e:ca65:66e8:c21".to_multiaddr().unwrap(), - Multiaddr::new("/ip6/2601:9:4f81:9700:803e:ca65:66e8:c21").unwrap()); + "/ip6/2601:9:4f81:9700:803e:ca65:66e8:c21".parse::().unwrap()); assert_eq!(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 1234).to_multiaddr().unwrap(), - Multiaddr::new("/ip4/127.0.0.1/tcp/1234").unwrap()); + "/ip4/127.0.0.1/tcp/1234".parse::().unwrap()); assert_eq!(SocketAddrV6::new(Ipv6Addr::new(0x2601, 0x9, 0x4f81, @@ -167,5 +167,5 @@ fn to_multiaddr() { 0) .to_multiaddr() .unwrap(), - Multiaddr::new("/ip6/2601:9:4f81:9700:803e:ca65:66e8:c21/tcp/1234").unwrap()); + "/ip6/2601:9:4f81:9700:803e:ca65:66e8:c21/tcp/1234".parse::().unwrap()); }