wip: add Ipv6Prefix::with_client_id impl
Some checks failed
/ build (push) Successful in 27s
/ check (clippy) (push) Failing after 11s
/ check (module-ipv4-only-test) (push) Successful in 30s
/ check (module-ipv4-test) (push) Successful in 29s
/ check (module-ipv6-only-test) (push) Successful in 29s
/ check (module-ipv6-test) (push) Successful in 29s
/ check (module-nginx-test) (push) Successful in 30s
/ check (nextest) (push) Successful in 3s
/ check (treefmt) (push) Successful in 3s
/ report-size (push) Successful in 7s
Some checks failed
/ build (push) Successful in 27s
/ check (clippy) (push) Failing after 11s
/ check (module-ipv4-only-test) (push) Successful in 30s
/ check (module-ipv4-test) (push) Successful in 29s
/ check (module-ipv6-only-test) (push) Successful in 29s
/ check (module-ipv6-test) (push) Successful in 29s
/ check (module-nginx-test) (push) Successful in 30s
/ check (nextest) (push) Successful in 3s
/ check (treefmt) (push) Successful in 3s
/ report-size (push) Successful in 7s
This commit is contained in:
parent
172076eaad
commit
87c3e31160
1 changed files with 23 additions and 0 deletions
23
src/main.rs
23
src/main.rs
|
@ -291,6 +291,29 @@ struct Ipv6Prefix {
|
||||||
length: u32,
|
length: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Ipv6Prefix {
|
||||||
|
/// Create an [`Ipv6Addr`] from a prefix and a client id
|
||||||
|
pub fn with_client_id(self, client_id: Ipv6Addr) -> Ipv6Addr {
|
||||||
|
let Self { prefix, length } = self;
|
||||||
|
// Clear the last `length` bits
|
||||||
|
let prefix_mask = u128::MAX << length;
|
||||||
|
let client_mask = !prefix_mask;
|
||||||
|
let prefix = prefix.to_bits();
|
||||||
|
let client = client_id.to_bits();
|
||||||
|
debug_assert_eq!(
|
||||||
|
prefix & client_mask,
|
||||||
|
0,
|
||||||
|
"prefix contains bits in client id part"
|
||||||
|
);
|
||||||
|
debug_assert_eq!(
|
||||||
|
client & prefix_mask,
|
||||||
|
0,
|
||||||
|
"client id contains bits in prefix part"
|
||||||
|
);
|
||||||
|
Ipv6Addr::from_bits((prefix & prefix_mask) | (client & client_mask))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for Ipv6Prefix {
|
impl std::fmt::Display for Ipv6Prefix {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
let Self { prefix, length } = self;
|
let Self { prefix, length } = self;
|
||||||
|
|
Loading…
Add table
Reference in a new issue