@@ -73,12 +73,9 @@ use crate::{MaybeUninitSlice, MsgHdr, RecvFlags};
7373/// # Ok(()) }
7474/// ```
7575pub struct Socket {
76- inner : Inner ,
76+ inner : sys :: Socket ,
7777}
7878
79- /// Store a `TcpStream` internally to take advantage of its niche optimizations on Unix platforms.
80- pub ( crate ) type Inner = std:: net:: TcpStream ;
81-
8279impl Socket {
8380 /// # Safety
8481 ///
@@ -88,24 +85,11 @@ impl Socket {
8885 /// inconvenient to mark it as `unsafe`.
8986 pub ( crate ) fn from_raw ( raw : sys:: RawSocket ) -> Socket {
9087 Socket {
91- inner : unsafe {
92- // SAFETY: the caller must ensure that `raw` is a valid file
93- // descriptor, but when it isn't it could return I/O errors, or
94- // potentially close a fd it doesn't own. All of that isn't
95- // memory unsafe, so it's not desired but never memory unsafe or
96- // causes UB.
97- //
98- // However there is one exception. We use `TcpStream` to
99- // represent the `Socket` internally (see `Inner` type),
100- // `TcpStream` has a layout optimisation that doesn't allow for
101- // negative file descriptors (as those are always invalid).
102- // Violating this assumption (fd never negative) causes UB,
103- // something we don't want. So check for that we have this
104- // `assert!`.
105- #[ cfg( unix) ]
106- assert ! ( raw >= 0 , "tried to create a `Socket` with an invalid fd" ) ;
107- sys:: socket_from_raw ( raw)
108- } ,
88+ // SAFETY: the caller must ensure that `raw` is a valid file
89+ // descriptor, but when it isn't it could return I/O errors, or
90+ // potentially close a fd it doesn't own. All of that isn't memory
91+ // unsafe, so it's not desired but never memory unsafe or causes UB.
92+ inner : unsafe { sys:: socket_from_raw ( raw) } ,
10993 }
11094 }
11195
0 commit comments