summaryrefslogtreecommitdiffstats
path: root/library/core/src/net
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-07 05:48:48 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-07 05:48:48 +0000
commitef24de24a82fe681581cc130f342363c47c0969a (patch)
tree0d494f7e1a38b95c92426f58fe6eaa877303a86c /library/core/src/net
parentReleasing progress-linux version 1.74.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-ef24de24a82fe681581cc130f342363c47c0969a.tar.xz
rustc-ef24de24a82fe681581cc130f342363c47c0969a.zip
Merging upstream version 1.75.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/core/src/net')
-rw-r--r--library/core/src/net/ip_addr.rs154
-rw-r--r--library/core/src/net/socket_addr.rs51
2 files changed, 146 insertions, 59 deletions
diff --git a/library/core/src/net/ip_addr.rs b/library/core/src/net/ip_addr.rs
index 6a36dfec0..77f85215d 100644
--- a/library/core/src/net/ip_addr.rs
+++ b/library/core/src/net/ip_addr.rs
@@ -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;
@@ -410,9 +412,12 @@ impl IpAddr {
/// # Examples
///
/// ```
- /// #![feature(ip)]
/// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
///
+ /// let localhost_v4 = Ipv4Addr::new(127, 0, 0, 1);
+ ///
+ /// assert_eq!(IpAddr::V4(localhost_v4).to_canonical(), localhost_v4);
+ /// assert_eq!(IpAddr::V6(localhost_v4.to_ipv6_mapped()).to_canonical(), localhost_v4);
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).to_canonical().is_loopback(), true);
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).is_loopback(), false);
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1)).to_canonical().is_loopback(), true);
@@ -420,11 +425,11 @@ impl IpAddr {
#[inline]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
- #[rustc_const_unstable(feature = "const_ip", issue = "76205")]
- #[unstable(feature = "ip", issue = "27709")]
+ #[stable(feature = "ip_to_canonical", since = "1.75.0")]
+ #[rustc_const_stable(feature = "ip_to_canonical", since = "1.75.0")]
pub const fn to_canonical(&self) -> IpAddr {
match self {
- &v4 @ IpAddr::V4(_) => v4,
+ IpAddr::V4(_) => *self,
IpAddr::V6(v6) => v6.to_canonical(),
}
}
@@ -1748,11 +1753,11 @@ impl Ipv6Addr {
/// Some(Ipv4Addr::new(192, 10, 2, 255)));
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1).to_ipv4_mapped(), None);
/// ```
- #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
- #[stable(feature = "ipv6_to_ipv4_mapped", since = "1.63.0")]
+ #[inline]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
- #[inline]
+ #[stable(feature = "ipv6_to_ipv4_mapped", since = "1.63.0")]
+ #[rustc_const_stable(feature = "const_ipv6_to_ipv4_mapped", since = "1.75.0")]
pub const fn to_ipv4_mapped(&self) -> Option<Ipv4Addr> {
match self.octets() {
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, a, b, c, d] => {
@@ -1817,11 +1822,11 @@ impl Ipv6Addr {
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).is_loopback(), false);
/// assert_eq!(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x7f00, 0x1).to_canonical().is_loopback(), true);
/// ```
- #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
- #[unstable(feature = "ip", issue = "27709")]
+ #[inline]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
- #[inline]
+ #[stable(feature = "ip_to_canonical", since = "1.75.0")]
+ #[rustc_const_stable(feature = "ip_to_canonical", since = "1.75.0")]
pub const fn to_canonical(&self) -> IpAddr {
if let Some(mapped) = self.to_ipv4_mapped() {
return IpAddr::V4(mapped);
@@ -2122,3 +2127,132 @@ impl From<[u16; 8]> for IpAddr {
IpAddr::V6(Ipv6Addr::from(segments))
}
}
+
+#[stable(feature = "ip_bitops", since = "1.75.0")]
+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 = "1.75.0")]
+impl Not for &'_ Ipv4Addr {
+ type Output = Ipv4Addr;
+
+ #[inline]
+ fn not(self) -> Ipv4Addr {
+ !*self
+ }
+}
+
+#[stable(feature = "ip_bitops", since = "1.75.0")]
+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 = "1.75.0")]
+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 = "1.75.0")]
+ impl (BitAnd, BitAndAssign) for Ipv4Addr = (bitand, bitand_assign);
+ #[stable(feature = "ip_bitops", since = "1.75.0")]
+ impl (BitOr, BitOrAssign) for Ipv4Addr = (bitor, bitor_assign);
+
+ #[stable(feature = "ip_bitops", since = "1.75.0")]
+ impl (BitAnd, BitAndAssign) for Ipv6Addr = (bitand, bitand_assign);
+ #[stable(feature = "ip_bitops", since = "1.75.0")]
+ impl (BitOr, BitOrAssign) for Ipv6Addr = (bitor, bitor_assign);
+}
diff --git a/library/core/src/net/socket_addr.rs b/library/core/src/net/socket_addr.rs
index 8396aecf9..551162858 100644
--- a/library/core/src/net/socket_addr.rs
+++ b/library/core/src/net/socket_addr.rs
@@ -1,6 +1,4 @@
-use crate::cmp::Ordering;
use crate::fmt::{self, Write};
-use crate::hash;
use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use super::display_buffer::DisplayBuffer;
@@ -63,7 +61,7 @@ pub enum SocketAddr {
/// assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1));
/// assert_eq!(socket.port(), 8080);
/// ```
-#[derive(Copy, Clone, Eq, PartialEq)]
+#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct SocketAddrV4 {
ip: Ipv4Addr,
@@ -96,7 +94,7 @@ pub struct SocketAddrV4 {
/// assert_eq!(socket.ip(), &Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 1));
/// assert_eq!(socket.port(), 8080);
/// ```
-#[derive(Copy, Clone, Eq, PartialEq)]
+#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct SocketAddrV6 {
ip: Ipv6Addr,
@@ -644,48 +642,3 @@ impl fmt::Debug for SocketAddrV6 {
fmt::Display::fmt(self, fmt)
}
}
-
-#[stable(feature = "socketaddr_ordering", since = "1.45.0")]
-impl PartialOrd for SocketAddrV4 {
- #[inline]
- fn partial_cmp(&self, other: &SocketAddrV4) -> Option<Ordering> {
- Some(self.cmp(other))
- }
-}
-
-#[stable(feature = "socketaddr_ordering", since = "1.45.0")]
-impl PartialOrd for SocketAddrV6 {
- #[inline]
- fn partial_cmp(&self, other: &SocketAddrV6) -> Option<Ordering> {
- Some(self.cmp(other))
- }
-}
-
-#[stable(feature = "socketaddr_ordering", since = "1.45.0")]
-impl Ord for SocketAddrV4 {
- #[inline]
- fn cmp(&self, other: &SocketAddrV4) -> Ordering {
- self.ip().cmp(other.ip()).then(self.port().cmp(&other.port()))
- }
-}
-
-#[stable(feature = "socketaddr_ordering", since = "1.45.0")]
-impl Ord for SocketAddrV6 {
- #[inline]
- fn cmp(&self, other: &SocketAddrV6) -> Ordering {
- self.ip().cmp(other.ip()).then(self.port().cmp(&other.port()))
- }
-}
-
-#[stable(feature = "rust1", since = "1.0.0")]
-impl hash::Hash for SocketAddrV4 {
- fn hash<H: hash::Hasher>(&self, s: &mut H) {
- (self.port, self.ip).hash(s)
- }
-}
-#[stable(feature = "rust1", since = "1.0.0")]
-impl hash::Hash for SocketAddrV6 {
- fn hash<H: hash::Hasher>(&self, s: &mut H) {
- (self.port, &self.ip, self.flowinfo, self.scope_id).hash(s)
- }
-}