placid 0.2.0

Separated ownership and in-place construction in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
use core::{
    fmt,
    mem::{self, ManuallyDrop, MaybeUninit},
    pin::Pin,
};

use crate::{
    init::{Init, InitPin, IntoInit, IntoInitPin},
    owned::Own,
    pin::{DropSlot, DroppingSlot, POwn},
    uninit::Uninit,
};

/// A place slot in memory whose initialization state is tracked at runtime.
///
/// This is similar to [`Option<T>`], but necessary for fallable in-place
/// initialization patterns where `T` does not implement `Default` or `Clone`.
/// For failed initializations, the old value is dropped and the place becomes
/// uninitialized.
///
/// This structure doesn't implement [`Place<T>`] because its initialization
/// state is dynamic and conflicts with the compile-time assertions by
/// `Place<T>`.
///
/// # Examples
///
/// ```rust
/// use placid::{prelude::*, place::DynPlace};
///
/// let mut place: DynPlace<String> = DynPlace::new();
/// assert!(!place.is_init());
///
/// let s = place.insert("Hello".to_string());
/// s.push_str(", world!");
/// assert!(place.is_init());
/// assert_eq!(place.as_deref(), Some("Hello, world!"));
///
/// let s = place.take().unwrap();
/// assert_eq!(s, "Hello, world!");
/// assert!(!place.is_init());
/// ```
///
/// [`Place<T>`]: crate::place::Place
pub struct DynPlace<T> {
    init: bool,
    data: MaybeUninit<T>,
}

impl<T: fmt::Debug> fmt::Debug for DynPlace<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.as_ref() {
            Some(value) => f.debug_tuple("DynPlace").field(value).finish(),
            None => write!(f, "DynPlace(<uninit>)"),
        }
    }
}

impl<T> Default for DynPlace<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T> DynPlace<T> {
    /// Creates a new uninitialized dynamic place.
    #[inline]
    pub const fn new() -> Self {
        Self {
            init: false,
            data: MaybeUninit::uninit(),
        }
    }

    /// Returns `true` if the place is initialized.
    #[inline]
    pub const fn is_init(&self) -> bool {
        self.init
    }

    /// Returns a raw pointer to the value.
    #[inline]
    pub const fn as_ptr(&self) -> *const T {
        self.data.as_ptr()
    }

    /// Returns a mutable raw pointer to the value.
    #[inline]
    pub const fn as_mut_ptr(&mut self) -> *mut T {
        self.data.as_mut_ptr()
    }

    /// Returns a reference to the value if initialized, or `None` otherwise.
    ///
    /// This is similar to [`Option::as_ref`].
    #[inline]
    pub const fn as_ref(&self) -> Option<&T> {
        if self.init {
            // SAFETY: We checked that the value is initialized.
            Some(unsafe { self.data.assume_init_ref() })
        } else {
            None
        }
    }

    /// Returns a mutable reference to the value if initialized, or `None`
    /// otherwise.
    ///
    /// This is similar to [`Option::as_mut`].
    #[inline]
    pub const fn as_mut(&mut self) -> Option<&mut T> {
        if self.init {
            // SAFETY: We checked that the value is initialized.
            Some(unsafe { self.data.assume_init_mut() })
        } else {
            None
        }
    }

