Skip to content

Commit

Permalink
add: starts_with in MultiAddr
Browse files Browse the repository at this point in the history
  • Loading branch information
kamuik16 committed Nov 13, 2024
1 parent a4a4fbd commit 33c56a4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
22 changes: 14 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.18.3

- Add `starts_with` on `Multiaddr`. See [PR ].

[PR ]:

# 0.18.2

- Implement missing protocols. See [PR 110].
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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/
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]`
Expand Down
12 changes: 12 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Multiaddr>();
assert!(m.starts_with(&prefix));
}
}
QuickCheck::new().quickcheck(prop as fn(_))
}

// Arbitrary impls

#[derive(PartialEq, Eq, Clone, Hash, Debug)]
Expand Down

0 comments on commit 33c56a4

Please sign in to comment.