Skip to content

Commit e97499a

Browse files
committed
impl Not, Bit{And,Or,Xor}{,Assign} for IP addresses
1 parent 4124617 commit e97499a

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed

library/core/src/net/ip_addr.rs

+154
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::cmp::Ordering;
22
use crate::fmt::{self, Write};
3+
use crate::iter;
34
use crate::mem::transmute;
5+
use crate::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not};
46

57
use super::display_buffer::DisplayBuffer;
68

@@ -2062,3 +2064,155 @@ impl From<[u16; 8]> for IpAddr {
20622064
IpAddr::V6(Ipv6Addr::from(segments))
20632065
}
20642066
}
2067+
2068+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2069+
impl Not for Ipv4Addr {
2070+
type Output = Ipv4Addr;
2071+
2072+
#[inline]
2073+
fn not(mut self) -> Ipv4Addr {
2074+
for octet in &mut self.octets {
2075+
*octet = !*octet;
2076+
}
2077+
self
2078+
}
2079+
}
2080+
2081+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2082+
impl Not for &'_ Ipv4Addr {
2083+
type Output = Ipv4Addr;
2084+
2085+
#[inline]
2086+
fn not(self) -> Ipv4Addr {
2087+
!*self
2088+
}
2089+
}
2090+
2091+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2092+
impl Not for Ipv6Addr {
2093+
type Output = Ipv6Addr;
2094+
2095+
#[inline]
2096+
fn not(mut self) -> Ipv6Addr {
2097+
for octet in &mut self.octets {
2098+
*octet = !*octet;
2099+
}
2100+
self
2101+
}
2102+
}
2103+
2104+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2105+
impl Not for &'_ Ipv6Addr {
2106+
type Output = Ipv6Addr;
2107+
2108+
#[inline]
2109+
fn not(self) -> Ipv6Addr {
2110+
!*self
2111+
}
2112+
}
2113+
2114+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2115+
impl Not for IpAddr {
2116+
type Output = IpAddr;
2117+
2118+
#[inline]
2119+
fn not(self) -> IpAddr {
2120+
match self {
2121+
IpAddr::V4(v4) => IpAddr::V4(!v4),
2122+
IpAddr::V6(v6) => IpAddr::V6(!v6),
2123+
}
2124+
}
2125+
}
2126+
2127+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2128+
impl Not for &'_ IpAddr {
2129+
type Output = IpAddr;
2130+
2131+
#[inline]
2132+
fn not(self) -> IpAddr {
2133+
!*self
2134+
}
2135+
}
2136+
2137+
macro_rules! bitop_impls {
2138+
($(
2139+
$(#[$attr:meta])*
2140+
impl ($BitOp:ident, $BitOpAssign:ident) for $ty:ty = ($bitop:ident, $bitop_assign:ident);
2141+
)*) => {
2142+
$(
2143+
$(#[$attr])*
2144+
impl $BitOpAssign for $ty {
2145+
fn $bitop_assign(&mut self, rhs: $ty) {
2146+
for (lhs, rhs) in iter::zip(&mut self.octets, rhs.octets) {
2147+
lhs.$bitop_assign(rhs);
2148+
}
2149+
}
2150+
}
2151+
2152+
$(#[$attr])*
2153+
impl $BitOpAssign<&'_ $ty> for $ty {
2154+
fn $bitop_assign(&mut self, rhs: &'_ $ty) {
2155+
self.$bitop_assign(*rhs);
2156+
}
2157+
}
2158+
2159+
$(#[$attr])*
2160+
impl $BitOp for $ty {
2161+
type Output = $ty;
2162+
2163+
#[inline]
2164+
fn $bitop(mut self, rhs: $ty) -> $ty {
2165+
self.$bitop_assign(rhs);
2166+
self
2167+
}
2168+
}
2169+
2170+
$(#[$attr])*
2171+
impl $BitOp<&'_ $ty> for $ty {
2172+
type Output = $ty;
2173+
2174+
#[inline]
2175+
fn $bitop(mut self, rhs: &'_ $ty) -> $ty {
2176+
self.$bitop_assign(*rhs);
2177+
self
2178+
}
2179+
}
2180+
2181+
$(#[$attr])*
2182+
impl $BitOp<$ty> for &'_ $ty {
2183+
type Output = $ty;
2184+
2185+
#[inline]
2186+
fn $bitop(self, rhs: $ty) -> $ty {
2187+
let mut lhs = *self;
2188+
lhs.$bitop_assign(rhs);
2189+
lhs
2190+
}
2191+
}
2192+
2193+
$(#[$attr])*
2194+
impl $BitOp<&'_ $ty> for &'_ $ty {
2195+
type Output = $ty;
2196+
2197+
#[inline]
2198+
fn $bitop(self, rhs: &'_ $ty) -> $ty {
2199+
let mut lhs = *self;
2200+
lhs.$bitop_assign(*rhs);
2201+
lhs
2202+
}
2203+
}
2204+
)*
2205+
};
2206+
}
2207+
2208+
bitop_impls! {
2209+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2210+
impl (BitAnd, BitAndAssign) for Ipv4Addr = (bitand, bitand_assign);
2211+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2212+
impl (BitOr, BitOrAssign) for Ipv4Addr = (bitor, bitor_assign);
2213+
2214+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2215+
impl (BitAnd, BitAndAssign) for Ipv6Addr = (bitand, bitand_assign);
2216+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2217+
impl (BitOr, BitOrAssign) for Ipv6Addr = (bitor, bitor_assign);
2218+
}

0 commit comments

Comments
 (0)