| 1 |
//! The following is derived from Rust's |
| 2 |
//! library/std/src/os/windows/io/socket.rs |
| 3 |
//! at revision |
| 4 |
//! 4f9b394c8a24803e57ba892fa00e539742ebafc0. |
| 5 |
//! |
| 6 |
//! All code in this file is licensed MIT or Apache 2.0 at your option. |
| 7 |
|
| 8 |
use super::raw::*; |
| 9 |
use crate::backend::c; |
| 10 |
use core::fmt; |
| 11 |
use core::marker::PhantomData; |
| 12 |
use core::mem::forget; |
| 13 |
|
| 14 |
/// A borrowed socket. |
| 15 |
/// |
| 16 |
/// This has a lifetime parameter to tie it to the lifetime of something that |
| 17 |
/// owns the socket. |
| 18 |
/// |
| 19 |
/// This uses `repr(transparent)` and has the representation of a host socket, |
| 20 |
/// so it can be used in FFI in places where a socket is passed as an argument, |
| 21 |
/// it is not captured or consumed, and it never has the value |
| 22 |
/// `INVALID_SOCKET`. |
| 23 |
/// |
| 24 |
/// This type's `.to_owned()` implementation returns another `BorrowedSocket` |
| 25 |
/// rather than an `OwnedSocket`. It just makes a trivial copy of the raw |
| 26 |
/// socket, which is then borrowed under the same lifetime. |
| 27 |
#[derive(Copy, Clone)] |
| 28 |
#[repr(transparent)] |
| 29 |
#[cfg_attr(staged_api, rustc_layout_scalar_valid_range_start(0))] |
| 30 |
// This is -2, in two's complement. -1 is `INVALID_SOCKET`. |
| 31 |
#[cfg_attr( |
| 32 |
all(staged_api, target_pointer_width = "32"), |
| 33 |
rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE) |
| 34 |
)] |
| 35 |
#[cfg_attr( |
| 36 |
all(staged_api, target_pointer_width = "64"), |
| 37 |
rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FF_FF_FF_FF_FE) |
| 38 |
)] |
| 39 |
#[cfg_attr(staged_api, rustc_nonnull_optimization_guaranteed)] |
| 40 |
#[cfg_attr(staged_api, stable(feature = "io_safety", since = "1.63.0"))] |
| 41 |
pub struct BorrowedSocket<'socket> { |
| 42 |
socket: RawSocket, |
| 43 |
_phantom: PhantomData<&'socket OwnedSocket>, |
| 44 |
} |
| 45 |
|
| 46 |
/// An owned socket. |
| 47 |
/// |
| 48 |
/// This closes the socket on drop. |
| 49 |
/// |
| 50 |
/// This uses `repr(transparent)` and has the representation of a host socket, |
| 51 |
/// so it can be used in FFI in places where a socket is passed as a consumed |
| 52 |
/// argument or returned as an owned value, and it never has the value |
| 53 |
/// `INVALID_SOCKET`. |
| 54 |
#[repr(transparent)] |
| 55 |
#[cfg_attr(staged_api, rustc_layout_scalar_valid_range_start(0))] |
| 56 |
// This is -2, in two's complement. -1 is `INVALID_SOCKET`. |
| 57 |
#[cfg_attr( |
| 58 |
all(staged_api, target_pointer_width = "32"), |
| 59 |
rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE) |
| 60 |
)] |
| 61 |
#[cfg_attr( |
| 62 |
all(staged_api, target_pointer_width = "64"), |
| 63 |
rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FF_FF_FF_FF_FE) |
| 64 |
)] |
| 65 |
#[cfg_attr(staged_api, rustc_nonnull_optimization_guaranteed)] |
| 66 |
#[cfg_attr(staged_api, stable(feature = "io_safety", since = "1.63.0"))] |
| 67 |
pub struct OwnedSocket { |
| 68 |
socket: RawSocket, |
| 69 |
} |
| 70 |
|
| 71 |
impl BorrowedSocket<'_> { |
| 72 |
/// Returns a `BorrowedSocket` holding the given raw socket. |
| 73 |
/// |
| 74 |
/// # Safety |
| 75 |
/// |
| 76 |
/// The resource pointed to by `raw` must remain open for the duration of |
| 77 |
/// the returned `BorrowedSocket`, and it must not have the value |
| 78 |
/// `INVALID_SOCKET`. |
| 79 |
#[inline] |
| 80 |
#[cfg_attr( |
| 81 |
staged_api, |
| 82 |
rustc_const_stable(feature = "io_safety", since = "1.63.0") |
| 83 |
)] |
| 84 |
#[cfg_attr(staged_api, stable(feature = "io_safety", since = "1.63.0"))] |
| 85 |
pub const unsafe fn borrow_raw(socket: RawSocket) -> Self { |
| 86 |
assert!(socket != c::INVALID_SOCKET as RawSocket); |
| 87 |
Self { |
| 88 |
socket, |
| 89 |
_phantom: PhantomData, |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
#[cfg_attr(staged_api, stable(feature = "io_safety", since = "1.63.0"))] |
| 95 |
impl AsRawSocket for BorrowedSocket<'_> { |
| 96 |
#[inline] |
| 97 |
fn as_raw_socket(&self) -> RawSocket { |
| 98 |
self.socket |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
#[cfg_attr(staged_api, stable(feature = "io_safety", since = "1.63.0"))] |
| 103 |
impl AsRawSocket for OwnedSocket { |
| 104 |
#[inline] |
| 105 |
fn as_raw_socket(&self) -> RawSocket { |
| 106 |
self.socket |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
#[cfg_attr(staged_api, stable(feature = "io_safety", since = "1.63.0"))] |
| 111 |
impl IntoRawSocket for OwnedSocket { |
| 112 |
#[inline] |
| 113 |
fn into_raw_socket(self) -> RawSocket { |
| 114 |
let socket = self.socket; |
| 115 |
forget(self); |
| 116 |
socket |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
#[cfg_attr(staged_api, stable(feature = "io_safety", since = "1.63.0"))] |
| 121 |
impl FromRawSocket for OwnedSocket { |
| 122 |
#[inline] |
| 123 |
unsafe fn from_raw_socket(socket: RawSocket) -> Self { |
| 124 |
debug_assert_ne!(socket, c::INVALID_SOCKET as RawSocket); |
| 125 |
Self { socket } |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
#[cfg_attr(staged_api, stable(feature = "io_safety", since = "1.63.0"))] |
| 130 |
impl Drop for OwnedSocket { |
| 131 |
#[inline] |
| 132 |
fn drop(&mut self) { |
| 133 |
unsafe { |
| 134 |
let _ = c::closesocket(self.socket as c::SOCKET); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
#[cfg_attr(staged_api, stable(feature = "io_safety", since = "1.63.0"))] |
| 140 |
impl fmt::Debug for BorrowedSocket<'_> { |
| 141 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 142 |
f.debug_struct("BorrowedSocket") |
| 143 |
.field("socket", &self.socket) |
| 144 |
.finish() |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
#[cfg_attr(staged_api, stable(feature = "io_safety", since = "1.63.0"))] |
| 149 |
impl fmt::Debug for OwnedSocket { |
| 150 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 151 |
f.debug_struct("OwnedSocket") |
| 152 |
.field("socket", &self.socket) |
| 153 |
.finish() |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
/// A trait to borrow the socket from an underlying object. |
| 158 |
#[cfg_attr(staged_api, stable(feature = "io_safety", since = "1.63.0"))] |
| 159 |
pub trait AsSocket { |
| 160 |
/// Borrows the socket. |
| 161 |
#[cfg_attr(staged_api, stable(feature = "io_safety", since = "1.63.0"))] |
| 162 |
fn as_socket(&self) -> BorrowedSocket<'_>; |
| 163 |
} |
| 164 |
|
| 165 |
#[cfg_attr(staged_api, stable(feature = "io_safety", since = "1.63.0"))] |
| 166 |
impl<T: AsSocket> AsSocket for &T { |
| 167 |
#[inline] |
| 168 |
fn as_socket(&self) -> BorrowedSocket<'_> { |
| 169 |
T::as_socket(self) |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
#[cfg_attr(staged_api, stable(feature = "io_safety", since = "1.63.0"))] |
| 174 |
impl<T: AsSocket> AsSocket for &mut T { |
| 175 |
#[inline] |
| 176 |
fn as_socket(&self) -> BorrowedSocket<'_> { |
| 177 |
T::as_socket(self) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
#[cfg_attr(staged_api, stable(feature = "io_safety", since = "1.63.0"))] |
| 182 |
impl AsSocket for BorrowedSocket<'_> { |
| 183 |
#[inline] |
| 184 |
fn as_socket(&self) -> BorrowedSocket<'_> { |
| 185 |
*self |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
#[cfg_attr(staged_api, stable(feature = "io_safety", since = "1.63.0"))] |
| 190 |
impl AsSocket for OwnedSocket { |
| 191 |
#[inline] |
| 192 |
fn as_socket(&self) -> BorrowedSocket<'_> { |
| 193 |
// Safety: `OwnedSocket` and `BorrowedSocket` have the same validity |
| 194 |
// invariants, and the `BorrowdSocket` is bounded by the lifetime |
| 195 |
// of `&self`. |
| 196 |
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) } |
| 197 |
} |
| 198 |
} |