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
use alloc::boxed::Box;
use core::ffi::c_void;
use core::marker::PhantomData;
use core::ptr::NonNull;

use crate::encode::{EncodeConvert, Encoding};
use crate::rc::{Id, Ownership};
use crate::Message;

use super::InnerIvarType;

/// A helper type to allow putting certain types that may drop into ivars.
///
/// This is used to work around current limitations in the type system.
/// Consider this type "temporary" in the sense that one day it may just
/// become `type IvarDrop<T> = T`.
///
/// This currently works with the following types:
/// - `Box<T>`
/// - `Option<Box<T>>`
/// - `Id<T, O>`
/// - `Option<Id<T, O>>`
///
/// Further may be added when the standard library guarantees their layout.
///
/// See `examples/delegate.rs` for usage.
pub struct IvarDrop<T> {
    /// For proper variance and auto traits.
    p: PhantomData<T>,
}

impl<T: Sized> super::ivar::private::Sealed for IvarDrop<Box<T>> {}
// SAFETY: The memory layout of `Box<T: Sized>` is guaranteed to be a pointer:
// <https://doc.rust-lang.org/1.62.1/std/boxed/index.html#memory-layout>
//
// The user ensures that the Box has been initialized in an `init` method
// before being used.
unsafe impl<T: Sized> InnerIvarType for IvarDrop<Box<T>> {
    // Note that we use `*const c_void` and not `*const T` to allow _any_
    // type, not just types that can be encoded by Objective-C
    const __ENCODING: Encoding = <*const c_void as EncodeConvert>::__ENCODING;

    type __Inner = Option<Box<T>>;
    type Output = Box<T>;

    const __MAY_DROP: bool = true;

    #[inline]
    unsafe fn __to_ref(inner: &Self::__Inner) -> &Self::Output {
        match inner {
            Some(inner) => inner,
            None => unsafe { box_unreachable() },
        }
    }

    #[inline]
    unsafe fn __to_mut(inner: &mut Self::__Inner) -> &mut Self::Output {
        match inner {
            Some(inner) => inner,
            None => unsafe { box_unreachable() },
        }
    }

    #[inline]
    fn __to_ptr(inner: NonNull<Self::__Inner>) -> NonNull<Self::Output> {
        inner.cast()
    }
}

impl<T: Sized> super::ivar::private::Sealed for IvarDrop<Option<Box<T>>> {}
// SAFETY: `Option<Box<T>>` guarantees the null-pointer optimization, so for
// `T: Sized` the layout is just a pointer:
// <https://doc.rust-lang.org/1.62.1/std/option/index.html#representation>
//
// This is valid to initialize as all-zeroes, so the user doesn't have to do
// anything to initialize it.
unsafe impl<T: Sized> InnerIvarType for IvarDrop<Option<Box<T>>> {
    const __ENCODING: Encoding = <*const c_void as EncodeConvert>::__ENCODING;

    type __Inner = Option<Box<T>>;
    type Output = Option<Box<T>>;

    const __MAY_DROP: bool = true;

    #[inline]
    unsafe fn __to_ref(this: &Self::__Inner) -> &Self::Output {
        this
    }

    #[inline]
    unsafe fn __to_mut(this: &mut Self::__Inner) -> &mut Self::Output {
        this
    }

    #[inline]
    fn __to_ptr(inner: NonNull<Self::__Inner>) -> NonNull<Self::Output> {
        inner.cast()
    }
}

impl<T: Message, O: Ownership> super::ivar::private::Sealed for IvarDrop<Id<T, O>> {}
// SAFETY: `Id` is `NonNull<T>`, and hence safe to store as a pointer.
//
// The user ensures that the Id has been initialized in an `init` method
// before being used.
//
// Note: We could technically do `impl InnerIvarType for Ivar<Id<T, O>>`
// directly today, but since we can't do so for `Box` (because that is
// `#[fundamental]`), I think it makes sense to handle them similarly.
unsafe impl<T: Message, O: Ownership> InnerIvarType for IvarDrop<Id<T, O>> {
    const __ENCODING: Encoding = <*const T as EncodeConvert>::__ENCODING;

    type __Inner = Option<Id<T, O>>;
    type Output = Id<T, O>;

    const __MAY_DROP: bool = true;

    #[inline]
    unsafe fn __to_ref(inner: &Self::__Inner) -> &Self::Output {
        match inner {
            Some(inner) => inner,
            None => unsafe { id_unreachable() },
        }
    }

    #[inline]
    unsafe fn __to_mut(inner: &mut Self::__Inner) -> &mut Self::Output {
        match inner {
            Some(inner) => inner,
            None => unsafe { id_unreachable() },
        }
    }

    #[inline]
    fn __to_ptr(inner: NonNull<Self::__Inner>) -> NonNull<Self::Output> {
        inner.cast()
    }
}

impl<T: Message, O: Ownership> super::ivar::private::Sealed for IvarDrop<Option<Id<T, O>>> {}
// SAFETY: `Id<T, O>` guarantees the null-pointer optimization.
//
// This is valid to initialize as all-zeroes, so the user doesn't have to do
// anything to initialize it.
unsafe impl<T: Message, O: Ownership> InnerIvarType for IvarDrop<Option<Id<T, O>>> {
    const __ENCODING: Encoding = <*const T as EncodeConvert>::__ENCODING;

    type __Inner = Option<Id<T, O>>;
    type Output = Option<Id<T, O>>;

    const __MAY_DROP: bool = true;

    #[inline]
    unsafe fn __to_ref(this: &Self::__Inner) -> &Self::Output {
        this
    }

