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
use_prelude!();

use crate::extension_traits::MaybeUninitExt;
use core::mem::MaybeUninit;

/// Converts a reference into its maybe-initialized form.
///
/// This trait allows you to use a unified API for `MaybeUninit`
/// on sized and unsized types.
///
/// You probably don't need to implement this trait yourself:
/// it is automatically implemented for all `T: Sized` and `[T]`.
///
/// # Safety
/// - `Uninit` must have the same layout as `Self` but with no requirement on its contents.
///   See the [`MaybeUninit` layout](https://doc.rust-lang.org/std/mem/union.MaybeUninit.html#layout-1).
/// - All methods return pointers that point to the same memory as their input.
pub unsafe trait AsMaybeUninit {
    /// This type in its maybe-uninitialized form.
    type Uninit: ?Sized + MaybeUninitExt<T = Self>;

    /// The largest `Sized` element in `Self`, used to check for the absence
    /// of [drop glue][core::mem::needs_drop] via a `Copy` bound.
    ///
    /// For `Self: Sized`, this equals `Self`, but for `[T]` it is `T`.
    type SizedPart: Sized;

    /// Converts a `&self` to its maybe-initialized equivalent.
    fn as_ref_uninit(&self) -> &Self::Uninit;

    /// Converts a `&mut T` to its maybe-initialized equivalent.
    ///
    /// This converts a read-write-valid-values-only reference to a write-anything reference.
    ///
    /// # Safety
    ///
    /// The same requirements as [`Out::as_mut_uninit`].
    /// Care should be taken with usage of the output - uninitialized garbage must not be written.
    ///
    /// [`Out::as_mut_uninit`]: crate::out_ref::Out::as_mut_uninit
    unsafe fn as_mut_uninit(&mut self) -> &mut Self::Uninit;

    /// Converts a raw pointer to a reference to maybe-uninit.
    ///
    /// # Safety
    ///
    /// This has the same requirements as `&*(raw as *const Self::Uninit)`.
    ///
    /// - `raw` must point to a readable allocated memory for `'a` for the size of `T`
    /// - `raw` must be aligned for `T`
    unsafe fn raw_as_uninit<'a>(raw: *const Self) -> &'a Self::Uninit;

    /// Converts a raw mutable pointer to a mutable reference to maybe-uninit.
    ///
    /// # Safety
    ///
    /// This has the same requirements as `&mut *(raw as *mut Self::Uninit)`.
    ///
    /// - `raw` must point to a readable and writeable allocated object for `'a` for the size of `T`
    /// - The pointer must not alias any other mutable references for `'a`
    /// - `raw` must be aligned for `T`
    unsafe fn raw_mut_as_uninit<'a>(raw: *mut Self) -> &'a mut Self::Uninit;
}

// SAFETY:
// - `MaybeUninit<T>` has the same layout as `T` but with no requirements on its contents.
// - `as_ref_uninit` and `as_mut_uninit` return the same address as their input.
unsafe impl<T> AsMaybeUninit for T {
    type Uninit = MaybeUninit<T>;
    type SizedPart = T;

    #[inline]
    fn as_ref_uninit(&self) -> &Self::Uninit {
        unsafe { &*(self as *const T).cast() }
    }

    #[inline]
    unsafe fn as_mut_uninit(&mut self) -> &mut Self::Uninit {
        // SAFETY: Uninit will not be written to the output as promised by the caller.
        unsafe { &mut *(self as *mut T).cast() }
    }

    #[inline]
    unsafe fn raw_as_uninit<'a>(raw: *const Self) -> &'a Self::Uninit {
        // SAFETY: The pointer is aligned and readable for `'a` as promised by the caller.
        unsafe { &*(raw as *const MaybeUninit<T>) }
    }

    #[inline]
    unsafe fn raw_mut_as_uninit<'a>(raw: *mut Self) -> &'a mut Self::Uninit {
        // SAFETY: The pointer is aligned and read-writeable for `'a` as promised by the caller.
        unsafe { &mut *(raw as *mut MaybeUninit<T>) }
    }
}

// Unfortunately this cannot `impl<T: ?Sized> AsMaybeUninit for ManuallyDrop<T>` as
// that would conflict with the `Sized` blanket impl.

// SAFETY:
// - `[MaybeUninit<T>]` has the same layout as `[T]` but with no requirements on its contents.
// - `as_ref_uninit` and `as_mut_uninit` return the same address as their input.
unsafe impl<T> AsMaybeUninit for [T] {
    type Uninit = [MaybeUninit<T>];
    type SizedPart = T;

    #[inline]
    fn as_ref_uninit(&self) -> &Self::Uninit {
        unsafe { &*(self as *const [T] as *const [MaybeUninit<T>]) }
    }

    #[inline]
    unsafe fn as_mut_uninit(&mut self) -> &mut Self::Uninit {
        // SAFETY: Uninit will not be written to the output as promised by the caller.
        unsafe { &mut *(self as *mut [T] as *mut [MaybeUninit<T>]) }
    }

    #[inline]
    unsafe fn raw_as_uninit<'a>(raw: *const Self) -> &'a Self::Uninit {
        // SAFETY: The pointer is aligned and readable for `'a` as promised by the caller.
        unsafe { &*(raw as *const [MaybeUninit<T>]) }
    }

    #[inline]
    unsafe fn raw_mut_as_uninit<'a>(raw: *mut Self) -> &'a mut Self::Uninit {
        // SAFETY: The pointer is aligned and read-writeable for `'a` as promised by the caller.
        unsafe { &mut *(raw as *mut [MaybeUninit<T>]) }
    }
}