Skip to content

make htons const fn #67321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2019
Merged
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
14 changes: 7 additions & 7 deletions src/libstd/net/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::hash;
use crate::io;
use crate::iter;
use crate::mem;
use crate::net::{hton, ntoh, IpAddr, Ipv4Addr, Ipv6Addr};
use crate::net::{htons, ntohs, IpAddr, Ipv4Addr, Ipv6Addr};
use crate::option;
use crate::slice;
use crate::sys::net::netc as c;
Expand Down Expand Up @@ -276,7 +276,7 @@ impl SocketAddrV4 {
SocketAddrV4 {
inner: c::sockaddr_in {
sin_family: c::AF_INET as c::sa_family_t,
sin_port: hton(port),
sin_port: htons(port),
sin_addr: *ip.as_inner(),
..unsafe { mem::zeroed() }
},
Expand Down Expand Up @@ -326,7 +326,7 @@ impl SocketAddrV4 {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn port(&self) -> u16 {
ntoh(self.inner.sin_port)
ntohs(self.inner.sin_port)
}

/// Changes the port number associated with this socket address.
Expand All @@ -342,7 +342,7 @@ impl SocketAddrV4 {
/// ```
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
pub fn set_port(&mut self, new_port: u16) {
self.inner.sin_port = hton(new_port);
self.inner.sin_port = htons(new_port);
}
}

Expand All @@ -368,7 +368,7 @@ impl SocketAddrV6 {
SocketAddrV6 {
inner: c::sockaddr_in6 {
sin6_family: c::AF_INET6 as c::sa_family_t,
sin6_port: hton(port),
sin6_port: htons(port),
sin6_addr: *ip.as_inner(),
sin6_flowinfo: flowinfo,
sin6_scope_id: scope_id,
Expand Down Expand Up @@ -420,7 +420,7 @@ impl SocketAddrV6 {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn port(&self) -> u16 {
ntoh(self.inner.sin6_port)
ntohs(self.inner.sin6_port)
}

/// Changes the port number associated with this socket address.
Expand All @@ -436,7 +436,7 @@ impl SocketAddrV6 {
/// ```
#[stable(feature = "sockaddr_setters", since = "1.9.0")]
pub fn set_port(&mut self, new_port: u16) {
self.inner.sin6_port = hton(new_port);
self.inner.sin6_port = htons(new_port);
}

/// Returns the flow information associated with this address.
Expand Down
19 changes: 4 additions & 15 deletions src/libstd/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,10 @@ pub enum Shutdown {
Both,
}

#[doc(hidden)]
trait NetInt {
fn from_be(i: Self) -> Self;
fn to_be(&self) -> Self;
}
macro_rules! doit {
($($t:ident)*) => ($(impl NetInt for $t {
fn from_be(i: Self) -> Self { <$t>::from_be(i) }
fn to_be(&self) -> Self { <$t>::to_be(*self) }
})*)
}
doit! { i8 i16 i32 i64 isize u8 u16 u32 u64 usize }

fn hton<I: NetInt>(i: I) -> I { i.to_be() }
fn ntoh<I: NetInt>(i: I) -> I { I::from_be(i) }
#[inline]
const fn htons(i: u16) -> u16 { i.to_be() }
#[inline]
const fn ntohs(i: u16) -> u16 { u16::from_be(i) }

fn each_addr<A: ToSocketAddrs, F, T>(addr: A, mut f: F) -> io::Result<T>
where F: FnMut(io::Result<&SocketAddr>) -> io::Result<T>
Expand Down