Refactor multiaddr crate. (#498)

Refactor multiaddr crate.

- Remove `AddrComponent`. Instead `Protocol` directly contains its
associated data.

- Various smaller changes around conversions to Multiaddr from other
types, e.g. socket addresses.

- Expand tests to include property tests which test encoding/decoding
identity.
This commit is contained in:
Toralf Wittner
2018-09-20 19:51:00 +02:00
committed by GitHub
parent 1969bde4fe
commit 84b089cacc
18 changed files with 669 additions and 748 deletions

View File

@ -254,23 +254,23 @@ impl Transport for CommonTransport {
/// # #[macro_use]
/// # extern crate libp2p;
/// # fn main() {
/// let _addr = multiaddr![IP4([127, 0, 0, 1]), TCP(10500u16)];
/// let _addr = multiaddr![Ip4([127, 0, 0, 1]), Tcp(10500u16)];
/// # }
/// ```
///
/// Each element passed to `multiaddr![]` should be a variant of the `AddrComponent` enum. The
/// Each element passed to `multiaddr![]` should be a variant of the `Protocol` enum. The
/// optional parameter is casted into the proper type with the `Into` trait.
///
/// For example, `IP4([127, 0, 0, 1])` works because `Ipv4Addr` implements `From<[u8; 4]>`.
/// For example, `Ip4([127, 0, 0, 1])` works because `Ipv4Addr` implements `From<[u8; 4]>`.
#[macro_export]
macro_rules! multiaddr {
($($comp:ident $(($param:expr))*),+) => {
{
use std::iter;
let elem = iter::empty::<$crate::multiaddr::AddrComponent>();
let elem = iter::empty::<$crate::multiaddr::Protocol>();
$(
let elem = {
let cmp = $crate::multiaddr::AddrComponent::$comp $(( $param.into() ))*;
let cmp = $crate::multiaddr::Protocol::$comp $(( $param.into() ))*;
elem.chain(iter::once(cmp))
};
)+