placid 0.1.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
//! Traits and types for working with places in memory.
//!
//! See the [`Place`] trait for more details.

#[cfg(feature = "alloc")]
use alloc::{boxed::Box, rc::Rc, sync::Arc};
#[cfg(feature = "alloc")]
use core::alloc::Allocator;
use core::{
    fmt,
    marker::{CoercePointee, PhantomData},
    mem::{self, ManuallyDrop, MaybeUninit},
    ops::Deref,
    pin::Pin,
    ptr::{self, NonNull},
};

use self::construct::PlaceConstruct;
use crate::{
    init::{IntoInit, IntoInitPin},
    owned::Own,
    pin::{DropSlot, POwn},
    sealed,
    uninit::Uninit,
};

/// A place in memory that can hold an owned value of type `T`.
///
/// This trait makes [place expressions] explicit by providing methods to work
/// directly with the underlying memory location. A `Place` can be used to read
/// from or write to the memory location it represents.
///
/// Common types that can be referred to as places include `MaybeUninit<T>`,
/// arrays of `MaybeUninit<T>`, and smart pointers like `Box<MaybeUninit<T>>`,
/// `Rc<MaybeUninit<T>>`, and `Arc<MaybeUninit<T>>`, which is basically a
/// location in heap memory or on the current call stack.
///
/// # Safety
///
/// Implementors of this trait must ensure that the methods uphold their
/// contracts, particularly regarding the initialization state of the memory.
///
/// Specifically:
///
/// - The `as_mut_ptr` method must return a valid pointer to the memory location
///   of the place.
/// - The `assume_init` method must return the same memory location as an
///   initialized place containing a valid value of type `T`. The validity of
///   the place is the caller's responsibility to uphold.
/// - The `from_init` method must correctly wrap an initialized place containing
///   a valid value of type `T` into `Self`, discarding the explicit
///   initialization state. However, the wrapped value must still be valid
///   inside the place.
///
/// [place expressions]: https://doc.rust-lang.org/stable/reference/expressions.html#place-expressions-and-value-expressions
#[diagnostic::on_unimplemented(
    message = "`{Self}` is not a place for type `{T}`",
    label = "`{Self}` is not a place for type `{T}`"
)]
pub unsafe trait Place<T: ?Sized>: Sized {
    /// The type of the place, but with an explicit initialized state.
    type Init;

    /// Returns a mutable pointer to the memory location of the place.
    fn as_mut_ptr(&mut self) -> *mut T;

    /// Assumes that the place is initialized and returns the initialized place.
    ///
    /// # Safety
    ///
    /// The caller must ensure that the place is indeed initialized before
    /// calling this method. Failing to do so results in undefined behavior.
    /// See [`MaybeUninit::assume_init`] for more details.
    ///
    /// [`MaybeUninit::assume_init`]: core::mem::MaybeUninit::assume_init
    unsafe fn assume_init(self) -> Self::Init;

    /// Discards the explicit initialization state and returns the place.
    fn from_init(init: Self::Init) -> Self;

