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
use core::{fmt::Debug, mem::MaybeUninit};

use crate::{private, Cell, CellOwner, Family, GetMutWithOwner, GetWithOwner};

/// Simple [`Debug`] implementation for [`CellOwner`].
///
/// # Example
/// ```
/// # #![cfg_attr(feature = "nightly", feature(thread_local))]
/// # cell_family::define!(type Family: CellOwner);
/// let owner = CellOwner::new();
///
/// assert_eq!(format!("{owner:?}"), "CellOwner { family: \"Family\" }");
/// ```
impl<F: Family> Debug for CellOwner<F> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("CellOwner")
            .field("family", &F::NAME)
            .finish()
    }
}

/// Simple [`Debug`] implementation for [`Cell`].
///
/// # Example
/// ```
/// # #![cfg_attr(feature = "nightly", feature(thread_local))]
/// # cell_family::define!(type Family: CellOwner for Cell<T>);
/// let cell = Cell::new(vec![1, 2, 3]);
/// let owner = CellOwner::new();
///
/// assert_eq!(format!("{cell:?}"), "Cell { family: \"Family\", .. }");
///
/// drop(owner);
///
/// assert_eq!(format!("{cell:?}"), "Cell { family: \"Family\", value: [1, 2, 3] }");
/// ```
impl<F: Family, T: Debug + ?Sized> Debug for Cell<F, T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        if let Some(owner) = CellOwner::try_new() {
            f.debug_struct("Cell")
                .field("family", &F::NAME)
                .field("value", &self.get(&owner))
                .finish()
        } else {
            f.debug_struct("Cell")
                .field("family", &F::NAME)
                .finish_non_exhaustive()
        }
    }
}

impl<'a, F: Family, T: ?Sized> private::Sealed<F> for &'a Cell<F, T> {}

impl<'a, F: Family, T: ?Sized> GetWithOwner<'a, F> for &'a Cell<F, T> {
    type Get = &'a T;

    fn get(self, owner: &'a CellOwner<F>) -> Self::Get {
        self.get(owner)
    }
}

impl<'a, F: Family, T: ?Sized> GetMutWithOwner<'a, F> for &'a Cell<F, T> {
    type GetMut = &'a mut T;

    fn try_get_mut(self, owner: &'a mut CellOwner<F>) -> Option<Self::GetMut> {
        Some(self.get_mut(owner))
    }

    fn get_mut(self, owner: &'a mut CellOwner<F>) -> Self::GetMut {
        self.get_mut(owner)
    }
}

impl<'a, F: Family, T, const N: usize> private::Sealed<F> for [&'a Cell<F, T>; N] {}

