Skip to content

Commit 6bf1d9e

Browse files
committed
fix (mutating) BitString implementation for AnyIpCidr
1 parent a553280 commit 6bf1d9e

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

src/cidr/any.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ impl bitstring::BitString for AnyIpCidr {
293293
} else {
294294
match self {
295295
Self::Any => unreachable!(),
296-
Self::V4(mut c) => c.set(ndx - 1, bit),
297-
Self::V6(mut c) => c.set(ndx - 1, bit),
296+
Self::V4(ref mut c) => c.set(ndx - 1, bit),
297+
Self::V6(ref mut c) => c.set(ndx - 1, bit),
298298
}
299299
}
300300
}
@@ -310,8 +310,8 @@ impl bitstring::BitString for AnyIpCidr {
310310
} else {
311311
match self {
312312
Self::Any => unreachable!(),
313-
Self::V4(mut c) => c.flip(ndx - 1),
314-
Self::V6(mut c) => c.flip(ndx - 1),
313+
Self::V4(ref mut c) => c.flip(ndx - 1),
314+
Self::V6(ref mut c) => c.flip(ndx - 1),
315315
}
316316
}
317317
}
@@ -334,8 +334,8 @@ impl bitstring::BitString for AnyIpCidr {
334334
} else {
335335
match self {
336336
Self::Any => (),
337-
Self::V4(mut c) => c.clip(len - 1),
338-
Self::V6(mut c) => c.clip(len - 1),
337+
Self::V4(ref mut c) => c.clip(len - 1),
338+
Self::V6(ref mut c) => c.clip(len - 1),
339339
}
340340
}
341341
}
@@ -349,8 +349,8 @@ impl bitstring::BitString for AnyIpCidr {
349349
*self = Self::V4(Ipv4Cidr::null());
350350
}
351351
},
352-
Self::V4(mut c) => c.append(bit),
353-
Self::V6(mut c) => c.append(bit),
352+
Self::V4(ref mut c) => c.append(bit),
353+
Self::V6(ref mut c) => c.append(bit),
354354
}
355355
}
356356

src/cidr/bitstring_tests.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use bitstring::BitString;
2+
3+
use crate::{
4+
AnyIpCidr,
5+
Ipv4Cidr,
6+
Ipv6Cidr,
7+
};
8+
9+
#[test]
10+
fn ipv4_clip() {
11+
let mut c = "192.168.0.0/16".parse::<Ipv4Cidr>().unwrap();
12+
c.clip(2);
13+
assert_eq!(c.len(), 2);
14+
assert_eq!(c, "192.0.0.0/2".parse::<Ipv4Cidr>().unwrap());
15+
}
16+
17+
#[test]
18+
fn ipv4_any_clip() {
19+
use bitstring::BitString;
20+
let mut c = "192.168.0.0/16".parse::<AnyIpCidr>().unwrap();
21+
c.clip(2);
22+
assert_eq!(c.len(), 2);
23+
assert_eq!(c, "128.0.0.0/1".parse::<AnyIpCidr>().unwrap());
24+
}
25+
26+
#[test]
27+
fn ipv6_clip() {
28+
use bitstring::BitString;
29+
let mut c = "2001:db8:1234:5678::/64".parse::<Ipv6Cidr>().unwrap();
30+
c.clip(4);
31+
assert_eq!(c.len(), 4);
32+
assert_eq!(c, "2000::/4".parse::<Ipv6Cidr>().unwrap());
33+
}
34+
35+
#[test]
36+
fn ipv6_any_clip() {
37+
use bitstring::BitString;
38+
let mut c = "2001:db8:1234:5678::/64".parse::<AnyIpCidr>().unwrap();
39+
c.clip(4);
40+
assert_eq!(c.len(), 4);
41+
assert_eq!(c, "2000::/3".parse::<AnyIpCidr>().unwrap());
42+
}
43+

src/cidr/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ mod serde;
88

99
#[cfg(test)]
1010
mod tests;
11+
#[cfg(feature = "bitstring")]
12+
#[cfg(test)]
13+
mod bitstring_tests;
1114

1215
use std::net::{
1316
Ipv4Addr,

0 commit comments

Comments
 (0)