    /// Initializes the place with the given initializer and returns an owned
    /// reference to the initialized value.
    ///
    /// This method is a convenience method of [`Uninit::from_mut`] +
    /// [`Uninit::write`], allowing direct initialization of a place without
    /// needing to wrap it in an `Uninit` first, which can be directly
    /// type-inferred by the compiler.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use placid::prelude::*;
    /// use core::mem::MaybeUninit;
    ///
    /// let mut place = MaybeUninit::uninit();
    /// let owned = Place::write(&mut place, 42);
    /// assert_eq!(*owned, 42);
    /// ```
    #[inline]
    fn write<'b, M, I>(&'b mut self, init: I) -> Own<'b, T>
    where
        I: IntoInit<T, M, Error: fmt::Debug>,
    {
        Uninit::from_mut(self).write(init)
    }

    /// Initializes the place with the given initializer and returns a pinned
    /// owned reference to the initialized value.
    ///
    /// This method is a convenience method of [`Uninit::from_mut`] +
    /// [`Uninit::write_pin`], allowing direct pinned initialization of a place
    /// without needing to wrap it in an `Uninit` first, which can be directly
    /// type-inferred by the compiler.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use placid::prelude::*;
    /// use core::mem::MaybeUninit;
    ///
    /// let mut place = MaybeUninit::uninit();
    /// let drop_slot = placid::drop_slot!();
    /// let owned = Place::write_pin(&mut place, 42, drop_slot);
    /// assert_eq!(*owned, 42);
    /// ```
    #[inline]
    fn write_pin<'a, 'b, M, I>(&'a mut self, init: I, slot: DropSlot<'a, 'b, T>) -> POwn<'b, T>
    where
        I: IntoInitPin<T, M, Error: fmt::Debug>,
    {
        Uninit::from_mut(self).write_pin(init, slot)
    }

    /// Initializes the place with the given initializer and returns the same
    /// place with an initialized state.
    ///
    /// This method is similar to [`Uninit::write`], but instead of returning an
    /// owned reference, it returns the place itself with an initialized state.
    ///
    /// # Panics
    ///
    /// This method will panic if the initialization fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use placid::prelude::*;
    ///
    /// let p = Box::<i32>::new_uninit().init(|| 42);
    /// assert_eq!(*p, 42);
    /// ```
    #[inline]
    fn init<M, I, E>(self, init: I) -> Self::Init
    where
        I: IntoInit<T, M, Error = E>,
        E: fmt::Debug,
    {
        self.try_init(init).map_err(|(e, _)| e).unwrap()
    }

    /// Tries to initialize the place with the given initializer and returns
    /// the same place with an initialized state.
    ///
    /// This method is similar to [`Uninit::try_write`], but instead of
    /// returning an owned reference, it returns the place itself with an
    /// initialized state.
    ///
    /// # Errors
    ///
    /// If the initialization fails, this method returns a tuple containing the
    /// error and the original place.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use placid::prelude::*;
    ///
    /// let p = Box::<i32>::new_uninit();
    /// let result = p.try_init(|| Ok::<_, &str>(42));
    /// assert!(result.is_ok());
    /// assert_eq!(*result.unwrap(), 42);
    ///
    /// // With a failing initializer
    /// let p = Box::<i32>::new_uninit();
    /// let result = p.try_init(|| Err::<i32, &str>("failed"));
    /// assert!(result.is_err());
    /// ```
    #[inline]
    fn try_init<M, I, E>(mut self, init: I) -> Result<Self::Init, (E, Self)>
    where
        I: IntoInit<T, M, Error = E>,
    {
        'ok: {
            let err = match Uninit::from_mut(&mut self).try_write(init) {
                Ok(own) => break 'ok mem::forget(own),
                Err(err) => (mem::forget(err.place), err.error).1,
            };
            return Err((err, self));
        }
        // SAFETY: The place is now initialized, and `own` is forgotten so that the
        // destructor is not run.
        Ok(unsafe { self.assume_init() })
    }

    /// Initializes the place with the given initializer and returns the same
    /// place with a pinned initialized state.
    ///
    /// This method is similar to [`Uninit::write_pin`], but instead of
    /// returning a pinned owned reference, it returns the place itself with
    /// a pinned initialized state.
    ///
    /// # Panics
    ///
    /// This method will panic if the initialization fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use placid::prelude::*;
    ///
    /// let place = Box::<i32>::new_uninit().init_pin(|| 42);
    /// assert_eq!(*place, 42);
    /// ```
    #[inline]
    fn init_pin<M, I, E>(self, init: I) -> Pin<Self::Init>
    where
        I: IntoInitPin<T, M, Error = E>,
        Self::Init: Deref,
        E: fmt::Debug,
    {
        self.try_init_pin(init).map_err(|(e, _)| e).unwrap()
    }

    /// Tries to initialize the place with the given initializer and returns the
    /// same place with a pinned initialized state.
    ///
    /// This method is similar to [`Uninit::try_write_pin`], but instead of
    /// returning a pinned owned reference, it returns the place itself with a
    /// pinned initialized state.
    ///
    /// # Errors
    ///
    /// If the initialization fails, this method returns a tuple containing
    /// the error and the original place.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use placid::prelude::*;
    ///
    /// let place = Box::<i32>::new_uninit();
    /// let result = place.try_init_pin(|| Ok::<_, &str>(42));
    /// assert!(result.is_ok());
    /// assert_eq!(*result.unwrap(), 42);
    ///
    /// let place = Box::<i32>::new_uninit();
    /// let result = place.try_init_pin(|| Err::<i32, &str>("failed"));
    /// assert!(result.is_err());
    /// ```
    #[inline]
    fn try_init_pin<M, I, E>(mut self, init: I) -> Result<Pin<Self::Init>, (E, Self)>
    where
        I: IntoInitPin<T, M, Error = E>,
        Self::Init: Deref,
    {
        'ok: {
            let mut slot = ManuallyDrop::new(crate::pin::DroppingSlot::new());
            // SAFETY:
            // - We actually forget `slot` since the lifetime of the object returns back to
            //   the place itself instead of `POwn`.
            let sr = unsafe { crate::pin::DropSlot::new_unchecked(&mut slot) };
            let err = match Uninit::from_mut(&mut self).try_write_pin(init, sr) {
                Ok(own) => break 'ok mem::forget(own),
                Err(err) => (mem::forget((err.place, err.slot)), err.error).1,
            };
            return Err((err, self));
        }
        // SAFETY: The place is now initialized and logically pinned, and `own` & `slot`
        // are forgotten so that the destructor is not run.
        Ok(unsafe { Pin::new_unchecked(self.assume_init()) })
    }
}

