Skip to content

Commit bcd66f8

Browse files
authored
Rollup merge of rust-lang#83524 - faern:document-socketaddr-mem-layout, r=sfackler
Document that the SocketAddr memory representation is not stable Intended to help out with rust-lang#78802. Work has been put into finding and fixing code that assumes the memory layout of `SocketAddrV4` and `SocketAddrV6`. But it turns out there are cases where new code continues to make the same assumption ([example](spacejam/seaslug@96927dc#diff-917db3d8ca6f862ebf42726b23c72a12b35e584e497ebdb24e474348d7c6ffb6R610-R621)). The memory layout of a type in `std` is never part of the public API. Unless explicitly stated I guess. But since that is invalidly relied upon by a considerable amount of code for these particular types, it might make sense to explicitly document this. This can be temporary. Once rust-lang#78802 lands it does not make sense to rely on the layout any longer, and this documentation can also be removed.
2 parents 33cc71a + 55dbab5 commit bcd66f8

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

std/src/net/addr.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ pub enum SocketAddr {
5757
/// See [`SocketAddr`] for a type encompassing both IPv4 and IPv6 socket addresses.
5858
///
5959
/// The size of a `SocketAddrV4` struct may vary depending on the target operating
60-
/// system.
60+
/// system. Do not assume that this type has the same memory layout as the underlying
61+
/// system representation.
6162
///
6263
/// [IETF RFC 793]: https://tools.ietf.org/html/rfc793
6364
/// [`IPv4` address]: Ipv4Addr
@@ -76,6 +77,8 @@ pub enum SocketAddr {
7677
#[derive(Copy)]
7778
#[stable(feature = "rust1", since = "1.0.0")]
7879
pub struct SocketAddrV4 {
80+
// Do not assume that this struct is implemented as the underlying system representation.
81+
// The memory layout is not part of the stable interface that std exposes.
7982
inner: c::sockaddr_in,
8083
}
8184

@@ -88,7 +91,8 @@ pub struct SocketAddrV4 {
8891
/// See [`SocketAddr`] for a type encompassing both IPv4 and IPv6 socket addresses.
8992
///
9093
/// The size of a `SocketAddrV6` struct may vary depending on the target operating
91-
/// system.
94+
/// system. Do not assume that this type has the same memory layout as the underlying
95+
/// system representation.
9296
///
9397
/// [IETF RFC 2553, Section 3.3]: https://tools.ietf.org/html/rfc2553#section-3.3
9498
/// [`IPv6` address]: Ipv6Addr
@@ -107,6 +111,8 @@ pub struct SocketAddrV4 {
107111
#[derive(Copy)]
108112
#[stable(feature = "rust1", since = "1.0.0")]
109113
pub struct SocketAddrV6 {
114+
// Do not assume that this struct is implemented as the underlying system representation.
115+
// The memory layout is not part of the stable interface that std exposes.
110116
inner: c::sockaddr_in6,
111117
}
112118

0 commit comments

Comments
 (0)