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
// Copyright (c) ZeroC, Inc.

use std::any::TypeId;

/// Represents a pointer that owns the data it's pointing to.
/// When this pointer is dropped, it ensures the pointed to data is dropped as well.
///
/// This can be used in conjunction with [`WeakPtr`] to form complex (and sometimes cyclic) data structures while still
/// adhering to Rust's ownership rules, and avoiding un-droppable memory cycles.
#[derive(Debug)]
pub struct OwnedPtr<T: ?Sized> {
    data: Box<T>,
    concrete_type_id: TypeId,
}

impl<T: Sized + 'static> OwnedPtr<T> {
    pub fn new(value: T) -> Self {
        OwnedPtr {
            data: Box::new(value),
            concrete_type_id: TypeId::of::<T>(),
        }
    }
}

impl<T: ?Sized> OwnedPtr<T> {
    #[allow(clippy::should_implement_trait)]
    pub fn borrow(&self) -> &T {
        &self.data
    }

    /// # Safety
    ///
    /// This function doesn't use unsafe Rust, but is marked unsafe because the invoker must
    /// GUARANTEE there are no other references to the underlying data when calling this function.
    ///
    /// The borrow checker can ensure that there are no other references through this `OwnedPtr`,
    /// but it's possible to obtain a reference via `WeakPtr::borrow` instead. Because `WeakPtr`
    /// uses raw pointers, the borrow checker can't reason about these accesses. So we have to
    /// enforce Rust's borrow policy manually by ensuring this mutable borrow is the ONLY borrow.
    ///
    /// Mutating the underlying data while another reference to it still exists, is undefined
    /// behavior. So ONLY call this function if you are CERTAIN that NO other references exist.
    #[allow(clippy::should_implement_trait)]
    pub unsafe fn borrow_mut(&mut self) -> &mut T {
        &mut self.data
    }

    pub fn downgrade(&self) -> WeakPtr<T> {
        WeakPtr {
            data: Some(&*self.data),
            concrete_type_id: self.concrete_type_id,
        }
    }

    pub fn downcast<U: 'static>(self) -> Result<OwnedPtr<U>, OwnedPtr<T>> {
        // Make sure that the original concrete type of this `OwnedPtr` matches the requested type.
        // If it doesn't, return an error holding the uncasted `OwnedPtr`.
        if self.concrete_type_id == TypeId::of::<U>() {
            // Convert the underlying box into a raw pointer so we can forcibly cast it.
            let inner = Box::into_raw(self.data);
            // Cast the pointer to the original concrete type and re-box it.
            let converted = unsafe { Box::from_raw(inner as *mut U) };
            // Construct a new OwnedPtr with the downcasted type.
            Ok(OwnedPtr::from_inner((converted, self.concrete_type_id)))
        } else {
            Err(self)
        }
    }

    pub fn from_inner(inner: (Box<T>, TypeId)) -> Self {
        OwnedPtr {
            data: inner.0,
            concrete_type_id: inner.1,
        }
    }

    pub fn into_inner(self) -> (Box<T>, TypeId) {
        (self.data, self.concrete_type_id)
    }
}

/// Represents a pointer that only references the data it's pointing to.
/// Unlike [`OwnedPtr`], dropping this pointer has no effect on the underlying data, this pointer can only immutably
/// access the underlying data, and care must be taken to prevent this pointer from dangling.
///
/// This can be used in conjunction with [`OwnedPtr`] to form complex (and sometimes cyclic) data structures while
/// still adhering to Rust's ownership rules, and avoiding un-droppable memory cycles.
#[derive(Debug)]
pub struct WeakPtr<T: ?Sized> {
    data: Option<*const T>,
    concrete_type_id: TypeId,
}

impl<T: ?Sized + 'static> WeakPtr<T> {
    pub fn create_uninitialized() -> Self {
        WeakPtr {
            data: None,
            concrete_type_id: TypeId::of::<T>(),
        }
    }
}

impl<T: ?Sized> WeakPtr<T> {
    pub fn is_initialized(&self) -> bool {
        self.data.is_some()
    }

    // This isn't marked as unsafe because it's assumed all WeakPtr live inside the AST, alongside
    // the OwnedPtr. Since the entire AST goes out of scope at the same time when the program ends,
    // it's impossible to have a dangling pointer here, and so, this function is always safe.
    //
    // Note that it IS still possible to call this on an uninitialized WeakPtr, which will cause a
    // panic. But this isn't 'unsafe' in the technical sense of involving unsafe Rust.
    #[allow(clippy::should_implement_trait)]
    pub fn borrow(&self) -> &T {
        unsafe { &*self.data.unwrap() }
    }

