Skip to content

Commit

Permalink
Rollup merge of rust-lang#100640 - reitermarkus:socket-display-buffer…
Browse files Browse the repository at this point in the history
…, r=thomcc

Use `DisplayBuffer` for socket addresses.

Continuation of rust-lang#100625 for socket addresses.

Renames `net::addr` to `net::addr::socket`, `net::ip` to `net::addr::ip` and `net::ip::display_buffer::IpDisplayBuffer` to `net::addr::display_buffer::DisplayBuffer`.
  • Loading branch information
Dylan-DPC authored Aug 26, 2022
2 parents d35938d + d61ecec commit c1e945e
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use crate::mem::MaybeUninit;
use crate::str;

/// Used for slow path in `Display` implementations when alignment is required.
pub struct IpDisplayBuffer<const SIZE: usize> {
pub struct DisplayBuffer<const SIZE: usize> {
buf: [MaybeUninit<u8>; SIZE],
len: usize,
}

impl<const SIZE: usize> IpDisplayBuffer<SIZE> {
impl<const SIZE: usize> DisplayBuffer<SIZE> {
#[inline]
pub const fn new() -> Self {
Self { buf: MaybeUninit::uninit_array(), len: 0 }
Expand All @@ -25,7 +25,7 @@ impl<const SIZE: usize> IpDisplayBuffer<SIZE> {
}
}

impl<const SIZE: usize> fmt::Write for IpDisplayBuffer<SIZE> {
impl<const SIZE: usize> fmt::Write for DisplayBuffer<SIZE> {
fn write_str(&mut self, s: &str) -> fmt::Result {
let bytes = s.as_bytes();

Expand Down
7 changes: 3 additions & 4 deletions library/std/src/net/ip.rs → library/std/src/net/ip_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use crate::mem::transmute;
use crate::sys::net::netc as c;
use crate::sys_common::{FromInner, IntoInner};

mod display_buffer;
use display_buffer::IpDisplayBuffer;
use super::display_buffer::DisplayBuffer;

/// An IP address, either IPv4 or IPv6.
///
Expand Down Expand Up @@ -997,7 +996,7 @@ impl fmt::Display for Ipv4Addr {
} else {
const LONGEST_IPV4_ADDR: &str = "255.255.255.255";

let mut buf = IpDisplayBuffer::<{ LONGEST_IPV4_ADDR.len() }>::new();
let mut buf = DisplayBuffer::<{ LONGEST_IPV4_ADDR.len() }>::new();
// Buffer is long enough for the longest possible IPv4 address, so this should never fail.
write!(buf, "{}.{}.{}.{}", octets[0], octets[1], octets[2], octets[3]).unwrap();

Expand Down Expand Up @@ -1844,7 +1843,7 @@ impl fmt::Display for Ipv6Addr {
} else {
const LONGEST_IPV6_ADDR: &str = "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff";

let mut buf = IpDisplayBuffer::<{ LONGEST_IPV6_ADDR.len() }>::new();
let mut buf = DisplayBuffer::<{ LONGEST_IPV6_ADDR.len() }>::new();
// Buffer is long enough for the longest possible IPv6 address, so this should never fail.
write!(buf, "{}", self).unwrap();

Expand Down
File renamed without changes.
11 changes: 6 additions & 5 deletions library/std/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@
use crate::io::{self, ErrorKind};

#[stable(feature = "rust1", since = "1.0.0")]
pub use self::addr::{SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs};
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::ip::{IpAddr, Ipv4Addr, Ipv6Addr, Ipv6MulticastScope};
pub use self::ip_addr::{IpAddr, Ipv4Addr, Ipv6Addr, Ipv6MulticastScope};
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::parser::AddrParseError;
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::socket_addr::{SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs};
#[unstable(feature = "tcplistener_into_incoming", issue = "88339")]
pub use self::tcp::IntoIncoming;
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::tcp::{Incoming, TcpListener, TcpStream};
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::udp::UdpSocket;

mod addr;
mod ip;
mod display_buffer;
mod ip_addr;
mod parser;
mod socket_addr;
mod tcp;
#[cfg(test)]
mod test;
Expand Down
58 changes: 22 additions & 36 deletions library/std/src/net/addr.rs → library/std/src/net/socket_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
mod tests;

use crate::cmp::Ordering;
use crate::fmt;
use crate::fmt::{self, Write};
use crate::hash;
use crate::io::{self, Write};
use crate::io;
use crate::iter;
use crate::mem;
use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr};
Expand All @@ -15,6 +15,8 @@ use crate::sys_common::net::LookupHost;
use crate::sys_common::{FromInner, IntoInner};
use crate::vec;

use super::display_buffer::DisplayBuffer;

/// An internet socket address, either IPv4 or IPv6.
///
/// Internet socket addresses consist of an [IP address], a 16-bit port number, as well
Expand Down Expand Up @@ -616,25 +618,18 @@ impl fmt::Debug for SocketAddr {
#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Display for SocketAddrV4 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Fast path: if there's no alignment stuff, write to the output buffer
// directly
// If there are no alignment requirements, write the socket address directly to `f`.
// Otherwise, write it to a local buffer and then use `f.pad`.
if f.precision().is_none() && f.width().is_none() {
write!(f, "{}:{}", self.ip(), self.port())
} else {
const IPV4_SOCKET_BUF_LEN: usize = (3 * 4) // the segments
+ 3 // the separators
+ 1 + 5; // the port
let mut buf = [0; IPV4_SOCKET_BUF_LEN];
let mut buf_slice = &mut buf[..];

// Unwrap is fine because writing to a sufficiently-sized
// buffer is infallible
write!(buf_slice, "{}:{}", self.ip(), self.port()).unwrap();
let len = IPV4_SOCKET_BUF_LEN - buf_slice.len();

// This unsafe is OK because we know what is being written to the buffer
let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) };
f.pad(buf)
const LONGEST_IPV4_SOCKET_ADDR: &str = "255.255.255.255:65536";

let mut buf = DisplayBuffer::<{ LONGEST_IPV4_SOCKET_ADDR.len() }>::new();
// Buffer is long enough for the longest possible IPv4 socket address, so this should never fail.
write!(buf, "{}:{}", self.ip(), self.port()).unwrap();

f.pad(buf.as_str())
}
}
}
Expand All @@ -649,35 +644,26 @@ impl fmt::Debug for SocketAddrV4 {
#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Display for SocketAddrV6 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Fast path: if there's no alignment stuff, write to the output
// buffer directly
// If there are no alignment requirements, write the socket address directly to `f`.
// Otherwise, write it to a local buffer and then use `f.pad`.
if f.precision().is_none() && f.width().is_none() {
match self.scope_id() {
0 => write!(f, "[{}]:{}", self.ip(), self.port()),
scope_id => write!(f, "[{}%{}]:{}", self.ip(), scope_id, self.port()),
}
} else {
const IPV6_SOCKET_BUF_LEN: usize = (4 * 8) // The address
+ 7 // The colon separators
+ 2 // The brackets
+ 1 + 10 // The scope id
+ 1 + 5; // The port

let mut buf = [0; IPV6_SOCKET_BUF_LEN];
let mut buf_slice = &mut buf[..];
const LONGEST_IPV6_SOCKET_ADDR: &str =
"[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%4294967296]:65536";

let mut buf = DisplayBuffer::<{ LONGEST_IPV6_SOCKET_ADDR.len() }>::new();
match self.scope_id() {
0 => write!(buf_slice, "[{}]:{}", self.ip(), self.port()),
scope_id => write!(buf_slice, "[{}%{}]:{}", self.ip(), scope_id, self.port()),
0 => write!(buf, "[{}]:{}", self.ip(), self.port()),
scope_id => write!(buf, "[{}%{}]:{}", self.ip(), scope_id, self.port()),
}
// Unwrap is fine because writing to a sufficiently-sized
// buffer is infallible
// Buffer is long enough for the longest possible IPv6 socket address, so this should never fail.
.unwrap();
let len = IPV6_SOCKET_BUF_LEN - buf_slice.len();

// This unsafe is OK because we know what is being written to the buffer
let buf = unsafe { crate::str::from_utf8_unchecked(&buf[..len]) };
f.pad(buf)
f.pad(buf.as_str())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,75 @@ fn to_socket_addr_string() {
// s has been moved into the tsa call
}

#[test]
fn ipv4_socket_addr_to_string() {
// Shortest possible IPv4 length.
assert_eq!(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), 0).to_string(), "0.0.0.0:0");

// Longest possible IPv4 length.
assert_eq!(
SocketAddrV4::new(Ipv4Addr::new(255, 255, 255, 255), u16::MAX).to_string(),
"255.255.255.255:65535"
);

// Test padding.
assert_eq!(
&format!("{:16}", SocketAddrV4::new(Ipv4Addr::new(1, 1, 1, 1), 53)),
"1.1.1.1:53 "
);
assert_eq!(
&format!("{:>16}", SocketAddrV4::new(Ipv4Addr::new(1, 1, 1, 1), 53)),
" 1.1.1.1:53"
);
}

#[test]
fn ipv6_socket_addr_to_string() {
// IPv4-mapped address.
assert_eq!(
SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc000, 0x280), 8080, 0, 0)
.to_string(),
"[::ffff:192.0.2.128]:8080"
);

// IPv4-compatible address.
assert_eq!(
SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x280), 8080, 0, 0).to_string(),
"[::192.0.2.128]:8080"
);

// IPv6 address with no zero segments.
assert_eq!(
SocketAddrV6::new(Ipv6Addr::new(8, 9, 10, 11, 12, 13, 14, 15), 80, 0, 0).to_string(),
"[8:9:a:b:c:d:e:f]:80"
);

// Shortest possible IPv6 length.
assert_eq!(SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, 0, 0, 0).to_string(), "[::]:0");

// Longest possible IPv6 length.
assert_eq!(
SocketAddrV6::new(
Ipv6Addr::new(0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888),
u16::MAX,
u32::MAX,
u32::MAX,
)
.to_string(),
"[1111:2222:3333:4444:5555:6666:7777:8888%4294967295]:65535"
);

// Test padding.
assert_eq!(
&format!("{:22}", SocketAddrV6::new(Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8), 9, 0, 0)),
"[1:2:3:4:5:6:7:8]:9 "
);
assert_eq!(
&format!("{:>22}", SocketAddrV6::new(Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8), 9, 0, 0)),
" [1:2:3:4:5:6:7:8]:9"
);
}

#[test]
fn bind_udp_socket_bad() {
// rust-lang/rust#53957: This is a regression test for a parsing problem
Expand Down

0 comments on commit c1e945e

Please sign in to comment.