Skip to content

Commit 0595fd8

Browse files
Rollup merge of rust-lang#67321 - lzutao:htons, r=dtolnay
make htons const fn This may partially help rust-lang#67315.
2 parents c605199 + 3594d8b commit 0595fd8

File tree

2 files changed

+11
-22
lines changed

2 files changed

+11
-22
lines changed

src/libstd/net/addr.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::hash;
44
use crate::io;
55
use crate::iter;
66
use crate::mem;
7-
use crate::net::{hton, ntoh, IpAddr, Ipv4Addr, Ipv6Addr};
7+
use crate::net::{htons, ntohs, IpAddr, Ipv4Addr, Ipv6Addr};
88
use crate::option;
99
use crate::slice;
1010
use crate::sys::net::netc as c;
@@ -276,7 +276,7 @@ impl SocketAddrV4 {
276276
SocketAddrV4 {
277277
inner: c::sockaddr_in {
278278
sin_family: c::AF_INET as c::sa_family_t,
279-
sin_port: hton(port),
279+
sin_port: htons(port),
280280
sin_addr: *ip.as_inner(),
281281
..unsafe { mem::zeroed() }
282282
},
@@ -326,7 +326,7 @@ impl SocketAddrV4 {
326326
/// ```
327327
#[stable(feature = "rust1", since = "1.0.0")]
328328
pub fn port(&self) -> u16 {
329-
ntoh(self.inner.sin_port)
329+
ntohs(self.inner.sin_port)
330330
}
331331

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

@@ -368,7 +368,7 @@ impl SocketAddrV6 {
368368
SocketAddrV6 {
369369
inner: c::sockaddr_in6 {
370370
sin6_family: c::AF_INET6 as c::sa_family_t,
371-
sin6_port: hton(port),
371+
sin6_port: htons(port),
372372
sin6_addr: *ip.as_inner(),
373373
sin6_flowinfo: flowinfo,
374374
sin6_scope_id: scope_id,
@@ -420,7 +420,7 @@ impl SocketAddrV6 {
420420
/// ```
421421
#[stable(feature = "rust1", since = "1.0.0")]
422422
pub fn port(&self) -> u16 {
423-
ntoh(self.inner.sin6_port)
423+
ntohs(self.inner.sin6_port)
424424
}
425425

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

442442
/// Returns the flow information associated with this address.

src/libstd/net/mod.rs

+4-15
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,10 @@ pub enum Shutdown {
8585
Both,
8686
}
8787

88-
#[doc(hidden)]
89-
trait NetInt {
90-
fn from_be(i: Self) -> Self;
91-
fn to_be(&self) -> Self;
92-
}
93-
macro_rules! doit {
94-
($($t:ident)*) => ($(impl NetInt for $t {
95-
fn from_be(i: Self) -> Self { <$t>::from_be(i) }
96-
fn to_be(&self) -> Self { <$t>::to_be(*self) }
97-
})*)
98-
}
99-
doit! { i8 i16 i32 i64 isize u8 u16 u32 u64 usize }
100-
101-
fn hton<I: NetInt>(i: I) -> I { i.to_be() }
102-
fn ntoh<I: NetInt>(i: I) -> I { I::from_be(i) }
88+
#[inline]
89+
const fn htons(i: u16) -> u16 { i.to_be() }
90+
#[inline]
91+
const fn ntohs(i: u16) -> u16 { u16::from_be(i) }
10392

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

0 commit comments

Comments
 (0)