pub mod construct;

unsafe impl<T> Place<T> for MaybeUninit<T> {
    type Init = T;

    #[inline]
    fn as_mut_ptr(&mut self) -> *mut T {
        self.as_mut_ptr()
    }

    #[inline]
    unsafe fn assume_init(self) -> Self::Init {
        unsafe { self.assume_init() }
    }

    #[inline]
    fn from_init(init: Self::Init) -> Self {
        Self::new(init)
    }
}

unsafe impl<const N: usize> Place<str> for MaybeUninit<[u8; N]> {
    type Init = [u8; N];

    #[inline]
    fn as_mut_ptr(&mut self) -> *mut str {
        let ptr = self.as_mut_ptr().cast::<u8>();
        ptr::from_raw_parts_mut(ptr, N)
    }

    #[inline]
    unsafe fn assume_init(self) -> Self::Init {
        unsafe { self.assume_init() }
    }

    #[inline]
    fn from_init(init: Self::Init) -> Self {
        MaybeUninit::new(init)
    }
}

unsafe impl<T, U: ?Sized, const N: usize> Place<U> for [MaybeUninit<T>; N]
where
    MaybeUninit<[T; N]>: Place<U>,
{
    type Init = [T; N];

    #[inline]
    fn as_mut_ptr(&mut self) -> *mut U {
        // SAFETY: [MaybeUninit<T>; N] and MaybeUninit<[T; N]> have the same memory
        // layout and validity value ranges.
        let r =
            unsafe { mem::transmute::<&mut [MaybeUninit<T>; N], &mut MaybeUninit<[T; N]>>(self) };
        Place::as_mut_ptr(r)
    }

    #[inline]
    unsafe fn assume_init(self) -> Self::Init {
        unsafe { self.map(|p| MaybeUninit::assume_init(p)) }
    }

    #[inline]
    fn from_init(init: Self::Init) -> Self {
        init.map(MaybeUninit::new)
    }
}