    pub fn downcast<U: 'static>(self) -> Result<WeakPtr<U>, WeakPtr<T>> {
        // Make sure that the original concrete type of this `WeakPtr` matches the requested type.
        // If it doesn't, return an error holding the uncasted `WeakPtr`.
        if self.concrete_type_id == TypeId::of::<U>() {
            // Forcibly downcast the underlying pointer to the original concrete type.
            let converted = self.data.map(|ptr| ptr as *const U);
            // Construct a new WeakPtr with the downcasted type.
            Ok(WeakPtr::from_inner((converted, self.concrete_type_id)))
        } else {
            Err(self)
        }
    }

    pub fn from_inner(inner: (Option<*const T>, TypeId)) -> Self {
        WeakPtr {
            data: inner.0,
            concrete_type_id: inner.1,
        }
    }

    pub fn into_inner(self) -> (Option<*const T>, TypeId) {
        (self.data, self.concrete_type_id)
    }
}

impl<T: ?Sized> Clone for WeakPtr<T> {
    fn clone(&self) -> Self {
        WeakPtr {
            data: self.data,
            concrete_type_id: self.concrete_type_id,
        }
    }
}

// It is safe to send and share these pointers between threads, since they do not use interior mutability.
// It is impossible to mutate the pointed-to data through a `WeakPtr`, and mutating the pointed-to data through an
// `OwnedPtr` is only possible through a mutable reference to the pointer itself, guaranteeing exclusivity.
// Additionally, both `WeakPtr` and `OwnedPtr` are covariant over `T`, and the lifetimes of references through them.
unsafe impl<T: ?Sized + Send> Send for OwnedPtr<T> {}
unsafe impl<T: ?Sized + Sync> Sync for OwnedPtr<T> {}
unsafe impl<T: ?Sized + Send> Send for WeakPtr<T> {}
unsafe impl<T: ?Sized + Sync> Sync for WeakPtr<T> {}

// TODO
// Implementing these traits would give our pointers support for implicit upcasting (casting a
// concrete type to a trait type it implements). But the trait is still marked as unstable.
// When it's stabilized, this should be uncommented, and the macros beneath this deleted.
//
// impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<OwnedPtr<U>> for OwnedPtr<T> {}
// impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WeakPtr<U>> for WeakPtr<T> {}

#[macro_export]
macro_rules! downgrade_as {
    ($owned:expr, $new_type:ty) => {
        $crate::upcast_weak_as!($owned.downgrade(), $new_type)
    };
}

#[macro_export]
macro_rules! upcast_owned_as {
    ($owned:expr, $new_type:ty) => {{
        let (data, type_id) = $owned.into_inner();
        OwnedPtr::from_inner((data as Box<$new_type>, type_id))
    }};
}

#[macro_export]
macro_rules! upcast_weak_as {
    ($weak:expr, $new_type:ty) => {{
        let (data, type_id) = $weak.into_inner();
        WeakPtr::from_inner((data.map(|ptr| ptr as *const $new_type), type_id))
    }};
}

impl<'a, T: ?Sized, U: ?Sized> PartialEq<&'a T> for OwnedPtr<U> {
    /// Returns true if this pointer and the provided reference both point to the same memory address.
    ///
    /// Note that this may return true in some unintuitive/exotic cases:
    /// - If you have 2 references to the same piece of data, with different types, this will return true. For example,
    ///   comparing `String` to `dyn Display` is valid, and will return true if they're actually the same.
    /// - If one or both of the types are zero-sized: since it's address may overlap with another piece of data.
    /// - Comparing the address of a struct to the address of its first field: these are conceptually different things,
    ///   but both live at the same address, since structs are stored as a list of it's fields.
    ///
    /// See <https://doc.rust-lang.org/std/ptr/fn.eq.html> for more information. This function uses the same semantics.
    fn eq(&self, other: &&'a T) -> bool {
        // Convert this pointer's box and the other borrow to raw pointers, then strip their typing, and convert any
        // DST fat pointers to thin pointers to avoid checking their v-tables (which can be transient).
        let self_ptr = (&*self.data as *const U).cast::<()>();
        let other_ptr = (*other as *const T).cast::<()>();
        // Check if the data pointers point to the same location in memory.
        std::ptr::eq(self_ptr, other_ptr)
    }
}

