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
//! A pointer type for heap allocation.

use ::core::{alloc::Layout, fmt, mem::MaybeUninit};
use ::mischief::{In, Slot};
use ::munge::munge;
use ::ptr_meta::Pointee;
use ::rel_core::{
    Basis,
    BasisPointee,
    DefaultBasis,
    Emplace,
    EmplaceExt,
    Move,
    Portable,
    RelPtr,
};
use ::situ::{
    alloc::RawRegionalAllocator,
    fmt::{DebugRaw, DisplayRaw},
    ops::{DerefMutRaw, DerefRaw, IndexMutRaw, IndexRaw},
    DropRaw,
    Mut,
    OwnedVal,
    Ref,
    Val,
};

use crate::alloc::RelAllocator;

/// A relative counterpart to `Box`.
#[derive(Move, Portable)]
#[repr(C)]
pub struct RelBox<
    T: BasisPointee<B> + ?Sized,
    A: RawRegionalAllocator,
    B: Basis = DefaultBasis,
> {
    ptr: RelPtr<T, A::Region, B>,
    alloc: A,
}

impl<T, A, B> DropRaw for RelBox<T, A, B>
where
    T: BasisPointee<B> + DropRaw + ?Sized,
    A: RawRegionalAllocator + DropRaw,
    B: Basis,
{
    #[inline]
    unsafe fn drop_raw(mut this: Mut<'_, Self>) {
        let inner = Self::deref_mut_raw(this.as_mut());
        let layout = Layout::for_value(&*inner);

        let inner_ptr = inner.as_non_null();
        // SAFETY: `inner` is valid for dropping because we own it. It will
        // never be accessed again because `this` will never be accessed again.
        unsafe {
            DropRaw::drop_raw(inner);
        }

        munge!(let RelBox { ptr, alloc } = this);

        // SAFETY: `ptr` is never null and always allocated in `alloc` with a
        // layout of `layout`.
        unsafe {
            A::raw_deallocate(alloc.as_ref(), inner_ptr.cast(), layout);
        }

        // SAFETY: `ptr` and `alloc` are always valid for dropping and are not
        // accessed again.
        unsafe {
            DropRaw::drop_raw(ptr);
            DropRaw::drop_raw(alloc);
        }
    }
}