#[cfg(feature = "alloc")]
macro_rules! std_alloc_places {
    ($($ty:ident: [$($mut:ident)?; $($try_slice:ident)?]),* $(,)?) => {
        $(std_alloc_places!(@IMP $ty: [$($mut)?; $($try_slice)?]);)*
    };
    (@IMP $ty:ident: [$($mut:ident)?; $($try_slice:ident)?]) => {
        unsafe impl<T, A: Allocator> Place<T> for $ty<MaybeUninit<T>, A> {
            type Init = $ty<T, A>;

            #[inline]
            fn as_mut_ptr(&mut self) -> *mut T {
                std_alloc_places!(@get_mut self, $($mut)? $ty).as_mut_ptr()
            }

            #[inline]
            unsafe fn assume_init(self) -> Self::Init {
                unsafe { self.assume_init() }
            }

            #[inline]
            fn from_init(init: Self::Init) -> Self {
                let (raw, alloc) = $ty::into_raw_with_allocator(init);
                unsafe { $ty::from_raw_in(raw.cast::<MaybeUninit<T>>(), alloc) }
            }
        }

        impl<T> PlaceConstruct<()> for $ty<T> {
            type Uninit = $ty<MaybeUninit<T>>;
            type Error = alloc::alloc::AllocError;

            #[inline]
            fn try_new_uninit_in(_: ()) -> Result<Self::Uninit, Self::Error> {
                $ty::try_new_uninit()
            }
        }

        impl<T, A: Allocator> PlaceConstruct<A> for $ty<T, A> {
            type Uninit = $ty<MaybeUninit<T>, A>;
            type Error = alloc::alloc::AllocError;

            #[inline]
            fn try_new_uninit_in(alloc: A) -> Result<Self::Uninit, Self::Error> {
                $ty::try_new_uninit_in(alloc)
            }
        }

        unsafe impl<T, A: Allocator> Place<[T]> for $ty<[MaybeUninit<T>], A> {
            type Init = $ty<[T], A>;

            #[inline]
            fn as_mut_ptr(&mut self) -> *mut [T] {
                let len = self.len();
                let ptr = std_alloc_places!(@get_mut self, $($mut)? $ty).as_mut_ptr();
                ptr::from_raw_parts_mut(ptr.cast::<T>(), len)
            }

            #[inline]
            unsafe fn assume_init(self) -> Self::Init {
                unsafe { self.assume_init() }
            }

            #[inline]
            fn from_init(init: Self::Init) -> Self {
                let len = init.len();
                let (raw, alloc) = $ty::into_raw_with_allocator(init);
                let ptr = std_alloc_places!(@from_raw_parts $($mut)? (
                    raw.cast::<MaybeUninit<T>>(),
                    len,
                ));
                unsafe { $ty::from_raw_in(ptr, alloc) }
            }
        }

        unsafe impl<A: Allocator> Place<str> for $ty<[MaybeUninit<u8>], A> {
            type Init = $ty<str, A>;

            #[inline]
            fn as_mut_ptr(&mut self) -> *mut str {
                let len = self.len();
                let ptr = std_alloc_places!(@get_mut self, $($mut)? $ty).as_mut_ptr();
                ptr::from_raw_parts_mut(ptr.cast::<u8>(), len)
            }

            #[inline]
            unsafe fn assume_init(self) -> Self::Init {
                unsafe {
                    let (raw, alloc) = $ty::into_raw_with_allocator(self.assume_init());
                    let ptr = std_alloc_places!(@from_raw_parts $($mut)? (
                        raw.cast::<u8>(),
                        raw.len(),
                    ));
                    $ty::from_raw_in(ptr, alloc)
                }
            }

            #[inline]
            fn from_init(init: Self::Init) -> Self {
                let len = init.len();
                let (raw, alloc) = $ty::into_raw_with_allocator(init);
                let ptr = std_alloc_places!(@from_raw_parts $($mut)? (
                    raw.cast::<MaybeUninit<u8>>(),
                    len,
                ));
                unsafe { $ty::from_raw_in(ptr, alloc) }
            }
        }

        std_alloc_places!(@TRY_SLICE $($try_slice)? $ty);
    };
    (@get_mut $this:ident, $ty:ident) => {
        $ty::get_mut($this).unwrap()
    };
    (@get_mut $this:ident, mut $ty:ident) => {
        **$this
    };
    (@from_raw_parts ($($t:tt)*)) => {
        ptr::from_raw_parts($($t)*)
    };
    (@from_raw_parts mut ($($t:tt)*)) => {
        ptr::from_raw_parts_mut($($t)*)
    };
    (@TRY_SLICE try_slice $ty:ident) => {
        impl<T> PlaceConstruct<usize> for $ty<[T]> {
            type Uninit = $ty<[MaybeUninit<T>]>;
            type Error = alloc::alloc::AllocError;

            #[inline]
            fn try_new_uninit_in(len: usize) -> Result<Self::Uninit, Self::Error> {
                $ty::try_new_uninit_slice(len)
            }
        }

        impl<T, A: Allocator> PlaceConstruct<(usize, A)> for $ty<[T], A> {
            type Uninit = $ty<[MaybeUninit<T>], A>;
            type Error = alloc::alloc::AllocError;

            #[inline]
            fn try_new_uninit_in((len, alloc): (usize, A)) -> Result<Self::Uninit, Self::Error> {
                $ty::try_new_uninit_slice_in(len, alloc)
            }
        }

        impl PlaceConstruct<usize> for $ty<str> {
            type Uninit = $ty<[MaybeUninit<u8>]>;
            type Error = alloc::alloc::AllocError;

            #[inline]
            fn try_new_uninit_in(len: usize) -> Result<Self::Uninit, Self::Error> {
                $ty::try_new_uninit_slice(len)
            }
        }

        impl<A: Allocator> PlaceConstruct<(usize, A)> for $ty<str, A> {
            type Uninit = $ty<[MaybeUninit<u8>], A>;
            type Error = alloc::alloc::AllocError;

            #[inline]
            fn try_new_uninit_in((len, alloc): (usize, A)) -> Result<Self::Uninit, Self::Error> {
                $ty::try_new_uninit_slice_in(len, alloc)
            }
        }
    };
    (@TRY_SLICE $ty:ident) => {
        impl<T> PlaceConstruct<usize> for $ty<[T]> {
            type Uninit = $ty<[MaybeUninit<T>]>;
            type Error = alloc::alloc::AllocError;

            #[inline]
            fn try_new_uninit_in(len: usize) -> Result<Self::Uninit, Self::Error> {
                Ok($ty::new_uninit_slice(len))
            }
        }

        impl<T, A: Allocator> PlaceConstruct<(usize, A)> for $ty<[T], A> {
            type Uninit = $ty<[MaybeUninit<T>], A>;
            type Error = alloc::alloc::AllocError;

            #[inline]
            fn try_new_uninit_in((len, alloc): (usize, A)) -> Result<Self::Uninit, Self::Error> {
                Ok($ty::new_uninit_slice_in(len, alloc))
            }
        }

        impl PlaceConstruct<usize> for $ty<str> {
            type Uninit = $ty<[MaybeUninit<u8>]>;
            type Error = alloc::alloc::AllocError;

            #[inline]
            fn try_new_uninit_in(len: usize) -> Result<Self::Uninit, Self::Error> {
                Ok($ty::new_uninit_slice(len))
            }
        }

        impl<A: Allocator> PlaceConstruct<(usize, A)> for $ty<str, A> {
            type Uninit = $ty<[MaybeUninit<u8>], A>;
            type Error = alloc::alloc::AllocError;

            #[inline]
            fn try_new_uninit_in((len, alloc): (usize, A)) -> Result<Self::Uninit, Self::Error> {
                Ok($ty::new_uninit_slice_in(len, alloc))
            }
        }
    }
}

