mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-22 14:21:33 +00:00
Merge pull request #82 from tomaka/rm-multiaddr-new
No longer use deprecated function Multiaddr::new
This commit is contained in:
@ -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");
|
||||
|
@ -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::<Multiaddr>().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::<Vec<_>>(),
|
||||
&[Multiaddr::new("/ip4/10.11.12.13/tcp/20000").unwrap()]);
|
||||
&["/ip4/10.11.12.13/tcp/20000".parse::<Multiaddr>().unwrap()]);
|
||||
}
|
||||
```
|
||||
|
@ -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::<Multiaddr>().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::<Vec<_>>(),
|
||||
//! &[Multiaddr::new("/ip4/10.11.12.13/tcp/20000").unwrap()]);
|
||||
//! &["/ip4/10.11.12.13/tcp/20000".parse::<Multiaddr>().unwrap()]);
|
||||
//! }
|
||||
//! # }
|
||||
//! ```
|
||||
|
@ -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::<Multiaddr>() {
|
||||
Ok(a) => a,
|
||||
Err(err) => return Err(DeserializerError::custom(err)),
|
||||
};
|
||||
|
@ -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::<Multiaddr>().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::<Multiaddr>().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::<Multiaddr>().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::<Multiaddr>().unwrap();
|
||||
let addr2 = "/ip4/0.0.0.1/tcp/0".parse::<Multiaddr>().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::<Multiaddr>().unwrap();
|
||||
let addr2 = "/ip4/0.0.0.1/tcp/0".parse::<Multiaddr>().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));
|
||||
|
@ -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::<libp2p_swarm::Multiaddr>().unwrap()).unwrap_or_else(|_| panic!())
|
||||
.and_then(|(mut pinger, service)| {
|
||||
pinger.ping().map_err(|_| panic!()).select(service).map_err(|_| panic!())
|
||||
});
|
||||
|
@ -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::<libp2p_swarm::Multiaddr>().unwrap()).unwrap_or_else(|_| panic!())
|
||||
//! .and_then(|(mut pinger, service)| {
|
||||
//! pinger.ping().map_err(|_| panic!()).select(service).map_err(|_| panic!())
|
||||
//! });
|
||||
|
@ -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::<Multiaddr>().unwrap())
|
||||
.unwrap_or_else(|_| panic!("Unable to dial node"))
|
||||
.and_then(|connection| {
|
||||
// Sends "hello world" on the connection, will be encrypted.
|
||||
|
@ -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::<Multiaddr>().unwrap())
|
||||
//! .unwrap_or_else(|_| panic!("Unable to dial node"))
|
||||
//! .and_then(|connection| {
|
||||
//! // Sends "hello world" on the connection, will be encrypted.
|
||||
|
@ -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::<libp2p_swarm::Multiaddr>().unwrap()).unwrap_or_else(|_| panic!())
|
||||
.and_then(|(mut pinger, service)| {
|
||||
pinger.ping().map_err(|_| panic!()).select(service).map_err(|_| panic!())
|
||||
});
|
||||
|
@ -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::<libp2p_swarm::Multiaddr>().unwrap()).unwrap_or_else(|_| panic!())
|
||||
//! .and_then(|(mut pinger, service)| {
|
||||
//! pinger.ping().map_err(|_| panic!()).select(service).map_err(|_| panic!())
|
||||
//! });
|
||||
|
@ -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::<Multiaddr>().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::<Multiaddr>().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::<Multiaddr>().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::<Multiaddr>().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::<Multiaddr>().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::<Multiaddr>().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::<Multiaddr>().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::<Multiaddr>().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::<Multiaddr>().unwrap();
|
||||
assert!(tcp.listen_on(addr).is_err());
|
||||
}
|
||||
}
|
||||
|
@ -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::<Multiaddr>().unwrap();
|
||||
// or directly from a string
|
||||
let other = "/ip4/127.0.0.1".to_multiaddr().unwrap();
|
||||
|
||||
|
@ -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<Multiaddr> {
|
||||
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<u8> {
|
||||
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<T: ToMultiaddr>(&self, input: T) -> Result<Multiaddr> {
|
||||
@ -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<Self> {
|
||||
Multiaddr::new(s)
|
||||
fn from_str(input: &str) -> Result<Self> {
|
||||
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> {
|
||||
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<Multiaddr> {
|
||||
// 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> {
|
||||
Multiaddr::new(&format!("/ip4/{}", &self))
|
||||
format!("/ip4/{}", &self).parse()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToMultiaddr for Ipv6Addr {
|
||||
fn to_multiaddr(&self) -> Result<Multiaddr> {
|
||||
Multiaddr::new(&format!("/ip6/{}", &self))
|
||||
format!("/ip6/{}", &self).parse()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToMultiaddr for String {
|
||||
fn to_multiaddr(&self) -> Result<Multiaddr> {
|
||||
Multiaddr::new(self)
|
||||
self.parse()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ToMultiaddr for &'a str {
|
||||
fn to_multiaddr(&self) -> Result<Multiaddr> {
|
||||
Multiaddr::new(self)
|
||||
self.parse()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -17,20 +17,20 @@ fn protocol_to_name() {
|
||||
|
||||
|
||||
fn assert_bytes(source: &str, target: &str, protocols: Vec<ProtocolId>) -> () {
|
||||
let address = Multiaddr::new(source).unwrap();
|
||||
let address = source.parse::<Multiaddr>().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::<Vec<_>>(), protocols);
|
||||
}
|
||||
fn ma_valid(source: &str, target: &str, protocols: Vec<ProtocolId>) -> () {
|
||||
assert_bytes(source, target, protocols);
|
||||
assert_eq!(Multiaddr::new(source).unwrap().to_string(), source);
|
||||
assert_eq!(source.parse::<Multiaddr>().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::<Multiaddr>().unwrap();
|
||||
let m2 = "/ip4/127.0.0.1/tcp/1234".parse::<Multiaddr>().unwrap();
|
||||
let m3 = "/ip4/127.0.0.1/tcp/1234".parse::<Multiaddr>().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::<Multiaddr>().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::<Multiaddr>().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::<Multiaddr>().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::<Multiaddr>().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::<Multiaddr>().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::<Multiaddr>().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::<Multiaddr>().unwrap());
|
||||
}
|
||||
|
Reference in New Issue
Block a user