Skip to content

Commit 46bb49a

Browse files
committed
impl Not, Bit{And,Or,Xor}{,Assign} for IP addresses
1 parent 130ff8c commit 46bb49a

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

@@ -2122,3 +2124,155 @@ impl From<[u16; 8]> for IpAddr {
21222124
IpAddr::V6(Ipv6Addr::from(segments))
21232125
}
21242126
}
2127+
2128+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2129+
impl Not for Ipv4Addr {
2130+
type Output = Ipv4Addr;
2131+
2132+
#[inline]
2133+
fn not(mut self) -> Ipv4Addr {
2134+
for octet in &mut self.octets {
2135+
*octet = !*octet;
2136+
}
2137+
self
2138+
}
2139+
}
2140+
2141+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2142+
impl Not for &'_ Ipv4Addr {
2143+
type Output = Ipv4Addr;
2144+
2145+
#[inline]
2146+
fn not(self) -> Ipv4Addr {
2147+
!*self
2148+
}
2149+
}
2150+
2151+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2152+
impl Not for Ipv6Addr {
2153+
type Output = Ipv6Addr;
2154+
2155+
#[inline]
2156+
fn not(mut self) -> Ipv6Addr {
2157+
for octet in &mut self.octets {
2158+
*octet = !*octet;
2159+
}
2160+
self
2161+
}
2162+
}
2163+
2164+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2165+
impl Not for &'_ Ipv6Addr {
2166+
type Output = Ipv6Addr;
2167+
2168+
#[inline]
2169+
fn not(self) -> Ipv6Addr {
2170+
!*self
2171+
}
2172+
}
2173+
2174+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2175+
impl Not for IpAddr {
2176+
type Output = IpAddr;
2177+
2178+
#[inline]
2179+
fn not(self) -> IpAddr {
2180+
match self {
2181+
IpAddr::V4(v4) => IpAddr::V4(!v4),
2182+
IpAddr::V6(v6) => IpAddr::V6(!v6),
2183+
}
2184+
}
2185+
}
2186+
2187+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2188+
impl Not for &'_ IpAddr {
2189+
type Output = IpAddr;
2190+
2191+
#[inline]
2192+
fn not(self) -> IpAddr {
2193+
!*self
2194+
}
2195+
}
2196+
2197+
macro_rules! bitop_impls {
2198+
($(
2199+
$(#[$attr:meta])*
2200+
impl ($BitOp:ident, $BitOpAssign:ident) for $ty:ty = ($bitop:ident, $bitop_assign:ident);
2201+
)*) => {
2202+
$(
2203+
$(#[$attr])*
2204+
impl $BitOpAssign for $ty {
2205+
fn $bitop_assign(&mut self, rhs: $ty) {
2206+
for (lhs, rhs) in iter::zip(&mut self.octets, rhs.octets) {
2207+
lhs.$bitop_assign(rhs);
2208+
}
2209+
}
2210+
}
2211+
2212+
$(#[$attr])*
2213+
impl $BitOpAssign<&'_ $ty> for $ty {
2214+
fn $bitop_assign(&mut self, rhs: &'_ $ty) {
2215+
self.$bitop_assign(*rhs);
2216+
}
2217+
}
2218+
2219+
$(#[$attr])*
2220+
impl $BitOp for $ty {
2221+
type Output = $ty;
2222+
2223+
#[inline]
2224+
fn $bitop(mut self, rhs: $ty) -> $ty {
2225+
self.$bitop_assign(rhs);
2226+
self
2227+
}
2228+
}
2229+
2230+
$(#[$attr])*
2231+
impl $BitOp<&'_ $ty> for $ty {
2232+
type Output = $ty;
2233+
2234+
#[inline]
2235+
fn $bitop(mut self, rhs: &'_ $ty) -> $ty {
2236+
self.$bitop_assign(*rhs);
2237+
self
2238+
}
2239+
}
2240+
2241+
$(#[$attr])*
2242+
impl $BitOp<$ty> for &'_ $ty {
2243+
type Output = $ty;
2244+
2245+
#[inline]
2246+
fn $bitop(self, rhs: $ty) -> $ty {
2247+
let mut lhs = *self;
2248+
lhs.$bitop_assign(rhs);
2249+
lhs
2250+
}
2251+
}
2252+
2253+
$(#[$attr])*
2254+
impl $BitOp<&'_ $ty> for &'_ $ty {
2255+
type Output = $ty;
2256+
2257+
#[inline]
2258+
fn $bitop(self, rhs: &'_ $ty) -> $ty {
2259+
let mut lhs = *self;
2260+
lhs.$bitop_assign(*rhs);
2261+
lhs
2262+
}
2263+
}
2264+
)*
2265+
};
2266+
}
2267+
2268+
bitop_impls! {
2269+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2270+
impl (BitAnd, BitAndAssign) for Ipv4Addr = (bitand, bitand_assign);
2271+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2272+
impl (BitOr, BitOrAssign) for Ipv4Addr = (bitor, bitor_assign);
2273+
2274+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2275+
impl (BitAnd, BitAndAssign) for Ipv6Addr = (bitand, bitand_assign);
2276+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2277+
impl (BitOr, BitOrAssign) for Ipv6Addr = (bitor, bitor_assign);
2278+
}

0 commit comments

Comments
 (0)