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
//! Helper types for a holder of data.

use std::any;
use std::fmt;

/// Our own private dynamic Any implementation.
///
/// In contrast to `Box<dyn std::any::Any>`, this allows for storing a raw
/// pointer directly in the object to avoid one level of indirection. Otherwise
/// it's equivalent.
#[repr(C)]
pub struct Any {
    data: *const (),
    vtable: &'static Vtable,
}

impl fmt::Debug for Any {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(fmt, "Any({})", self.type_name())
    }
}

impl Any {
    /// Construct a new any from the original any.
    pub fn new<T>(data: T) -> Self
    where
        T: any::Any,
    {
        let data = Box::into_raw(Box::new(data));

        return Any {
            vtable: &Vtable {
                drop: drop_impl::<T>,
                as_ptr: as_ptr_impl::<T>,
                as_mut_ptr: as_mut_ptr_impl::<T>,
                take_mut_ptr: as_mut_ptr_impl::<T>,
                type_name: any::type_name::<T>,
                type_id: any::TypeId::of::<T>,
            },
            data: data as *mut (),
        };

        unsafe fn drop_impl<T>(this: *const ()) {
            Box::from_raw(this as *mut () as *mut T);
        }
    }

    /// Construct a new any from a pointer.
    ///
    /// # Safety
    ///
    /// It is up to the caller to make sure that whatever data is pointed to is
    /// valid for the duration of the `Any`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// let value = 1u32;
    /// let any = unsafe { stk::Any::from_ptr(&value) };
    /// assert!(any.is::<u32>());
    /// assert_eq!(Some(&1u32), any.downcast_ref());
    /// ```
    pub unsafe fn from_ptr<T>(data: *const T) -> Self
    where
        T: any::Any,
    {
        Any {
            vtable: &Vtable {
                drop: noop_drop_impl::<T>,
                as_ptr: as_ptr_impl::<T>,
                as_mut_ptr: unsupported_as_mut::<T>,
                take_mut_ptr: unsupported_as_mut::<T>,
                type_name: any::type_name::<T>,
                type_id: any::TypeId::of::<T>,
            },
            data: data as *const (),
        }
    }

    /// Construct a new any from a mutable pointer.
    ///
    /// # Safety
    ///
    /// It is up to the caller to make sure that whatever data is pointed to is
    /// valid for the duration of the `Any`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// let mut value = 1u32;
    /// let mut any = unsafe { stk::Any::from_mut_ptr(&mut value) };
    /// assert!(any.is::<u32>());
    /// *any.downcast_mut::<u32>().unwrap() = 2;
    /// assert_eq!(Some(&2u32), any.downcast_ref());
    /// ```
    pub unsafe fn from_mut_ptr<T>(data: *mut T) -> Self
    where
        T: any::Any,
    {
        Any {
            vtable: &Vtable {
                drop: noop_drop_impl::<T>,
                as_ptr: as_ptr_impl::<T>,
                as_mut_ptr: as_mut_ptr_impl::<T>,
                take_mut_ptr: unsupported_as_mut::<T>,
                type_name: any::type_name::<T>,
                type_id: any::TypeId::of::<T>,
            },
            data: data as *mut (),
        }
    }

    /// Returns `true` if the boxed type is the same as `T`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// let any = stk::Any::new(1u32);
    /// assert!(any.is::<u32>());
    /// ```
    #[inline]
    pub fn is<T>(&self) -> bool
    where
        T: any::Any,
    {
        any::TypeId::of::<T>() == self.type_id()
    }

    /// Returns some reference to the boxed value if it is of type `T`, or
    /// `None` if it isn't.
    ///
    /// # Examples
    ///
    /// ```rust
    /// let any = stk::Any::new(1u32);
    /// assert_eq!(Some(&1u32), any.downcast_ref::<u32>());
    /// assert_eq!(None, any.downcast_ref::<&u32>());
    /// ```
    #[inline]
    pub fn downcast_ref<T>(&self) -> Option<&T>
    where
        T: any::Any,
    {
        if self.is::<T>() {
            unsafe { Some(&*(self.data as *const T)) }
        } else {
            None
        }
    }