    /// Returns an owned reference to the value if initialized, or `None`
    /// otherwise.
    ///
    /// The ownership of the value is transferred to the returned `Own<T>`, and
    /// the place becomes uninitialized even if the returned reference gets
    /// [`mem::forget`]ed.
    #[inline]
    pub const fn as_own(&mut self) -> Option<Own<'_, T>> {
        if self.init {
            self.init = false;
            // SAFETY: We checked that the value is initialized.
            Some(unsafe { Own::from_raw(self.data.as_mut_ptr()) })
        } else {
            None
        }
    }

    /// Returns a pinned reference to the value if initialized, or `None`
    /// otherwise.
    ///
    /// This is similar to [`Option::as_pin_ref`].
    #[inline]
    pub const fn as_pin_ref(self: Pin<&Self>) -> Option<Pin<&T>> {
        unsafe {
            // SAFETY: We don't move `data`.
            let this = self.get_ref();
            if this.init {
                // SAFETY: We checked that the value is initialized.
                Some(Pin::new_unchecked(this.data.assume_init_ref()))
            } else {
                None
            }
        }
    }

    /// Returns a pinned mutable reference to the value if initialized, or
    /// `None` otherwise.
    ///
    /// This is similar to [`Option::as_pin_mut`].
    #[inline]
    pub const fn as_pin_mut(self: Pin<&mut Self>) -> Option<Pin<&mut T>> {
        unsafe {
            // SAFETY: We don't move `data`.
            let this = self.get_unchecked_mut();
            if this.init {
                // SAFETY: We checked that the value is initialized.
                Some(Pin::new_unchecked(this.data.assume_init_mut()))
            } else {
                None
            }
        }
    }

    /// Returns a pinned owned reference to the value if initialized, or
    /// `None` otherwise.
    ///
    /// The ownership of the value is transferred to the returned `POwn<T>`, and
    /// the place becomes uninitialized even if the returned reference gets
    /// [`mem::forget`]ed. The value in the place gets properly dropped, as
    /// guaranteed by `slot`.
    #[inline]
    pub fn as_pin_own<'b>(self: Pin<&mut Self>, slot: DropSlot<'_, 'b, T>) -> Option<POwn<'b, T>> {
        // SAFETY: We don't move `data`.
        let this = unsafe { self.get_unchecked_mut() };
        if this.init {
            this.init = false;
            // SAFETY: We checked that the value is initialized.
            Some(unsafe { POwn::new(Own::from_raw(this.data.as_mut_ptr()), slot) })
        } else {
            None
        }
    }

    /// Returns a reference to the dereferenced value if initialized, or
    /// `None` otherwise.
    ///
    /// This is similar to [`Option::as_deref`].
    #[inline]
    pub fn as_deref(&self) -> Option<&T::Target>
    where
        T: core::ops::Deref,
    {
        self.as_ref().map(|r| r.deref())
    }

    /// Returns a mutable reference to the dereferenced value if initialized,
    /// or `None` otherwise.
    ///
    /// This is similar to [`Option::as_deref_mut`].
    #[inline]
    pub fn as_deref_mut(&mut self) -> Option<&mut T::Target>
    where
        T: core::ops::DerefMut,
    {
        self.as_mut().map(|r| r.deref_mut())
    }

    /// Returns a cloned value if initialized, or `None` otherwise.
    ///
    /// This is similar to [`Option::cloned`].
    #[inline]
    pub fn cloned(&self) -> Option<T>
    where
        T: Clone,
    {
        self.as_ref().cloned()
    }

    /// Returns a copied value if initialized, or `None` otherwise.
    ///
    /// This is similar to [`Option::copied`].
    #[inline]
    pub fn copied(&self) -> Option<T>
    where
        T: Copy,
    {
        self.as_ref().copied()
    }

    /// Takes the value out of the place if initialized, leaving it
    /// uninitialized, and returns it. Returns `None` if the place is
    /// uninitialized.
    ///
    /// This is similar to [`Option::take`].
    #[inline]
    pub const fn take(&mut self) -> Option<T> {
        if self.init {
            // SAFETY: We checked that the value is initialized.
            let value = unsafe { self.data.assume_init_read() };
            self.init = false;
            Some(value)
        } else {
            None
        }
    }
}

impl<T> DynPlace<T> {
    /// Drops the value in place if initialized, leaving the place
    /// uninitialized.
    ///
    /// This is a convenience method of `place = DynPlace::new()` for any
    /// `place: DynPlace<T>`.
    pub fn drop_in_place(&mut self) {
        if self.init {
            // SAFETY: We checked that the value is initialized.
            unsafe { self.data.assume_init_drop() };
            self.init = false;
        }
    }

    /// Drops the value in place if initialized, leaving the place
    /// uninitialized.
    ///
    /// This is a convenience method of `place.set(DynPlace::new())` for any
    /// `place: Pin<&mut DynPlace<T>>`.
    pub fn drop_in_place_pin(self: Pin<&mut Self>) {
        // SAFETY: We don't move `data`.
        unsafe { self.get_unchecked_mut().drop_in_place() }
    }

