Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl Not, Bit{And,Or}{,Assign} for IP addresses #113747

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions library/core/src/net/ip_addr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::cmp::Ordering;
use crate::fmt::{self, Write};
use crate::iter;
use crate::mem::transmute;
use crate::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not};

use super::display_buffer::DisplayBuffer;

Expand Down Expand Up @@ -2122,3 +2124,132 @@ impl From<[u16; 8]> for IpAddr {
IpAddr::V6(Ipv6Addr::from(segments))
}
}

#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
impl Not for Ipv4Addr {
type Output = Ipv4Addr;

#[inline]
fn not(mut self) -> Ipv4Addr {
for octet in &mut self.octets {
*octet = !*octet;
}
self
}
}

#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
impl Not for &'_ Ipv4Addr {
type Output = Ipv4Addr;

#[inline]
fn not(self) -> Ipv4Addr {
!*self
}
}

#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
impl Not for Ipv6Addr {
type Output = Ipv6Addr;

#[inline]
fn not(mut self) -> Ipv6Addr {
for octet in &mut self.octets {
*octet = !*octet;
}
self
}
}

#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
impl Not for &'_ Ipv6Addr {
type Output = Ipv6Addr;

#[inline]
fn not(self) -> Ipv6Addr {
!*self
}
}

macro_rules! bitop_impls {
($(
$(#[$attr:meta])*
impl ($BitOp:ident, $BitOpAssign:ident) for $ty:ty = ($bitop:ident, $bitop_assign:ident);
)*) => {
$(
$(#[$attr])*
impl $BitOpAssign for $ty {
fn $bitop_assign(&mut self, rhs: $ty) {
for (lhs, rhs) in iter::zip(&mut self.octets, rhs.octets) {
lhs.$bitop_assign(rhs);
}
}
}

$(#[$attr])*
impl $BitOpAssign<&'_ $ty> for $ty {
fn $bitop_assign(&mut self, rhs: &'_ $ty) {
self.$bitop_assign(*rhs);
}
}

$(#[$attr])*
impl $BitOp for $ty {
type Output = $ty;

#[inline]
fn $bitop(mut self, rhs: $ty) -> $ty {
self.$bitop_assign(rhs);
self
}
}

$(#[$attr])*
impl $BitOp<&'_ $ty> for $ty {
type Output = $ty;

#[inline]
fn $bitop(mut self, rhs: &'_ $ty) -> $ty {
self.$bitop_assign(*rhs);
self
}
}

$(#[$attr])*
impl $BitOp<$ty> for &'_ $ty {
type Output = $ty;

#[inline]
fn $bitop(self, rhs: $ty) -> $ty {
let mut lhs = *self;
lhs.$bitop_assign(rhs);
lhs
}
}

$(#[$attr])*
impl $BitOp<&'_ $ty> for &'_ $ty {
type Output = $ty;

#[inline]
fn $bitop(self, rhs: &'_ $ty) -> $ty {
let mut lhs = *self;
lhs.$bitop_assign(*rhs);
lhs
}
}
)*
};
}

bitop_impls! {
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
impl (BitAnd, BitAndAssign) for Ipv4Addr = (bitand, bitand_assign);
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
impl (BitOr, BitOrAssign) for Ipv4Addr = (bitor, bitor_assign);

#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
impl (BitAnd, BitAndAssign) for Ipv6Addr = (bitand, bitand_assign);
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
impl (BitOr, BitOrAssign) for Ipv6Addr = (bitor, bitor_assign);
}
Loading