Skip to content

Commit 30d310c

Browse files
committed
Auto merge of rust-lang#113747 - clarfonthey:ip_bitops, r=dtolnay
impl Not, Bit{And,Or}{,Assign} for IP addresses ACP: rust-lang/libs-team#235 Note: since these are insta-stable, these require an FCP. Implements, where `N` is either `4` or `6`: ```rust impl Not for IpvNAddr impl Not for &IpvNAddr impl BitAnd<IpvNAddr> for IpvNAddr impl BitAnd<&IpvNAddr> for IpvNAddr impl BitAnd<IpvNAddr> for &IpvNAddr impl BitAnd<&IpvNAddr> for &IpvNAddr impl BitAndAssign<IpvNAddr> for IpvNAddr impl BitAndAssign<&IpvNAddr> for IpvNAddr impl BitOr<IpvNAddr> for IpvNAddr impl BitOr<&IpvNAddr> for IpvNAddr impl BitOr<IpvNAddr> for &IpvNAddr impl BitOr<&IpvNAddr> for &IpvNAddr impl BitOrAssign<IpvNAddr> for IpvNAddr impl BitOrAssign<&IpvNAddr> for IpvNAddr ```
2 parents 42b1224 + 6b13950 commit 30d310c

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

library/core/src/net/ip_addr.rs

+131
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,132 @@ 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+
macro_rules! bitop_impls {
2175+
($(
2176+
$(#[$attr:meta])*
2177+
impl ($BitOp:ident, $BitOpAssign:ident) for $ty:ty = ($bitop:ident, $bitop_assign:ident);
2178+
)*) => {
2179+
$(
2180+
$(#[$attr])*
2181+
impl $BitOpAssign for $ty {
2182+
fn $bitop_assign(&mut self, rhs: $ty) {
2183+
for (lhs, rhs) in iter::zip(&mut self.octets, rhs.octets) {
2184+
lhs.$bitop_assign(rhs);
2185+
}
2186+
}
2187+
}
2188+
2189+
$(#[$attr])*
2190+
impl $BitOpAssign<&'_ $ty> for $ty {
2191+
fn $bitop_assign(&mut self, rhs: &'_ $ty) {
2192+
self.$bitop_assign(*rhs);
2193+
}
2194+
}
2195+
2196+
$(#[$attr])*
2197+
impl $BitOp for $ty {
2198+
type Output = $ty;
2199+
2200+
#[inline]
2201+
fn $bitop(mut self, rhs: $ty) -> $ty {
2202+
self.$bitop_assign(rhs);
2203+
self
2204+
}
2205+
}
2206+
2207+
$(#[$attr])*
2208+
impl $BitOp<&'_ $ty> for $ty {
2209+
type Output = $ty;
2210+
2211+
#[inline]
2212+
fn $bitop(mut self, rhs: &'_ $ty) -> $ty {
2213+
self.$bitop_assign(*rhs);
2214+
self
2215+
}
2216+
}
2217+
2218+
$(#[$attr])*
2219+
impl $BitOp<$ty> for &'_ $ty {
2220+
type Output = $ty;
2221+
2222+
#[inline]
2223+
fn $bitop(self, rhs: $ty) -> $ty {
2224+
let mut lhs = *self;
2225+
lhs.$bitop_assign(rhs);
2226+
lhs
2227+
}
2228+
}
2229+
2230+
$(#[$attr])*
2231+
impl $BitOp<&'_ $ty> for &'_ $ty {
2232+
type Output = $ty;
2233+
2234+
#[inline]
2235+
fn $bitop(self, rhs: &'_ $ty) -> $ty {
2236+
let mut lhs = *self;
2237+
lhs.$bitop_assign(*rhs);
2238+
lhs
2239+
}
2240+
}
2241+
)*
2242+
};
2243+
}
2244+
2245+
bitop_impls! {
2246+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2247+
impl (BitAnd, BitAndAssign) for Ipv4Addr = (bitand, bitand_assign);
2248+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2249+
impl (BitOr, BitOrAssign) for Ipv4Addr = (bitor, bitor_assign);
2250+
2251+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2252+
impl (BitAnd, BitAndAssign) for Ipv6Addr = (bitand, bitand_assign);
2253+
#[stable(feature = "ip_bitops", since = "CURRENT_RUSTC_VERSION")]
2254+
impl (BitOr, BitOrAssign) for Ipv6Addr = (bitor, bitor_assign);
2255+
}

0 commit comments

Comments
 (0)