    /// Initializes the place with the given initializer, returning an owned
    /// reference to the value.
    ///
    /// This will drop any existing value in the place regardless of any panic
    /// during initialization.
    ///
    /// # Panics
    ///
    /// This will panic if the initialization fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use placid::{prelude::*, place::DynPlace};
    ///
    /// let mut place: DynPlace<String> = DynPlace::new();
    /// let mut s = place.init("Hello".to_string());
    /// s.push_str(", world!");
    /// assert_eq!(*s, "Hello, world!");
    /// drop(s);
    /// assert_eq!(place.as_deref(), None);
    /// ```
    #[inline]
    pub fn init<I, M>(&mut self, init: I) -> Own<'_, T>
    where
        I: IntoInit<T, M>,
    {
        self.try_init(init).unwrap()
    }

    /// Tries to initialize the place with the given initializer, returning an
    /// owned reference to the value.
    ///
    /// This will drop any existing value in the place, regardless of any error
    /// during initialization.
    ///
    /// # Errors
    ///
    /// This will return an error if the initialization fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use placid::{prelude::*, place::DynPlace};
    ///
    /// let mut place: DynPlace<String> = DynPlace::new();
    /// match place.try_init("Hello".to_string()) {
    ///     Ok(mut s) => {
    ///         s.push_str(", world!");
    ///         assert_eq!(*s, "Hello, world!");
    ///     }
    ///     Err(_) => panic!("Initialization failed"),
    /// }
    /// assert_eq!(place.as_deref(), None);
    /// ```
    pub fn try_init<I, M>(&mut self, init: I) -> Result<Own<'_, T>, I::Error>
    where
        I: IntoInit<T, M>,
    {
        self.drop_in_place();

        unsafe {
            let uninit = Uninit::from_raw(self.data.as_mut_ptr());
            init.into_init().init(uninit).map_err(|e| e.error)
        }
    }

    /// Initializes the place with the given initializer if uninitialized,
    /// returning an owned reference to the value.
    ///
    /// See also [`DynPlace::init`], which updates the value regardless of the
    /// initialization state.
    ///
    /// # Panics
    ///
    /// This will panic if the initialization fails.
    #[inline]
    pub fn get_or_init<I, M>(&mut self, init: I) -> Own<'_, T>
    where
        I: IntoInit<T, M>,
    {
        if self.is_init() {
            self.init = false;
            // SAFETY: We checked that the value is initialized.
            unsafe { Own::from_raw(self.data.as_mut_ptr()) }
        } else {
            self.init(init)
        }
    }

    /// Initializes the pinned place with the given initializer, returning a
    /// pinned mutable reference to the value.
    ///
    /// This will drop any existing value in the place regardless of any panic
    /// during initialization.
    ///
    /// # Panics
    ///
    /// This will panic if the initialization fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use placid::{prelude::*, place::DynPlace, drop_slot};
    /// use core::pin::pin;
    ///
    /// let mut place = pin!(DynPlace::new());
    /// {
    ///     let slot = drop_slot!();
    ///     let mut s = place.as_mut().init_pin("Hello".to_string(), slot);
    ///     s.push_str(", world!");
    ///     assert_eq!(*s, "Hello, world!");
    /// }
    /// assert_eq!(place.as_deref(), None);
    /// ```
    #[inline]
    pub fn init_pin<'b, I, M>(
        self: Pin<&mut Self>,
        init: I,
        slot: DropSlot<'_, 'b, T>,
    ) -> POwn<'b, T>
    where
        I: IntoInitPin<T, M>,
    {
        self.try_init_pin(init, slot).unwrap()
    }

    /// Tries to initialize the pinned place with the given initializer,
    /// returning a pinned mutable reference to the value.
    ///
    /// This will drop any existing value in the place, regardless of any error
    /// during initialization.
    ///
    /// # Errors
    ///
    /// This will return an error if the initialization fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use placid::{prelude::*, place::DynPlace, drop_slot};
    /// use core::pin::pin;
    ///
    /// let mut place = pin!(DynPlace::new());
    /// let slot = drop_slot!();
    /// match place.as_mut().try_init_pin("Hello".to_string(), slot) {
    ///     Ok(mut s) => {
    ///         s.push_str(", world!");
    ///         assert_eq!(*s, "Hello, world!");
    ///     }
    ///     Err(_) => panic!("Initialization failed"),
    /// }
    /// ```
    pub fn try_init_pin<'b, I, M>(
        mut self: Pin<&mut Self>,
        init: I,
        slot: DropSlot<'_, 'b, T>,
    ) -> Result<POwn<'b, T>, I::Error>
    where
        I: IntoInitPin<T, M>,
    {
        self.as_mut().drop_in_place_pin();

        unsafe {
            let this = self.get_unchecked_mut();
            let uninit = Uninit::from_raw(this.data.as_mut_ptr());
            init.into_init().init_pin(uninit, slot).map_err(|e| e.error)
        }
    }

    /// Initializes the pinned place with the given initializer if
    /// uninitialized, returning a pinned mutable reference to the value.
    ///
    /// See also [`DynPlace::init_pin`], which updates the value regardless
    /// of the initialization state.
    ///
    /// # Panics
    ///
    /// This will panic if the initialization fails.
    #[inline]
    pub fn get_or_init_pin<'b, I, M>(
        self: Pin<&mut Self>,
        init: I,
        slot: DropSlot<'_, 'b, T>,
    ) -> POwn<'b, T>
    where
        I: IntoInitPin<T, M>,
    {
        if self.is_init() {
            // SAFETY: We don't move `data`.
            let this = unsafe { self.get_unchecked_mut() };
            this.init = false;
            // SAFETY: We checked that the value is initialized.
            unsafe { POwn::new(Own::from_raw(this.data.as_mut_ptr()), slot) }
        } else {
            self.init_pin(init, slot)
        }
    }
}

