Skip to content
Draft
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions library/core/src/net/socket_addr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::display_buffer::DisplayBuffer;
use crate::fmt::{self, Write};
use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use crate::num::NonZero;

/// An internet socket address, either IPv4 or IPv6.
///
Expand Down Expand Up @@ -625,6 +626,20 @@ impl<I: [const] Into<IpAddr>> const From<(I, u16)> for SocketAddr {
}
}

#[stable(feature = "nonzerou16_to_socket_addrs", since = "CURRENT_RUSTC_VERSION")]
#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
impl<I: [const] Into<IpAddr>> const From<(I, NonZero<u16>)> for SocketAddr {
/// Converts a tuple struct (Into<[`IpAddr`]>, [`NonZero<u16>`]) into a [`SocketAddr`].
///
/// This conversion creates a [`SocketAddr::V4`] for an [`IpAddr::V4`]
/// and creates a [`SocketAddr::V6`] for an [`IpAddr::V6`].
///
/// `u16` is treated as port of the newly created [`SocketAddr`].
fn from(pieces: (I, NonZero<u16>)) -> SocketAddr {
SocketAddr::new(pieces.0.into(), pieces.1.get())
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Display for SocketAddr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
41 changes: 41 additions & 0 deletions library/std/src/net/socket_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod tests;

#[stable(feature = "rust1", since = "1.0.0")]
pub use core::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
use core::num::NonZero;

use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use crate::{io, iter, option, slice, vec};
Expand Down Expand Up @@ -171,6 +172,14 @@ impl ToSocketAddrs for (IpAddr, u16) {
}
}

#[stable(feature = "nonzerou16_to_socket_addrs", since = "CURRENT_RUSTC_VERSION")]
impl ToSocketAddrs for (IpAddr, NonZero<u16>) {
type Iter = <(IpAddr, u16) as ToSocketAddrs>::Iter;
fn to_socket_addrs(&self) -> io::Result<Self::Iter> {
(self.0, self.1.get()).to_socket_addrs()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl ToSocketAddrs for (Ipv4Addr, u16) {
type Iter = option::IntoIter<SocketAddr>;
Expand All @@ -180,6 +189,14 @@ impl ToSocketAddrs for (Ipv4Addr, u16) {
}
}

#[stable(feature = "nonzerou16_to_socket_addrs", since = "CURRENT_RUSTC_VERSION")]
impl ToSocketAddrs for (Ipv4Addr, NonZero<u16>) {
type Iter = <(Ipv4Addr, u16) as ToSocketAddrs>::Iter;
fn to_socket_addrs(&self) -> io::Result<Self::Iter> {
(self.0, self.1.get()).to_socket_addrs()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl ToSocketAddrs for (Ipv6Addr, u16) {
type Iter = option::IntoIter<SocketAddr>;
Expand All @@ -189,6 +206,14 @@ impl ToSocketAddrs for (Ipv6Addr, u16) {
}
}

#[stable(feature = "nonzerou16_to_socket_addrs", since = "CURRENT_RUSTC_VERSION")]
impl ToSocketAddrs for (Ipv6Addr, NonZero<u16>) {
type Iter = <(Ipv6Addr, u16) as ToSocketAddrs>::Iter;
fn to_socket_addrs(&self) -> io::Result<Self::Iter> {
(self.0, self.1.get()).to_socket_addrs()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl ToSocketAddrs for (&str, u16) {
type Iter = vec::IntoIter<SocketAddr>;
Expand All @@ -206,6 +231,14 @@ impl ToSocketAddrs for (&str, u16) {
}
}

#[stable(feature = "nonzerou16_to_socket_addrs", since = "CURRENT_RUSTC_VERSION")]
impl<'a> ToSocketAddrs for (&'a str, NonZero<u16>) {
type Iter = <(&'a str, u16) as ToSocketAddrs>::Iter;
fn to_socket_addrs(&self) -> io::Result<Self::Iter> {
(self.0, self.1.get()).to_socket_addrs()
}
}

#[stable(feature = "string_u16_to_socket_addrs", since = "1.46.0")]
impl ToSocketAddrs for (String, u16) {
type Iter = vec::IntoIter<SocketAddr>;
Expand All @@ -214,6 +247,14 @@ impl ToSocketAddrs for (String, u16) {
}
}

#[stable(feature = "nonzerou16_to_socket_addrs", since = "CURRENT_RUSTC_VERSION")]
impl ToSocketAddrs for (String, NonZero<u16>) {
type Iter = <(String, u16) as ToSocketAddrs>::Iter;
fn to_socket_addrs(&self) -> io::Result<Self::Iter> {
(&*self.0, self.1).to_socket_addrs()
}
}

// accepts strings like 'localhost:12345'
#[stable(feature = "rust1", since = "1.0.0")]
impl ToSocketAddrs for str {
Expand Down
Loading