    #[inline]
    unsafe fn __to_mut(this: &mut Self::__Inner) -> &mut Self::Output {
        this
    }

    #[inline]
    fn __to_ptr(inner: NonNull<Self::__Inner>) -> NonNull<Self::Output> {
        inner.cast()
    }
}

// TODO: Allow the following once their layout is guaranteed by `std`:
// - Arc<T>
// - Option<Arc<T>>
// - sync::Weak<T>
// - Rc<T>
// - Option<Rc<T>>
// - rc::Weak<T>
// - Vec<T>
// - String

// TODO: Allow `WeakId` once we figure out how to allow it being initialized
// by default.

#[inline]
unsafe fn id_unreachable() -> ! {
    #[cfg(debug_assertions)]
    {
        unreachable!("an Id in instance variables must always be initialized before use!")
    }
    // SAFETY: Checked by caller
    #[cfg(not(debug_assertions))]
    unsafe {
        core::hint::unreachable_unchecked()
    }
}

#[inline]
unsafe fn box_unreachable() -> ! {
    #[cfg(debug_assertions)]
    {
        unreachable!("a Box in instance variables must always be initialized before use!")
    }
    // SAFETY: Checked by caller
    #[cfg(not(debug_assertions))]
    unsafe {
        core::hint::unreachable_unchecked()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::declare::{Ivar, IvarType};
    use crate::foundation::NSObject;
    use crate::rc::{Allocated, Owned, RcTestObject, Shared, ThreadTestData};
    use crate::runtime::Object;
    use crate::{declare_class, msg_send, msg_send_id, ClassType};

    struct TestIvar1;
    unsafe impl IvarType for TestIvar1 {
        type Type = IvarDrop<Box<u8>>;
        const NAME: &'static str = "_abc";
    }

    struct TestIvar2;
    unsafe impl IvarType for TestIvar2 {
        type Type = IvarDrop<Option<Box<u8>>>;
        const NAME: &'static str = "_abc";
    }

    struct TestIvar3;
    unsafe impl IvarType for TestIvar3 {
        type Type = IvarDrop<Id<Object, Shared>>;
        const NAME: &'static str = "_abc";
    }

    struct TestIvar4;
    unsafe impl IvarType for TestIvar4 {
        type Type = IvarDrop<Option<Id<Object, Owned>>>;
        const NAME: &'static str = "_abc";
    }

    declare_class!(
        #[derive(Debug, PartialEq)]
        struct IvarTester {
            ivar1: IvarDrop<Id<RcTestObject, Shared>>,
            ivar2: IvarDrop<Option<Id<RcTestObject, Owned>>>,
            ivar3: IvarDrop<Box<Id<RcTestObject, Owned>>>,
            ivar4: IvarDrop<Option<Box<Id<RcTestObject, Owned>>>>,
        }

        unsafe impl ClassType for IvarTester {
            type Super = NSObject;
        }

        unsafe impl IvarTester {
            #[sel(init)]
            fn init(&mut self) -> Option<&mut Self> {
                let this: Option<&mut Self> = unsafe { msg_send![super(self), init] };
                this.map(|this| {
                    Ivar::write(&mut this.ivar1, Id::into_shared(RcTestObject::new()));
                    *this.ivar2 = Some(RcTestObject::new());
                    Ivar::write(&mut this.ivar3, Box::new(RcTestObject::new()));
                    *this.ivar4 = Some(Box::new(RcTestObject::new()));
                    this
                })
            }

            #[sel(initInvalid)]
            fn init_invalid(&mut self) -> Option<&mut Self> {
                // Don't actually initialize anything here; this creates an
                // invalid instance, where accessing the two ivars `ivar1`
                // and `ivar3` is UB
                unsafe { msg_send![super(self), init] }
            }
        }
    );

    #[test]
    fn test_alloc_dealloc() {
        let expected = ThreadTestData::current();

        let obj: Id<Allocated<IvarTester>, Owned> =
            unsafe { msg_send_id![IvarTester::class(), alloc] };
        expected.assert_current();

        drop(obj);
        expected.assert_current();
    }

    #[test]
    fn test_init_drop() {
        let mut expected = ThreadTestData::current();

        let mut obj: Id<IvarTester, Owned> = unsafe { msg_send_id![IvarTester::class(), new] };
        expected.alloc += 4;
        expected.init += 4;
        expected.assert_current();

        *obj.ivar1 = (*obj.ivar1).clone();
        expected.retain += 1;
        expected.release += 1;
        expected.assert_current();

        *obj.ivar2 = None;
        expected.release += 1;
        expected.dealloc += 1;
        expected.assert_current();

        drop(obj);
        expected.release += 3;
        expected.dealloc += 3;
        expected.assert_current();
    }

    #[test]
    #[cfg_attr(not(debug_assertions), ignore = "only panics in debug mode")]
    #[should_panic = "an Id in instance variables must always be initialized before use"]
    fn test_init_invalid_ref() {
        let obj: Id<IvarTester, Owned> =
            unsafe { msg_send_id![msg_send_id![IvarTester::class(), alloc], initInvalid] };

        std::println!("{:?}", obj.ivar1);
    }

    #[test]
    #[cfg_attr(not(debug_assertions), ignore = "only panics in debug mode")]
    #[should_panic = "an Id in instance variables must always be initialized before use"]
    fn test_init_invalid_mut() {
        let mut obj: Id<IvarTester, Owned> =
            unsafe { msg_send_id![msg_send_id![IvarTester::class(), alloc], initInvalid] };

        *obj.ivar1 = RcTestObject::new().into();
    }
}