impl<T, A, B> RelBox<T, A, B>
where
    T: BasisPointee<B> + ?Sized,
    A: RawRegionalAllocator,
    B: Basis,
{
    /// Returns a reference to the underlying allocator.
    #[inline]
    pub fn allocator(this: Ref<'_, Self>) -> Ref<'_, A> {
        munge!(let RelBox { alloc, .. } = this);
        alloc
    }
}

impl<T, A, B> DerefRaw for RelBox<T, A, B>
where
    T: BasisPointee<B> + ?Sized,
    A: RawRegionalAllocator,
    B: Basis,
{
    type Target = T;

    fn deref_raw(this: Ref<'_, Self>) -> Ref<'_, T> {
        munge!(let RelBox { ptr, .. } = this);
        // SAFETY:
        // - The value pointed to by a `RelBox` is always non-null,
        //   properly-aligned, and valid for reads.
        // - Because `this` is a shared reference and `ptr` is borrowed from it,
        //   `ptr` cannot alias any other mutable references for `'_`.
        // - The value pointed to by a `RelBox` is always initialized.
        unsafe { RelPtr::as_ref(ptr) }
    }
}

impl<T, A, B> DerefMutRaw for RelBox<T, A, B>
where
    T: BasisPointee<B> + ?Sized,
    A: RawRegionalAllocator,
    B: Basis,
{
    fn deref_mut_raw(this: Mut<'_, Self>) -> Mut<'_, T> {
        munge!(let RelBox { ptr, .. } = this);
        // SAFETY:
        // - The value pointed to by a `RelBox` is always non-null,
        //   properly-aligned, and valid for reads and writes.
        // - Because `this` is a shared reference and `ptr` is borrowed from it,
        //   `ptr` cannot alias any other accessible references for `'_`.
        // - The value pointed to by a `RelBox` is always initialized and
        //   treated as immovable.
        unsafe { RelPtr::as_mut(ptr) }
    }
}

impl<T, A, B> DebugRaw for RelBox<T, A, B>
where
    T: BasisPointee<B> + DebugRaw + ?Sized,
    A: RawRegionalAllocator,
    B: Basis,
{
    fn fmt_raw(
        this: Ref<'_, Self>,
        f: &mut fmt::Formatter<'_>,
    ) -> Result<(), fmt::Error> {
        DebugRaw::fmt_raw(DerefRaw::deref_raw(this), f)
    }
}

impl<T, A, B> DisplayRaw for RelBox<T, A, B>
where
    T: BasisPointee<B> + DisplayRaw + ?Sized,
    A: RawRegionalAllocator,
    B: Basis,
{
    fn fmt_raw(
        this: Ref<'_, Self>,
        f: &mut fmt::Formatter<'_>,
    ) -> Result<(), fmt::Error> {
        DisplayRaw::fmt_raw(DerefRaw::deref_raw(this), f)
    }
}

impl<T, A, B, Idx> IndexRaw<Idx> for RelBox<T, A, B>
where
    T: BasisPointee<B> + IndexRaw<Idx> + ?Sized,
    A: RawRegionalAllocator,
    B: Basis,
{
    type Output = <T as IndexRaw<Idx>>::Output;

    fn index_raw(this: Ref<'_, Self>, index: Idx) -> Ref<'_, Self::Output> {
        IndexRaw::index_raw(DerefRaw::deref_raw(this), index)
    }

    unsafe fn index_raw_unchecked(
        this: Ref<'_, Self>,
        index: Idx,
    ) -> Ref<'_, Self::Output> {
        // SAFETY: The caller has guaranteed that `index` is in bounds for
        // indexing.
        unsafe {
            IndexRaw::index_raw_unchecked(DerefRaw::deref_raw(this), index)
        }
    }
}

impl<T, A, B, Idx> IndexMutRaw<Idx> for RelBox<T, A, B>
where
    T: BasisPointee<B> + IndexMutRaw<Idx> + ?Sized,
    A: RawRegionalAllocator,
    B: Basis,
{
    fn index_mut_raw(this: Mut<'_, Self>, index: Idx) -> Mut<'_, Self::Output> {
        IndexMutRaw::index_mut_raw(DerefMutRaw::deref_mut_raw(this), index)
    }

    unsafe fn index_mut_raw_unchecked(
        this: Mut<'_, Self>,
        index: Idx,
    ) -> Mut<'_, Self::Output> {
        // SAFETY: The caller has guaranteed that `index` is in bounds for
        // indexing.
        unsafe {
            IndexMutRaw::index_mut_raw_unchecked(
                DerefMutRaw::deref_mut_raw(this),
                index,
            )
        }
    }
}

impl<T, A, B> RelBox<MaybeUninit<T>, A, B>
where
    T: DropRaw,
    A: RawRegionalAllocator + DropRaw,
    B: Basis,
{
    /// Converts to a `RelBox<T, A, B>`.
    ///
    /// # Safety
    ///
    /// The value in this `RelBox` must be initialized.
    pub unsafe fn assume_init(b: Val<Self>) -> Val<RelBox<T, A, B>> {
        // SAFETY: The caller has guaranteed that the underlying `MaybeUninit`
        // has been properly initialized, and `MaybeUninit<T>` has the same
        // layout as the `T` it wraps.
        unsafe { b.cast() }
    }
}

// SAFETY:
// - `RelBox` is `Sized` and always has metadata `()`, so `emplaced_meta` always
//   returns valid metadata for it.
// - `emplace_unsized_unchecked` initializes its `out` parameter.
unsafe impl<T, A, B, R> Emplace<RelBox<T, A, B>, R::Region> for OwnedVal<T, R>
where
    T: BasisPointee<B> + DropRaw + ?Sized,
    A: DropRaw + RawRegionalAllocator<Region = R::Region>,
    B: Basis,
    R: RelAllocator<A>,
{
    #[inline]
    fn emplaced_meta(&self) -> <RelBox<T, A, B> as Pointee>::Metadata {}

    unsafe fn emplace_unsized_unchecked(
        self,
        mut out: In<Slot<'_, RelBox<T, A, B>>, R::Region>,
    ) {
        let this = In::new(self);
        let ptr = this.as_raw();
        let (_, alloc) = OwnedVal::into_raw_parts(In::into_inner(this));

        munge!(let RelBox { ptr: out_ptr, alloc: out_alloc } = out.as_mut());

        ptr.emplace(out_ptr);
        alloc.emplace(out_alloc);
    }
}