mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-07-02 19:21:37 +00:00
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:
10
src/lib.rs
10
src/lib.rs
@ -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))
|
||||
};
|
||||
)+
|
||||
|
Reference in New Issue
Block a user