| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 |
|
| 8 | use super::*;
|
| 9 |
|
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | #[repr(transparent)]
|
| 20 | pub struct PhantomInvariant<T: ?Sized>(PhantomData<fn(T) -> T>);
|
| 21 |
|
| 22 | impl<T: ?Sized> Clone for PhantomInvariant<T> {
|
| 23 | #[inline(always)]
|
| 24 | fn clone(&self) -> Self {
|
| 25 | *self
|
| 26 | }
|
| 27 | }
|
| 28 |
|
| 29 | impl<T: ?Sized> Copy for PhantomInvariant<T> {}
|
| 30 |
|
| 31 | impl<T: ?Sized> Default for PhantomInvariant<T> {
|
| 32 | #[inline(always)]
|
| 33 | fn default() -> Self {
|
| 34 | Self::new()
|
| 35 | }
|
| 36 | }
|
| 37 |
|
| 38 | impl<T: ?Sized> PhantomInvariant<T> {
|
| 39 | #[inline(always)]
|
| 40 | pub const fn new() -> Self {
|
| 41 | Self(PhantomData)
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | #[repr(transparent)]
|
| 51 | #[derive(Clone, Copy, Default)]
|
| 52 | pub struct PhantomInvariantLifetime<'a>(PhantomInvariant<&'a ()>);
|
| 53 |
|
| 54 | impl PhantomInvariantLifetime<'_> {
|
| 55 | #[inline(always)]
|
| 56 | pub const fn new() -> Self {
|
| 57 | Self(PhantomInvariant::new())
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | pub struct InitOk(());
|
| 66 |
|
| 67 | impl InitOk {
|
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | #[inline(always)]
|
| 74 | pub unsafe fn new() -> Self {
|
| 75 | Self(())
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | pub unsafe trait HasPinData {
|
| 86 | type PinData;
|
| 87 |
|
| 88 | #[expect(clippy::missing_safety_doc)]
|
| 89 | unsafe fn __pin_data() -> Self::PinData;
|
| 90 | }
|
| 91 |
|
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | pub unsafe trait HasInitData {
|
| 99 | type InitData;
|
| 100 |
|
| 101 | #[expect(clippy::missing_safety_doc)]
|
| 102 | unsafe fn __init_data() -> Self::InitData;
|
| 103 | }
|
| 104 |
|
| 105 | pub struct AllData<T: ?Sized>(PhantomInvariant<T>);
|
| 106 |
|
| 107 | impl<T: ?Sized> Clone for AllData<T> {
|
| 108 | #[inline]
|
| 109 | fn clone(&self) -> Self {
|
| 110 | *self
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | impl<T: ?Sized> Copy for AllData<T> {}
|
| 115 |
|
| 116 | impl<T: ?Sized> AllData<T> {
|
| 117 | |
| 118 | #[inline(always)]
|
| 119 | pub fn __make_closure<F, E>(self, f: F) -> F
|
| 120 | where
|
| 121 | F: FnOnce(*mut T) -> Result<InitOk, E>,
|
| 122 | {
|
| 123 | f
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | |
| 128 | unsafe impl<T: ?Sized> HasInitData for T {
|
| 129 | type InitData = AllData<T>;
|
| 130 |
|
| 131 | #[inline]
|
| 132 | unsafe fn __init_data() -> Self::InitData {
|
| 133 | AllData(PhantomInvariant::new())
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | pub struct StackInit<T> {
|
| 145 | value: MaybeUninit<T>,
|
| 146 | is_init: bool,
|
| 147 | }
|
| 148 |
|
| 149 | impl<T> Drop for StackInit<T> {
|
| 150 | #[inline]
|
| 151 | fn drop(&mut self) {
|
| 152 | if self.is_init {
|
| 153 | |
| 154 | |
| 155 | unsafe { self.value.assume_init_drop() };
|
| 156 | }
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | impl<T> StackInit<T> {
|
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | #[inline]
|
| 166 | pub fn uninit() -> Self {
|
| 167 | Self {
|
| 168 | value: MaybeUninit::uninit(),
|
| 169 | is_init: false,
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | |
| 174 | #[inline]
|
| 175 | pub fn init<E>(self: Pin<&mut Self>, init: impl PinInit<T, E>) -> Result<Pin<&mut T>, E> {
|
| 176 | |
| 177 | let this = unsafe { Pin::into_inner_unchecked(self) };
|
| 178 | |
| 179 | |
| 180 | if this.is_init {
|
| 181 | this.is_init = false;
|
| 182 | |
| 183 | unsafe { this.value.assume_init_drop() };
|
| 184 | }
|
| 185 | |
| 186 | unsafe { init.__init(this.value.as_mut_ptr())? };
|
| 187 | |
| 188 | this.is_init = true;
|
| 189 | |
| 190 | Ok(unsafe { Pin::new_unchecked(this.value.assume_init_mut()) })
|
| 191 | }
|
| 192 | }
|
| 193 |
|
| 194 | #[test]
|
| 195 | #[cfg(feature = "std")]
|
| 196 | fn stack_init_reuse() {
|
| 197 | use ::std::{borrow::ToOwned, println, string::String};
|
| 198 | use core::pin::pin;
|
| 199 |
|
| 200 | #[derive(Debug)]
|
| 201 | struct Foo {
|
| 202 | a: usize,
|
| 203 | b: String,
|
| 204 | }
|
| 205 | let mut slot: Pin<&mut StackInit<Foo>> = pin!(StackInit::uninit());
|
| 206 | let value: Result<Pin<&mut Foo>, core::convert::Infallible> =
|
| 207 | slot.as_mut().init(crate::init!(Foo {
|
| 208 | a: 42,
|
| 209 | b: "Hello".to_owned(),
|
| 210 | }));
|
| 211 | let value = value.unwrap();
|
| 212 | println!("{value:?}");
|
| 213 | let value: Result<Pin<&mut Foo>, core::convert::Infallible> =
|
| 214 | slot.as_mut().init(crate::init!(Foo {
|
| 215 | a: 24,
|
| 216 | b: "world!".to_owned(),
|
| 217 | }));
|
| 218 | let value = value.unwrap();
|
| 219 | println!("{value:?}");
|
| 220 | }
|
| 221 |
|
| 222 | |
| 223 | pub struct Pinned;
|
| 224 | pub struct Unpinned;
|
| 225 |
|
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | pub struct Slot<P, T: ?Sized> {
|
| 233 | ptr: *mut T,
|
| 234 | _phantom: PhantomData<P>,
|
| 235 | }
|
| 236 |
|
| 237 | impl<P, T: ?Sized> Slot<P, T> {
|
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | #[inline(always)]
|
| 244 | pub unsafe fn new(ptr: *mut T) -> Self {
|
| 245 | |
| 246 | Self {
|
| 247 | ptr,
|
| 248 | _phantom: PhantomData,
|
| 249 | }
|
| 250 | }
|
| 251 |
|
| 252 | |
| 253 | #[inline(always)]
|
| 254 | pub fn write(self, value: T) -> DropGuard<P, T>
|
| 255 | where
|
| 256 | T: Sized,
|
| 257 | {
|
| 258 | |
| 259 | unsafe { self.ptr.write(value) }
|
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | unsafe { DropGuard::new(self.ptr) }
|
| 265 | }
|
| 266 | }
|
| 267 |
|
| 268 | impl<T: ?Sized> Slot<Unpinned, T> {
|
| 269 | |
| 270 | #[inline(always)]
|
| 271 | pub fn init<E>(self, init: impl Init<T, E>) -> Result<DropGuard<Unpinned, T>, E> {
|
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | unsafe { init.__init(self.ptr)? };
|
| 277 |
|
| 278 | |
| 279 | |
| 280 | |
| 281 | Ok(unsafe { DropGuard::new(self.ptr) })
|
| 282 | }
|
| 283 | }
|
| 284 |
|
| 285 | impl<T: ?Sized> Slot<Pinned, T> {
|
| 286 | |
| 287 | #[inline(always)]
|
| 288 | pub fn init<E>(self, init: impl PinInit<T, E>) -> Result<DropGuard<Pinned, T>, E> {
|
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | unsafe { init.__init(self.ptr)? };
|
| 295 |
|
| 296 | |
| 297 | |
| 298 | |
| 299 | Ok(unsafe { DropGuard::new(self.ptr) })
|
| 300 | }
|
| 301 | }
|
| 302 |
|
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | pub struct DropGuard<P, T: ?Sized> {
|
| 313 | ptr: *mut T,
|
| 314 | phantom: PhantomData<P>,
|
| 315 | }
|
| 316 |
|
| 317 | impl<P, T: ?Sized> DropGuard<P, T> {
|
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | #[inline]
|
| 328 | pub unsafe fn new(ptr: *mut T) -> Self {
|
| 329 | |
| 330 | Self {
|
| 331 | ptr,
|
| 332 | phantom: PhantomData,
|
| 333 | }
|
| 334 | }
|
| 335 | }
|
| 336 |
|
| 337 | impl<T: ?Sized> DropGuard<Unpinned, T> {
|
| 338 | |
| 339 | #[inline]
|
| 340 | pub fn let_binding(&mut self) -> &mut T {
|
| 341 | |
| 342 | unsafe { &mut *self.ptr }
|
| 343 | }
|
| 344 | }
|
| 345 |
|
| 346 | impl<T: ?Sized> DropGuard<Pinned, T> {
|
| 347 | |
| 348 | #[inline]
|
| 349 | pub fn let_binding(&mut self) -> Pin<&mut T> {
|
| 350 | |
| 351 | |
| 352 | unsafe { Pin::new_unchecked(&mut *self.ptr) }
|
| 353 | }
|
| 354 | }
|
| 355 |
|
| 356 | impl<P, T: ?Sized> Drop for DropGuard<P, T> {
|
| 357 | #[inline]
|
| 358 | fn drop(&mut self) {
|
| 359 | |
| 360 | unsafe { ptr::drop_in_place(self.ptr) }
|
| 361 | }
|
| 362 | }
|
| 363 |
|
| 364 | |
| 365 | |
| 366 | |
| 367 | pub struct OnlyCallFromDrop(());
|
| 368 |
|
| 369 | impl OnlyCallFromDrop {
|
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | pub unsafe fn new() -> Self {
|
| 375 | Self(())
|
| 376 | }
|
| 377 | }
|
| 378 |
|
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | pub struct AlwaysFail<T: ?Sized> {
|
| 385 | _t: PhantomData<T>,
|
| 386 | }
|
| 387 |
|
| 388 | impl<T: ?Sized> AlwaysFail<T> {
|
| 389 | |
| 390 | #[inline]
|
| 391 | pub fn new() -> Self {
|
| 392 | Self { _t: PhantomData }
|
| 393 | }
|
| 394 | }
|
| 395 |
|
| 396 | impl<T: ?Sized> Default for AlwaysFail<T> {
|
| 397 | #[inline]
|
| 398 | fn default() -> Self {
|
| 399 | Self::new()
|
| 400 | }
|
| 401 | }
|
| 402 |
|
| 403 | |
| 404 | unsafe impl<T: ?Sized> PinInit<T, ()> for AlwaysFail<T> {
|
| 405 | #[inline]
|
| 406 | unsafe fn __init(self, _slot: *mut T) -> Result<(), ()> {
|
| 407 | Err(())
|
| 408 | }
|
| 409 | }
|