wip: add Ipv6Prefix::with_client_id impl
Some checks failed
/ check-renovaterc (push) Successful in 8s
/ build (push) Successful in 27s
/ test (push) Failing after 1m43s
/ report-size (push) Successful in 10s

This commit is contained in:
Jalil David Salamé Messina 2025-02-05 23:13:32 +01:00
parent 2d00dd3818
commit c64be28e9a
Signed by: jalil
GPG key ID: F016B9E770737A0B

View file

@ -230,6 +230,29 @@ struct Ipv6Prefix {
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 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { prefix, length } = self;