#[cfg(feature = "alloc")]
std_alloc_places! {
    Box: [mut; ],
    Rc: [;],
    Arc: [;],
}

/// A place state marker for owned places.
pub enum Owned {}
/// A place state marker for uninitialized places.
pub enum Uninitialized {}

/// A place state marker.
pub trait PlaceState: sealed::Sealed {
    #[doc(hidden)]
    #[allow(private_interfaces)]
    unsafe fn drop<T: ?Sized>(inner: *mut T);
}

impl sealed::Sealed for Owned {}
impl PlaceState for Owned {
    #[allow(private_interfaces)]
    #[inline]
    unsafe fn drop<T: ?Sized>(inner: *mut T) {
        // SAFETY: The value is owned, so it is initialized.
        unsafe { inner.drop_in_place() };
    }
}

impl sealed::Sealed for Uninitialized {}
impl PlaceState for Uninitialized {
    #[allow(private_interfaces)]
    #[inline]
    unsafe fn drop<T: ?Sized>(_inner: *mut T) {
        // No-op, as the value is uninitialized.
    }
}

/// A reference to a place in memory that can hold an owned value of type `T`.
///
/// `PlaceRef` can be in one of two states: [`Owned`] or [`Uninitialized`]. An
/// `Owned` place contains a fully initialized value of type `T`, while an
/// `Uninitialized` place is a location in memory that is reserved for a value
/// of type `T` but has not yet been initialized. The state of the place is
/// tracked at the type level using the `PlaceState` trait.
///
/// Users may find it easier to work with the type aliases [`Own`] and
/// [`Uninit`] instead of using `PlaceRef` directly.
#[derive(CoercePointee)]
#[repr(transparent)]
pub struct PlaceRef<'a, #[pointee] T: ?Sized, State: PlaceState> {
    pub(crate) inner: NonNull<T>,
    state: PhantomData<(State, &'a mut MaybeUninit<PhantomData<T>>)>,
    // See the drop implementation for details on why this is needed.
    owns_value: PhantomData<T>,
}

