@@ -73,12 +73,9 @@ use crate::{MaybeUninitSlice, MsgHdr, RecvFlags};
73
73
/// # Ok(()) }
74
74
/// ```
75
75
pub struct Socket {
76
- inner : Inner ,
76
+ inner : sys :: Socket ,
77
77
}
78
78
79
- /// Store a `TcpStream` internally to take advantage of its niche optimizations on Unix platforms.
80
- pub ( crate ) type Inner = std:: net:: TcpStream ;
81
-
82
79
impl Socket {
83
80
/// # Safety
84
81
///
@@ -88,24 +85,11 @@ impl Socket {
88
85
/// inconvenient to mark it as `unsafe`.
89
86
pub ( crate ) fn from_raw ( raw : sys:: RawSocket ) -> Socket {
90
87
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) } ,
109
93
}
110
94
}
111
95
0 commit comments