Skip to content

Commit 33c56a4

Browse files
committed
add: starts_with in MultiAddr
1 parent a4a4fbd commit 33c56a4

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

CHANGELOG.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 0.18.3
2+
3+
- Add `starts_with` on `Multiaddr`. See [PR ].
4+
5+
[PR ]:
6+
17
# 0.18.2
28

39
- Implement missing protocols. See [PR 110].
@@ -42,7 +48,7 @@
4248
- Rename string representation of `WebRTC` protocol from `/webrtc` to `/webrt-direct`.
4349
For backwards compatibility `/webrtc` will still be decoded to `Protocol::WebRTC`, but `Protocol::WebRTC` will from now on always be encoded as `/webrtc-direct`.
4450
See [multiformats/multiaddr discussion] and [PR 84] for context.
45-
``` rust
51+
```rust
4652
assert_eq!(
4753
Multiaddr::empty().with(Protocol::WebRTC),
4854
"/webrtc".parse().unwrap(),
@@ -114,15 +120,15 @@
114120

115121
# 0.12.0 [2021-05-26]
116122

117-
- Merge [multiaddr] and [parity-multiaddr] (see [PR 40]).
123+
- Merge [multiaddr] and [parity-multiaddr] (see [PR 40]).
118124

119-
- Functionality to go from a `u64` to a `multiadddr::Protocol` and back is
120-
removed. Please open an issue on [multiaddr] in case this is still needed.
125+
- Functionality to go from a `u64` to a `multiadddr::Protocol` and back is
126+
removed. Please open an issue on [multiaddr] in case this is still needed.
121127

122-
- Given that `multiaddr::Protocol` now represents both the protocol
123-
identifier as well as the protocol data (e.g. protocol identifier `55`
124-
(`dns6`) and protocol data `some-domain.example`) `multiaddr::Protocol` is
125-
no longer `Copy`.
128+
- Given that `multiaddr::Protocol` now represents both the protocol
129+
identifier as well as the protocol data (e.g. protocol identifier `55`
130+
(`dns6`) and protocol data `some-domain.example`) `multiaddr::Protocol` is
131+
no longer `Copy`.
126132

127133
[multiaddr]: https://github.com/multiformats/rust-multiaddr
128134
[parity-multiaddr]: https://github.com/libp2p/rust-libp2p/blob/master/misc/multiaddr/

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ keywords = ["multiaddr", "ipfs"]
88
license = "MIT"
99
name = "multiaddr"
1010
readme = "README.md"
11-
version = "0.18.2"
11+
version = "0.18.3"
1212

1313
[features]
1414
default = ["url"]

src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,16 @@ impl Multiaddr {
210210
self.bytes[(n - m)..] == other.bytes[..]
211211
}
212212

213+
/// Checks whether the given `Multiaddr` is a prefix of this `Multiaddr`.
214+
pub fn starts_with(&self, other: &Multiaddr) -> bool {
215+
let n = self.bytes.len();
216+
let m = other.bytes.len();
217+
if n < m {
218+
return false;
219+
}
220+
self.bytes[..m] == other.bytes[..]
221+
}
222+
213223
/// Returns &str identifiers for the protocol names themselves.
214224
/// This omits specific info like addresses, ports, peer IDs, and the like.
215225
/// Example: `"/ip4/127.0.0.1/tcp/5001"` would return `["ip4", "tcp"]`

tests/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ fn ends_with() {
6868
QuickCheck::new().quickcheck(prop as fn(_))
6969
}
7070

71+
#[test]
72+
fn starts_with() {
73+
fn prop(Ma(m): Ma) {
74+
let n = m.iter().count();
75+
for i in 0..n {
76+
let prefix = m.iter().take(i + 1).collect::<Multiaddr>();
77+
assert!(m.starts_with(&prefix));
78+
}
79+
}
80+
QuickCheck::new().quickcheck(prop as fn(_))
81+
}
82+
7183
// Arbitrary impls
7284

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

0 commit comments

Comments
 (0)