impl<'a, T: ?Sized, U: ?Sized> PartialEq<&'a T> for WeakPtr<U> {
    /// Returns true if this pointer and the provided reference both point to the same memory address.
    ///
    /// Note that this may return true in some unintuitive/exotic cases:
    /// - If you have 2 references to the same piece of data, with different types, this will return true. For example,
    ///   comparing `String` to `dyn Display` is valid, and will return true if they're actually the same.
    /// - If one or both of the types are zero-sized: since it's address may overlap with another piece of data.
    /// - Comparing the address of a struct to the address of its first field: these are conceptually different things,
    ///   but both live at the same address, since structs are stored as a list of it's fields.
    ///
    /// See <https://doc.rust-lang.org/std/ptr/fn.eq.html> for more information. This function uses the same semantics.
    fn eq(&self, other: &&'a T) -> bool {
        // Convert the other borrow to a raw pointer, then strip it and this pointer's typing, and convert any
        // DST fat pointers to thin pointers to avoid checking their v-tables (which can be transient).
        let Some(self_ptr) = self.data else { return false };
        let other_ptr = (*other as *const T).cast::<()>();
        // Check if the data pointers point to the same location in memory.
        std::ptr::eq(self_ptr.cast::<()>(), other_ptr)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Ensure that it's valid to have multiple immutable borrows to a piece a data through
    // the pointer that owns it, and any weak pointers created from it.
    #[test]
    fn multiple_immutable_borrows_is_legal() {
        // Arrange
        let owned_ptr = OwnedPtr::new(79_u32);
        let weak_ptr1 = owned_ptr.downgrade();
        let weak_ptr2 = owned_ptr.downgrade();

        let borrow0 = owned_ptr.borrow();
        let borrow1 = weak_ptr1.borrow();
        let borrow2 = weak_ptr2.borrow();

        // Act
        // Use all the borrowed values to prevent the compiler from dropping them pre-maturely.
        let dummy = borrow0 + borrow1 + borrow2;

        // Assert
        assert_eq!(dummy, 3 * 79);
    }

    // Ensure that accessing an uninitialized pointer causes a panic.
    // It's impossible to construct an uninitialized `OwnedPtr`, so we only check `WeakPtr`.
    #[test]
    #[should_panic]
    fn accessing_uninitialized_pointer_causes_panic() {
        // Arrange
        let weak_ptr: WeakPtr<String> = WeakPtr::create_uninitialized();

        // Act
        weak_ptr.borrow();

        // Assert
        // This function should panic in the 'act' section. This is 'asserted' by the 'should_panic' attribute on it.
    }

    #[test]
    fn pointer_equality_is_reflexive() {
        // Arrange
        let owned_ptr = OwnedPtr::new("test".to_owned());
        let weak_ptr = owned_ptr.downgrade();

        // Act/Assert: asserting that they're equal is the 'act'.
        assert_eq!(owned_ptr, owned_ptr.borrow());
        assert_eq!(owned_ptr, weak_ptr.borrow());
        assert_eq!(weak_ptr, owned_ptr.borrow());
        assert_eq!(weak_ptr, weak_ptr.borrow());
    }

    // Ensure that two pointers that point to the same memory location are equal, even if they hold different types.
    #[test]
    fn pointer_equality_is_type_independent() {
        // Arrange
        let owned_ptr: OwnedPtr<String> = OwnedPtr::new("test".to_owned());

        // Create a weak pointer to the string.
        let weak_ptr: WeakPtr<String> = owned_ptr.downgrade();
        // Rip it apart and manually cast the pointer from `String` to `bool`.
        let (raw_pointer, type_id) = weak_ptr.into_inner();
        let casted_pointer = raw_pointer.map(|ptr| ptr as *const bool);
        // Re-assemble the weak pointer with the casted type.
        // This is safe and legal to do in Rust, but borrowing from this pointer in any way would be unsafe.
        let casted_weak_ptr: WeakPtr<bool> = WeakPtr::from_inner((casted_pointer, type_id));

        // Act/Assert: asserting that they're equal is the 'act'.
        assert_eq!(casted_weak_ptr, owned_ptr.borrow());
    }

    // Ensure that two pointers that point to different memory locations are unequal, even if they point to equal data.
    #[test]
    fn different_pointers_are_not_equal() {
        // Arrange
        let owned_ptr1 = OwnedPtr::new(79_i32);
        let weak_ptr1 = owned_ptr1.downgrade();

        let owned_ptr2 = OwnedPtr::new(79_i32);
        let weak_ptr2 = owned_ptr2.downgrade();

        // Act/Assert: asserting that they're not equal is the 'act'.
        assert_ne!(owned_ptr1, owned_ptr2.borrow());
        assert_ne!(owned_ptr1, weak_ptr2.borrow());

        assert_ne!(weak_ptr1, owned_ptr2.borrow());
        assert_ne!(weak_ptr1, weak_ptr2.borrow());

        assert_ne!(owned_ptr2, owned_ptr1.borrow());
        assert_ne!(owned_ptr2, weak_ptr1.borrow());

        assert_ne!(weak_ptr2, owned_ptr1.borrow());
        assert_ne!(weak_ptr2, weak_ptr1.borrow());
    }
}