From 33c56a413d2c8b71e340190f6df737280a6ebc48 Mon Sep 17 00:00:00 2001 From: kamuik16 Date: Wed, 13 Nov 2024 11:38:12 +0530 Subject: [PATCH 1/3] add: starts_with in MultiAddr --- CHANGELOG.md | 22 ++++++++++++++-------- Cargo.toml | 2 +- src/lib.rs | 10 ++++++++++ tests/lib.rs | 12 ++++++++++++ 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c867cc2..5b7e333 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.18.3 + +- Add `starts_with` on `Multiaddr`. See [PR ]. + +[PR ]: + # 0.18.2 - Implement missing protocols. See [PR 110]. @@ -42,7 +48,7 @@ - Rename string representation of `WebRTC` protocol from `/webrtc` to `/webrt-direct`. For backwards compatibility `/webrtc` will still be decoded to `Protocol::WebRTC`, but `Protocol::WebRTC` will from now on always be encoded as `/webrtc-direct`. See [multiformats/multiaddr discussion] and [PR 84] for context. - ``` rust + ```rust assert_eq!( Multiaddr::empty().with(Protocol::WebRTC), "/webrtc".parse().unwrap(), @@ -114,15 +120,15 @@ # 0.12.0 [2021-05-26] -- Merge [multiaddr] and [parity-multiaddr] (see [PR 40]). +- Merge [multiaddr] and [parity-multiaddr] (see [PR 40]). - - Functionality to go from a `u64` to a `multiadddr::Protocol` and back is - removed. Please open an issue on [multiaddr] in case this is still needed. + - Functionality to go from a `u64` to a `multiadddr::Protocol` and back is + removed. Please open an issue on [multiaddr] in case this is still needed. - - Given that `multiaddr::Protocol` now represents both the protocol - identifier as well as the protocol data (e.g. protocol identifier `55` - (`dns6`) and protocol data `some-domain.example`) `multiaddr::Protocol` is - no longer `Copy`. + - Given that `multiaddr::Protocol` now represents both the protocol + identifier as well as the protocol data (e.g. protocol identifier `55` + (`dns6`) and protocol data `some-domain.example`) `multiaddr::Protocol` is + no longer `Copy`. [multiaddr]: https://github.com/multiformats/rust-multiaddr [parity-multiaddr]: https://github.com/libp2p/rust-libp2p/blob/master/misc/multiaddr/ diff --git a/Cargo.toml b/Cargo.toml index 1c7dd96..dc6ae2d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ keywords = ["multiaddr", "ipfs"] license = "MIT" name = "multiaddr" readme = "README.md" -version = "0.18.2" +version = "0.18.3" [features] default = ["url"] diff --git a/src/lib.rs b/src/lib.rs index df596a5..9ad6c6d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -210,6 +210,16 @@ impl Multiaddr { self.bytes[(n - m)..] == other.bytes[..] } + /// Checks whether the given `Multiaddr` is a prefix of this `Multiaddr`. + pub fn starts_with(&self, other: &Multiaddr) -> bool { + let n = self.bytes.len(); + let m = other.bytes.len(); + if n < m { + return false; + } + self.bytes[..m] == other.bytes[..] + } + /// Returns &str identifiers for the protocol names themselves. /// This omits specific info like addresses, ports, peer IDs, and the like. /// Example: `"/ip4/127.0.0.1/tcp/5001"` would return `["ip4", "tcp"]` diff --git a/tests/lib.rs b/tests/lib.rs index 9248eb5..936809e 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -68,6 +68,18 @@ fn ends_with() { QuickCheck::new().quickcheck(prop as fn(_)) } +#[test] +fn starts_with() { + fn prop(Ma(m): Ma) { + let n = m.iter().count(); + for i in 0..n { + let prefix = m.iter().take(i + 1).collect::(); + assert!(m.starts_with(&prefix)); + } + } + QuickCheck::new().quickcheck(prop as fn(_)) +} + // Arbitrary impls #[derive(PartialEq, Eq, Clone, Hash, Debug)] From 1b8fa14d12ee64db80aad5675f3c276d5afd456d Mon Sep 17 00:00:00 2001 From: kamuik16 Date: Wed, 13 Nov 2024 11:46:06 +0530 Subject: [PATCH 2/3] update changelog.md --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b7e333..b0bff71 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ # 0.18.3 -- Add `starts_with` on `Multiaddr`. See [PR ]. +- Add `starts_with` on `Multiaddr`. See [PR 119]. -[PR ]: +[PR 119]: https://github.com/multiformats/rust-multiaddr/pull/119 # 0.18.2 From 890be0692850269a5dfb3821602107a5b9d15899 Mon Sep 17 00:00:00 2001 From: kamuik16 Date: Wed, 13 Nov 2024 11:53:04 +0530 Subject: [PATCH 3/3] update changelog.md --- CHANGELOG.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0bff71..d4d8f50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,7 +48,7 @@ - Rename string representation of `WebRTC` protocol from `/webrtc` to `/webrt-direct`. For backwards compatibility `/webrtc` will still be decoded to `Protocol::WebRTC`, but `Protocol::WebRTC` will from now on always be encoded as `/webrtc-direct`. See [multiformats/multiaddr discussion] and [PR 84] for context. - ```rust + ``` rust assert_eq!( Multiaddr::empty().with(Protocol::WebRTC), "/webrtc".parse().unwrap(), @@ -120,15 +120,15 @@ # 0.12.0 [2021-05-26] -- Merge [multiaddr] and [parity-multiaddr] (see [PR 40]). +- Merge [multiaddr] and [parity-multiaddr] (see [PR 40]). - - Functionality to go from a `u64` to a `multiadddr::Protocol` and back is - removed. Please open an issue on [multiaddr] in case this is still needed. + - Functionality to go from a `u64` to a `multiadddr::Protocol` and back is + removed. Please open an issue on [multiaddr] in case this is still needed. - - Given that `multiaddr::Protocol` now represents both the protocol - identifier as well as the protocol data (e.g. protocol identifier `55` - (`dns6`) and protocol data `some-domain.example`) `multiaddr::Protocol` is - no longer `Copy`. + - Given that `multiaddr::Protocol` now represents both the protocol + identifier as well as the protocol data (e.g. protocol identifier `55` + (`dns6`) and protocol data `some-domain.example`) `multiaddr::Protocol` is + no longer `Copy`. [multiaddr]: https://github.com/multiformats/rust-multiaddr [parity-multiaddr]: https://github.com/libp2p/rust-libp2p/blob/master/misc/multiaddr/