impl<T> DynPlace<T> {
    /// Initializes the place with the given initializer, returning a mutable
    /// reference to the value.
    ///
    /// This is similar to [`Option::insert`], but will drop any existing value
    /// in the place regardless of any panic during initialization.
    ///
    /// # Panics
    ///
    /// This will panic if the initialization fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use placid::{prelude::*, place::DynPlace};
    ///
    /// let mut place: DynPlace<String> = DynPlace::new();
    /// let s = place.insert("Hello".to_string());
    /// s.push_str(", world!");
    /// assert_eq!(place.as_deref(), Some("Hello, world!"));
    /// ```
    #[inline]
    pub fn insert<I, M>(&mut self, init: I) -> &mut T
    where
        I: IntoInit<T, M>,
    {
        self.try_insert(init).unwrap()
    }

    /// Tries to initialize the place with the given initializer, returning a
    /// mutable reference to the value.
    ///
    /// This is similar to [`Option::insert`], but will drop any existing value
    /// in the place, regardless of any error during initialization.
    ///
    /// # Errors
    ///
    /// This will return an error if the initialization fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use placid::{prelude::*, place::DynPlace};
    ///
    /// let mut place: DynPlace<String> = DynPlace::new();
    /// match place.try_insert("Hello".to_string()) {
    ///     Ok(s) => {
    ///         s.push_str(", world!");
    ///         assert_eq!(place.as_deref(), Some("Hello, world!"));
    ///     }
    ///     Err(_) => panic!("Initialization failed"),
    /// }
    /// ```
    pub fn try_insert<I, M>(&mut self, init: I) -> Result<&mut T, I::Error>
    where
        I: IntoInit<T, M>,
    {
        mem::forget(self.try_init(init)?);
        self.init = true;
        // SAFETY: We just initialized the value.
        Ok(unsafe { self.data.assume_init_mut() })
    }

    /// Initializes the place with the given initializer if uninitialized,
    /// returning a mutable reference to the value.
    ///
    /// See also [`DynPlace::insert`], which updates the value regardless of the
    /// initialization state.
    ///
    /// This is similar to [`Option::get_or_insert`].
    ///
    /// # Panics
    ///
    /// This will panic if the initialization fails.
    #[inline]
    pub fn get_or_insert<I, M>(&mut self, init: I) -> &mut T
    where
        I: IntoInit<T, M>,
    {
        if self.is_init() {
            // SAFETY: We checked that the value is initialized.
            unsafe { self.data.assume_init_mut() }
        } else {
            self.insert(init)
        }
    }

    /// Replaces the value in the place with a new initialized value,
    /// returning the old value if initialized.
    ///
    /// This is similar to [`Option::replace`], but will drop any existing
    /// value in the place regardless of any panic during initialization.
    ///
    /// # Panics
    ///
    /// This will panic if the initialization fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use placid::{prelude::*, place::DynPlace};
    ///
    /// let mut place: DynPlace<String> = DynPlace::new();
    /// let old = place.replace("Hello".to_string());
    /// assert!(old.is_none());
    ///
    /// let old = place.replace("World".to_string());
    /// assert_eq!(old, Some("Hello".to_string()));
    ///
    /// assert_eq!(place.as_deref(), Some("World"));
    /// ```
    #[inline]
    pub fn replace<I, M>(&mut self, init: I) -> Option<T>
    where
        I: IntoInit<T, M>,
    {
        self.try_replace(init).unwrap()
    }