impl<'a, F: Family, T, const N: usize> GetWithOwner<'a, F> for [&'a Cell<F, T>; N] {
    type Get = [&'a T; N];

    fn get(self, owner: &'a CellOwner<F>) -> Self::Get {
        let mut result = uninit_array();

        for i in 0..self.len() {
            result[i].write(self[i].get(owner));
        }

        // SAFETY: all items were initialized in the loop above.
        unsafe { array_assume_init(result) }
    }
}

impl<'a, F: Family, T, const N: usize> GetMutWithOwner<'a, F> for [&'a Cell<F, T>; N] {
    type GetMut = [&'a mut T; N];

    fn try_get_mut(self, owner: &'a mut CellOwner<F>) -> Option<Self::GetMut> {
        let _ = owner;
        let mut result = uninit_array();

        for i in 0..self.len() {
            let item = self[i];

            for further_item in self.iter().copied().skip(i + 1) {
                if core::ptr::eq(item, further_item) {
                    return None;
                }
            }

            // SAFETY: we do have a mutably-borrowed `owner` here, and we made
            // sure with the inner loop that there is no other mutable borrow
            // of `item`.
            result[i].write(unsafe { item.get_unchecked_mut() });
        }

        // SAFETY: all items were initialized above.
        Some(unsafe { array_assume_init(result) })
    }
}

impl<'a, F: Family, T, const N: usize> private::Sealed<F> for &'a [Cell<F, T>; N] {}

impl<'a, F: Family, T, const N: usize> GetWithOwner<'a, F> for &'a [Cell<F, T>; N] {
    type Get = [&'a T; N];

    fn get(self, owner: &'a CellOwner<F>) -> Self::Get {
        let _ = owner;
        let mut result = uninit_array();

        for i in 0..self.len() {
            result[i].write(self[i].get(owner));
        }

        // SAFETY: all items were initialized in the loop above.
        unsafe { array_assume_init(result) }
    }
}

impl<'a, F: Family, T, const N: usize> GetMutWithOwner<'a, F> for &'a [Cell<F, T>; N] {
    type GetMut = [&'a mut T; N];

    fn try_get_mut(self, owner: &'a mut CellOwner<F>) -> Option<Self::GetMut> {
        // Edge case: each item in a slice of ZSTs point to the same memory
        // address, and referencing this address more than once would lead to
        // aliasing.
        if core::mem::size_of::<T>() == 0 && self.len() > 1 {
            return None;
        }

        let _ = owner;
        let mut result = uninit_array();

        for i in 0..self.len() {
            // SAFETY: the mutable reference to `owner` proves that no cell
            // contents in this family are mutably borrowed. Since every cell in
            // the current array is guaranteed to have a different address, we
            // can borrow simultaneously from different cells.
            result[i].write(unsafe { self[i].get_unchecked_mut() });
        }

        // SAFETY: all items were initialized in the loop above.
        Some(unsafe { array_assume_init(result) })
    }
}

#[inline]
fn uninit_array<T, const N: usize>() -> [MaybeUninit<T>; N] {
    #[cfg(feature = "nightly")]
    {
        MaybeUninit::uninit_array()
    }
    #[cfg(not(feature = "nightly"))]
    {
        // SAFETY: an uninitialized `[MaybeUninit<T>; N]` is valid.
        unsafe { MaybeUninit::<[MaybeUninit<T>; N]>::uninit().assume_init() }
    }
}

#[inline]
unsafe fn array_assume_init<T, const N: usize>(array: [MaybeUninit<T>; N]) -> [T; N] {
    #[cfg(feature = "nightly")]
    {
        MaybeUninit::array_assume_init(array)
    }
    #[cfg(not(feature = "nightly"))]
    {
        // SAFETY: see `MaybeUninit::array_assume_init`.
        (&array as *const _ as *const [T; N]).read()
    }
}

#[rustversion::nightly]
impl<'a, F: Family, T> private::Sealed<F> for &'a [Cell<F, T>] {}

#[rustversion::nightly]
impl<'a, F: Family, T> GetWithOwner<'a, F> for &'a [Cell<F, T>] {
    type Get = alloc::boxed::Box<[&'a T]>;

    fn get(self, owner: &'a CellOwner<F>) -> Self::Get {
        let _ = owner;
        let mut result = alloc::boxed::Box::new_uninit_slice(self.len());

        for i in 0..self.len() {
            result[i].write(self[i].get(owner));
        }

        // SAFETY: all items were initialized in the loop above.
        unsafe { result.assume_init() }
    }
}

#[rustversion::nightly]
impl<'a, F: Family, T> GetMutWithOwner<'a, F> for &'a [Cell<F, T>] {
    type GetMut = alloc::boxed::Box<[&'a mut T]>;

    fn try_get_mut(self, owner: &'a mut CellOwner<F>) -> Option<Self::GetMut> {
        // Edge case: each item in a slice of ZSTs point to the same memory
        // address, and referencing this address more than once would lead to
        // aliasing.
        if core::mem::size_of::<T>() == 0 && self.len() > 1 {
            return None;
        }

        let _ = owner;
        let mut result = alloc::boxed::Box::new_uninit_slice(self.len());

        for i in 0..self.len() {
            // SAFETY: the mutable reference to `owner` proves that no cell
            // contents in this family are mutably borrowed. Since every cell in
            // the current array is guaranteed to have a different address, we
            // can borrow simultaneously from different cells.
            result[i].write(unsafe { self[i].get_unchecked_mut() });
        }

        // SAFETY: all items were initialized in the loop above.
        Some(unsafe { result.assume_init() })
    }
}

#[inline]
fn are_unique<const N: usize>(pointers: [*const u8; N]) -> bool {
    // O(n log n) check.
    for i in 0..pointers.len() {
        for j in i + 1..pointers.len() {
            if core::ptr::eq(pointers[i], pointers[j]) {
                return false;
            }
        }
    }

    true
}

macro_rules! is_unique {
    () => {
        // No value: no-op.
        true
    };

    ( $unique: ident, ) => {
        // Only one value: no-op.
        true
    };

    ( $a: ident, $b: ident, ) => {
        // Two values: direct comparison.
        !core::ptr::eq($a, $b)
    };

    ( $($values: ident),* ) => {
        // More values: generic check.
        are_unique([$($values as *const _ as *const u8),*])
    };
}

macro_rules! impl_get_for_tuple {
    (
        $($name: ident),*
    ) => {
        #[allow(unused_parens, clippy::unused_unit)]
        impl<'a, CF: Family, $($name: ?Sized),*> private::Sealed<CF> for ($(&'a Cell<CF, $name>,)*) {}

        #[allow(unused_parens, clippy::unused_unit)]
        impl<'a, CF: Family, $($name: ?Sized),*> GetWithOwner<'a, CF> for ($(&'a Cell<CF, $name>,)*) {
            type Get = ($(&'a $name,)*);

            #[allow(non_snake_case, unused_variables)]
            fn get(self, owner: &'a CellOwner<CF>) -> Self::Get {
                let ($($name,)*) = self;

                ($($name.get(owner),)*)
            }
        }

        #[allow(unused_parens, clippy::unused_unit)]
        impl<'a, CF: Family, $($name: ?Sized),*> GetMutWithOwner<'a, CF> for ($(&'a Cell<CF, $name>,)*) {
            type GetMut = ($(&'a mut $name,)*);

            #[allow(non_snake_case, unused_unsafe)]
            fn try_get_mut(self, _owner: &'a mut CellOwner<CF>) -> Option<Self::GetMut> {
                let ($($name,)*) = self;

                if !is_unique!($($name),*) {
                    return None;
                }

                // SAFETY: all references are different, so we're not
                // introducing any aliasing.
                Some(unsafe {
                    ($($name.get_unchecked_mut(),)*)
                })
            }
        }
    };
}

impl_get_for_tuple!();
impl_get_for_tuple!(A);
impl_get_for_tuple!(A, B);
impl_get_for_tuple!(A, B, C);
impl_get_for_tuple!(A, B, C, D);
impl_get_for_tuple!(A, B, C, D, E);
impl_get_for_tuple!(A, B, C, D, E, F);
impl_get_for_tuple!(A, B, C, D, E, F, G);
impl_get_for_tuple!(A, B, C, D, E, F, G, H);
impl_get_for_tuple!(A, B, C, D, E, F, G, H, I);
impl_get_for_tuple!(A, B, C, D, E, F, G, H, I, J);