Skip to content

Commit

Permalink
Rollup merge of rust-lang#82487 - CDirkx:const-socketaddr, r=m-ou-se
Browse files Browse the repository at this point in the history
Constify methods of `std::net::SocketAddr`, `SocketAddrV4` and `SocketAddrV6`

The following methods are made unstable const under the `const_socketaddr` feature (rust-lang#82485):

```rust
// std::net

impl SocketAddr {
    pub const fn ip(&self) -> IpAddr;
    pub const fn port(&self) -> u16;
    pub const fn is_ipv4(&self) -> bool;
    pub const fn is_ipv6(&self) -> bool;
}

impl SocketAddrV4 {
    pub const fn ip(&self) -> IpAddr;
    pub const fn port(&self) -> u16;
}

impl SocketAddrV6 {
    pub const fn ip(&self) -> IpAddr;
    pub const fn port(&self) -> u16;
    pub const fn flowinfo(&self) -> u32;
    pub const fn scope_id(&self) -> u32;
}
```

Note: `SocketAddrV4::ip` and `SocketAddrV6::ip` use pointer casting and depend on the unstable feature `const_raw_ptr_deref`
  • Loading branch information
Dylan-DPC committed Apr 3, 2021
2 parents 138fd56 + 5b84b9a commit bd4d338
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@
#![feature(const_ip)]
#![feature(const_ipv6)]
#![feature(const_raw_ptr_deref)]
#![feature(const_socketaddr)]
#![feature(const_ipv4)]
#![feature(container_error_extra)]
#![feature(core_intrinsics)]
Expand Down
30 changes: 20 additions & 10 deletions library/std/src/net/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ impl SocketAddr {
/// assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
/// ```
#[stable(feature = "ip_addr", since = "1.7.0")]
pub fn ip(&self) -> IpAddr {
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
pub const fn ip(&self) -> IpAddr {
match *self {
SocketAddr::V4(ref a) => IpAddr::V4(*a.ip()),
SocketAddr::V6(ref a) => IpAddr::V6(*a.ip()),
Expand Down Expand Up @@ -188,7 +189,8 @@ impl SocketAddr {
/// assert_eq!(socket.port(), 8080);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn port(&self) -> u16 {
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
pub const fn port(&self) -> u16 {
match *self {
SocketAddr::V4(ref a) => a.port(),
SocketAddr::V6(ref a) => a.port(),
Expand Down Expand Up @@ -230,7 +232,8 @@ impl SocketAddr {
/// assert_eq!(socket.is_ipv6(), false);
/// ```
#[stable(feature = "sockaddr_checker", since = "1.16.0")]
pub fn is_ipv4(&self) -> bool {
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
pub const fn is_ipv4(&self) -> bool {
matches!(*self, SocketAddr::V4(_))
}

Expand All @@ -250,7 +253,8 @@ impl SocketAddr {
/// assert_eq!(socket.is_ipv6(), true);
/// ```
#[stable(feature = "sockaddr_checker", since = "1.16.0")]
pub fn is_ipv6(&self) -> bool {
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
pub const fn is_ipv6(&self) -> bool {
matches!(*self, SocketAddr::V6(_))
}
}
Expand Down Expand Up @@ -290,7 +294,8 @@ impl SocketAddrV4 {
/// assert_eq!(socket.ip(), &Ipv4Addr::new(127, 0, 0, 1));
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn ip(&self) -> &Ipv4Addr {
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
pub const fn ip(&self) -> &Ipv4Addr {
// SAFETY: `Ipv4Addr` is `#[repr(C)] struct { _: in_addr; }`.
// It is safe to cast from `&in_addr` to `&Ipv4Addr`.
unsafe { &*(&self.inner.sin_addr as *const c::in_addr as *const Ipv4Addr) }
Expand Down Expand Up @@ -323,7 +328,8 @@ impl SocketAddrV4 {
/// assert_eq!(socket.port(), 8080);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn port(&self) -> u16 {
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
pub const fn port(&self) -> u16 {
ntohs(self.inner.sin_port)
}

Expand Down Expand Up @@ -386,7 +392,8 @@ impl SocketAddrV6 {
/// assert_eq!(socket.ip(), &Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1));
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn ip(&self) -> &Ipv6Addr {
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
pub const fn ip(&self) -> &Ipv6Addr {
unsafe { &*(&self.inner.sin6_addr as *const c::in6_addr as *const Ipv6Addr) }
}

Expand Down Expand Up @@ -417,7 +424,8 @@ impl SocketAddrV6 {
/// assert_eq!(socket.port(), 8080);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn port(&self) -> u16 {
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
pub const fn port(&self) -> u16 {
ntohs(self.inner.sin6_port)
}

Expand Down Expand Up @@ -458,7 +466,8 @@ impl SocketAddrV6 {
/// assert_eq!(socket.flowinfo(), 10);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn flowinfo(&self) -> u32 {
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
pub const fn flowinfo(&self) -> u32 {
self.inner.sin6_flowinfo
}

Expand Down Expand Up @@ -496,7 +505,8 @@ impl SocketAddrV6 {
/// assert_eq!(socket.scope_id(), 78);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn scope_id(&self) -> u32 {
#[rustc_const_unstable(feature = "const_socketaddr", issue = "82485")]
pub const fn scope_id(&self) -> u32 {
self.inner.sin6_scope_id
}

Expand Down

0 comments on commit bd4d338

Please sign in to comment.