    /// Returns some mutable reference to the boxed value if it is of type `T`, or
    /// `None` if it isn't.
    ///
    /// # Examples
    ///
    /// ```rust
    /// let mut any = stk::Any::new(1u32);
    /// *any.downcast_mut::<u32>().unwrap() = 2;
    /// assert_eq!(Some(&2u32), any.downcast_ref::<u32>());
    /// ```
    #[inline]
    pub fn downcast_mut<T>(&mut self) -> Option<&mut T>
    where
        T: any::Any,
    {
        if self.is::<T>() {
            unsafe { Some(&mut *(self.data as *mut () as *mut T)) }
        } else {
            None
        }
    }

    /// Attempt to perform a conversion to a raw pointer.
    pub fn as_ptr(&self, expected_type: any::TypeId) -> Option<*const ()> {
        // Safety: invariants are checked at construction time.
        unsafe { (self.vtable.as_ptr)(self.data, expected_type) }
    }

    /// Attempt to perform a conversion to a raw mutable pointer.
    pub fn as_mut_ptr(&mut self, expected_type: any::TypeId) -> Option<*mut ()> {
        // Safety: invariants are checked at construction time.
        unsafe { (self.vtable.as_mut_ptr)(self.data, expected_type) }
    }

    /// Attempt to perform a conversion to a raw mutable pointer with the intent
    /// of taking it.
    ///
    /// If the conversion is not possible, we return a reconstructed `Any` as
    /// the error variant.
    pub fn take_mut_ptr(self, expected_type: any::TypeId) -> Result<*mut (), Self> {
        use std::mem::ManuallyDrop;

        let this = ManuallyDrop::new(self);

        // Safety: invariants are checked at construction time.
        match unsafe { (this.vtable.take_mut_ptr)(this.data, expected_type) } {
            Some(data) => Ok(data),
            None => Err(ManuallyDrop::into_inner(this)),
        }
    }

    /// Access the underlying type name for the data.
    pub fn type_name(&self) -> &'static str {
        (self.vtable.type_name)()
    }

    /// Access the underlying type id for the data.
    pub fn type_id(&self) -> any::TypeId {
        (self.vtable.type_id)()
    }
}

impl Drop for Any {
    fn drop(&mut self) {
        // Safety: The safety of the called implementation is guaranteed at
        // compile time.
        unsafe {
            (self.vtable.drop)(self.data);
        }
    }
}

type DropFn = unsafe fn(*const ());
type AsPtrFn = unsafe fn(*const (), expected_type: any::TypeId) -> Option<*const ()>;
type AsMutPtrFn = unsafe fn(*const (), expected_type: any::TypeId) -> Option<*mut ()>;
type TakeMutPtrFn = unsafe fn(*const (), expected_type: any::TypeId) -> Option<*mut ()>;
type TypeNameFn = fn() -> &'static str;
type TypeIdFn = fn() -> any::TypeId;

/// The vtable for any type stored in the virtual machine.
///
/// We rely _heavily_ on the invariants provided by `std::any::Any` which are
/// checked at construction-time for this type.
#[repr(C)]
struct Vtable {
    /// The underlying drop implementation for the stored type.
    drop: DropFn,
    /// Conversion to pointer.
    as_ptr: AsPtrFn,
    /// Conversion to mutable pointer.
    as_mut_ptr: AsMutPtrFn,
    /// Pointer to the function used to "take" the inner value.
    /// This can optionally be punted into an implementation which always
    /// returns `None` in case taking is not supported, as it would be with
    /// pointers.
    take_mut_ptr: TakeMutPtrFn,
    /// Type information for diagnostics.
    type_name: TypeNameFn,
    /// The inner type identifier.
    type_id: TypeIdFn,
}

unsafe fn noop_drop_impl<T>(_: *const ()) {
    // noop since we have a wrapped pointer that doesn't need to be
    // dropped.
}

fn as_ptr_impl<T>(this: *const (), expected_type: any::TypeId) -> Option<*const ()>
where
    T: any::Any,
{
    if expected_type == any::TypeId::of::<T>() {
        Some(this)
    } else {
        None
    }
}

fn as_mut_ptr_impl<T>(this: *const (), expected_type: any::TypeId) -> Option<*mut ()>
where
    T: any::Any,
{
    if expected_type == any::TypeId::of::<T>() {
        Some(this as *mut ())
    } else {
        None
    }
}

fn unsupported_as_mut<T>(_: *const (), _: any::TypeId) -> Option<*mut ()>
where
    T: any::Any,
{
    None
}