// General PlaceRef implementations

// SAFETY: PlaceRef is Send if T is Send.
unsafe impl<'a, T: ?Sized + Send, S: PlaceState> Send for PlaceRef<'a, T, S> {}
// SAFETY: PlaceRef is Sync if T is Sync.
unsafe impl<'a, T: ?Sized + Sync, S: PlaceState> Sync for PlaceRef<'a, T, S> {}

impl<'a, T: ?Sized, S: PlaceState> Unpin for PlaceRef<'a, T, S> {}

impl<'a, T: ?Sized, S: PlaceState> PlaceRef<'a, T, S> {
    #[inline]
    pub(crate) const unsafe fn from_inner(inner: NonNull<T>) -> Self {
        PlaceRef {
            inner,
            state: PhantomData,
            owns_value: PhantomData,
        }
    }
}

// SAFETY: We have `owns_value: PhantomData<T>`, which tells the dropck that we
// own a value of type T.
unsafe impl<'a, #[may_dangle] T: ?Sized, S: PlaceState> Drop for PlaceRef<'a, T, S> {
    #[inline]
    fn drop(&mut self) {
        // SAFETY: We are dropping the place, so we need to drop the value if it is
        // initialized.
        unsafe { S::drop::<T>(self.inner.as_ptr()) };
    }
}

#[cfg(test)]
#[cfg(feature = "alloc")]
mod tests {
    use super::*;
    use crate::{init, place::construct::PlaceStr};

    #[test]
    fn places() {
        let b = Box::new_str(5, init::str("hello"));
        assert_eq!(&*b, "hello");

        let (e, _) = Box::new_uninit_slice(7)
            .try_init(init::str("world"))
            .unwrap_err();
        assert_eq!(e, init::SliceError);
    }
}