    /// Tries to replace the value in the place with a new initialized value,
    /// returning the old value if initialized.
    ///
    /// This is similar to [`Option::replace`], but will drop any existing
    /// value in the place, regardless of any error during initialization.
    ///
    /// # Errors
    ///
    /// This will return an error if the initialization fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use placid::{prelude::*, place::DynPlace};
    ///
    /// let mut place: DynPlace<String> = DynPlace::new();
    /// match place.try_replace("Hello".to_string()) {
    ///     Ok(old) => assert!(old.is_none()),
    ///     Err(_) => panic!("Initialization failed"),
    /// }
    /// ```
    #[inline]
    pub fn try_replace<I, M>(&mut self, init: I) -> Result<Option<T>, I::Error>
    where
        I: IntoInit<T, M>,
    {
        let old = self.take();
        self.try_insert(init)?;
        Ok(old)
    }

    /// Initializes the pinned place with the given initializer, returning a
    /// pinned mutable reference to the value.
    ///
    /// This will drop any existing value in the place regardless of any panic
    /// during initialization.
    ///
    /// # Panics
    ///
    /// This will panic if the initialization fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use placid::{prelude::*, place::DynPlace};
    /// use core::pin::pin;
    ///
    /// let mut place = pin!(DynPlace::new());
    /// let mut s = place.as_mut().insert_pin("Hello".to_string());
    /// s.push_str(", world!");
    /// assert_eq!(place.as_deref(), Some("Hello, world!"));
    /// ```
    #[inline]
    pub fn insert_pin<I, M>(self: Pin<&mut Self>, init: I) -> Pin<&mut T>
    where
        I: IntoInitPin<T, M>,
    {
        self.try_insert_pin(init).unwrap()
    }

    /// Tries to initialize the pinned place with the given initializer,
    /// returning a pinned mutable reference to the value.
    ///
    /// This will drop any existing value in the place, regardless of any error
    /// during initialization.
    ///
    /// # Errors
    ///
    /// This will return an error if the initialization fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use placid::{prelude::*, place::DynPlace};
    /// use core::pin::pin;
    ///
    /// let mut place = pin!(DynPlace::new());
    /// match place.as_mut().try_insert_pin("Hello".to_string()) {
    ///     Ok(mut s) => {
    ///         s.push_str(", world!");
    ///         assert_eq!(place.as_deref(), Some("Hello, world!"));
    ///     }
    ///     Err(_) => panic!("Initialization failed"),
    /// }
    /// ```
    pub fn try_insert_pin<I, M>(mut self: Pin<&mut Self>, init: I) -> Result<Pin<&mut T>, I::Error>
    where
        I: IntoInitPin<T, M>,
    {
        let mut slot = ManuallyDrop::new(DroppingSlot::new());
        // SAFETY: We are not moving the ownership.
        let slot_ref = unsafe { DropSlot::new_unchecked(&mut slot) };
        mem::forget(self.as_mut().try_init_pin(init, slot_ref)?);

        // SAFETY: We don't move `data`.
        let this = unsafe { self.get_unchecked_mut() };
        this.init = true;
        // SAFETY: We just initialized the value.
        Ok(unsafe { Pin::new_unchecked(this.data.assume_init_mut()) })
    }

    /// Initializes the pinned place with the given initializer if
    /// uninitialized, returning a pinned mutable reference to the value.
    ///
    /// See also [`DynPlace::insert_pin`], which updates the value regardless
    /// of the initialization state.
    ///
    /// This is similar to a pinned version of [`Option::get_or_insert`].
    ///
    /// # Panics
    ///
    /// This will panic if the initialization fails.
    #[inline]
    pub fn get_or_insert_pin<I, M>(self: Pin<&mut Self>, init: I) -> Pin<&mut T>
    where
        I: IntoInitPin<T, M>,
    {
        if self.is_init() {
            // SAFETY: We checked that the value is initialized.
            unsafe { Pin::new_unchecked(self.get_unchecked_mut().data.assume_init_mut()) }
        } else {
            self.insert_pin(init)
        }
    }
}

impl<T> Drop for DynPlace<T> {
    fn drop(&mut self) {
        self.drop_in_place();
    }
}