hv-alchemy 0.1.0

Heavy Alchemy - the black arts of transmutation, wrapped for your safe usage and enjoyment
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
//! Heavy Alchemy - The black arts of transmutation, wrapped for your safe usage and enjoyment.
//!
//! Functionality for dynamically examining and manipulating `dyn Trait` objects. This is done
//! mainly through [`TypeTable`], which is a bit like a superpowered [`TypeId`](core::any::TypeId)
//! that also allows you to ask whether the type it pertains to implements some object safe trait.
//! If you can write it as a `dyn Trait`, then you can try to get a [`DynVtable`] corresponding to
//! that trait's implementation for the type the [`TypeTable`] corresponds to.
//!
//! A few basic relationships:
//! - [`TypeTable`] => one per type, potentially many [`DynVtable`]s per [`TypeTable`]
//!     - A single [`TypeTable`] provides all the information necessary to dynamically allocate,
//!       deallocate, drop, and manipulate the type it corresponds to.
//! - [`Type`] => just a wrapper around [`TypeTable`] that adds the type, for use in dynamically
//!   dispatching over [`Type<T>`]. Useful for when you want to represent the type of an object in a
//!   way that can be `Box<dyn Any>` or `Box<dyn AlchemicalAny>`'d.
//! - [`DynVtable`] => at most one per pair of types (object-safe trait `dyn` type, implementor
//!   type.) Contains the necessary metadata to reconstruct a `*const` or `*mut dyn Trait` for the
//!   object-safe trait type it pertains to, pointing to the implementor type it pertains to.
//!
//! Non-object-safe traits can also be represented using this crate, but they have to be done
//! through blanket-impl'd object-safe traits. For a couple of builtin examples, the [`Clone`] trait
//! is represented through [`CloneProxy`], and the [`Copy`] trait is represented through
//! [`CopyProxy`]. Although you cannot directly see a type as `Clone` or `Copy` through its
//! `DynVtable`, you can still do equivalent things (and make use of the consequences of a type
//! being `Clone` or `Copy`.) Also see [`try_clone`](crate::AlchemicalAnyExt::try_clone) and
//! [`try_copy`](crate::AlchemicalAnyExt::try_copy).
//!
//! # Traits
//!
//! Several traits are used to safely represent the transmutations used inside this crate. The two
//! most important ones, which govern whether or not a type can be seen as a `dyn Trait` for some
//! `Trait`, are [`Alchemy`] and [`Alchemical`]. The [`Alchemy`] trait represents a `dyn Trait`
//! type/an object-safe trait, while [`Alchemical<U>`] is blanket-implemented for all types which
//! implement and can be converted to `dyn` objects of some trait `U` (which is a `dyn Trait` type.)
//!
//! There are also a handful of other convenient traits included:
//! - [`AlchemicalAny`] is a powered-up version of [`Any`] which allows for easily fetching the
//!   corresponding [`TypeTable`] for a type.
//! - [`AlchemicalAnyExt`] is an extension trait implemented on all [`AlchemicalAny`] types which
//!   adds downcasting, "dyncasting" (casting to trait objects), fallible cloning/copying, and more.
//! - [`CloneProxy`] is an object-safe [`Clone`] abstraction which can allow for cloning boxed [`dyn
//!   AlchemicalAny`](AlchemicalAny) objects.
//! - [`CopyProxy`] is an object-safe [`Copy`] abstraction which can allow for copying
//!   [`AlchemicalAny`] objects.
//!
//! # Caveats
//!
//! In order for an `TypeTable` to be useful with respect to some object-safe trait `U` implemented
//! for some type `T`, that trait's impl for `T` has to be registered with the global static
//! registry, which is initialized at program runtime. There are a number of ways to do this, but
//! the most convenient is [`Type::add`] (and also the related `mark_copy` and `mark_clone`) traits.
//! It's always a good idea to add the copy/clone markings and also `dyn Send` and `dyn Sync` if
//! they can be applied! Note that it is impossible to add a trait which is unimplemented by `T`, so
//! you don't have to worry about causing unsafety or anything with such. This library should
//! (unless some soundness bug has escaped my notice) be completely safe as long as it is kept to
//! its safe API.

#![no_std]
#![feature(ptr_metadata, unsize, arbitrary_self_types)]
#![warn(missing_docs)]

extern crate alloc;

use alloc::boxed::Box;
use core::{
    alloc::Layout,
    any::{Any, TypeId},
    fmt,
    hash::Hash,
    marker::{PhantomData, Unsize},
    ptr::{DynMetadata, Pointee},
    sync::atomic::{AtomicPtr, Ordering},
};
use hashbrown::{HashMap, HashSet};
use hv_atom::AtomSetOnce;
use hv_cell::AtomicRefCell;
use lazy_static::lazy_static;
use spin::RwLock;

/// An object-safe trait which contains a method that will only be called if its `T: Send` bound can
/// be proven for the argument type `T`.
pub trait WithSend<T: ?Sized> {
    /// Called if it can be dynamically proven that `T: Send`.
    fn with_send(&mut self)
    where
        T: Send;
}

/// An object-safe trait which contains a method that will only be called if its `T: Sync` bound can
/// be proven for the argument type `T`.
pub trait WithSync<T: ?Sized> {
    /// Called if it can be dynamically proven that `T: Sync`.
    fn with_sync(&mut self)
    where
        T: Sync;
}

/// An object-safe trait which contains a method that will only be called if its `T: Copy` bound can
/// be proven for the argument type `T`.
pub trait WithCopy<T: ?Sized> {
    /// Called if it can be dynamically proven that `T: Copy`.
    fn with_copy(&mut self)
    where
        T: Copy;
}

/// An object-safe trait which contains a method that will only be called if its `T: Clone` bound
/// can be proven for the argument type `T`.
pub trait WithClone<T: ?Sized> {
    /// Called if it can be dynamically proven that `T: Clone`.
    fn with_clone(&mut self)
    where
        T: Clone;
}

/// Convenience function for getting the [`Type`] for some `T`.
pub fn of<T: 'static>() -> Type<T> {
    Type::of()
}

/// Convenience function for marking some `T` as being convertible into some `U`.
pub fn convertible<T: 'static, U: 'static + From<T>>() {
    of::<T>().add_into::<U>();
    of::<U>().add_from::<T>();
}

fn typed<T: 'static>() -> Type<T> {
    Type::new()
}

macro_rules! add_types {
    ($m:ident, $closure:expr; $($t:ty),*) => {{
        $({
            let t = <Type<$t>>::new();
            $closure(t);
            $m.insert(TypeId::of::<$t>(), t.as_untyped());
        })*
    }}
}

fn make_registry() -> HashMap<TypeId, &'static TypeTable> {
    fn smart_pointers<T: 'static>(_: Type<T>) {
        use alloc::{
            rc::{Rc, Weak as RcWeak},
            sync::{Arc, Weak as ArcWeak},
        };

        typed::<Rc<T>>().add_clone();
        typed::<RcWeak<T>>().add_clone();
        typed::<Arc<T>>().add_clone();
        typed::<ArcWeak<T>>().add_clone();
        typed::<&'static T>().add_clone().add_copy();
    }

    fn wrappers<T: 'static>(_: Type<T>) {
        smart_pointers::<T>(typed());
        smart_pointers::<core::cell::RefCell<T>>(typed());
        smart_pointers::<AtomicRefCell<T>>(typed());
    }

    fn primitive<T: 'static>(t: Type<T>)
    where
        T: Clone
            + Copy
            + PartialEq
            + Eq
            + PartialOrd
            + Ord
            + Hash
            + fmt::Debug
            + fmt::Display
            + Send
            + Sync,
    {
        t.add_clone().add_copy().add_send().add_sync();
        wrappers(t);
    }

    let mut m = HashMap::new();

    // Primitive types
    add_types! {m,
        primitive;

        // unsigned integers
        u8, u16, u32, u64, u128, usize,

        // signed integers
        i8, i16, i32, i64, i128, isize,

        // string slice
        &'static str
    };

    // Stdlib types

    m
}

lazy_static! {
    static ref ALCHEMY_TABLE_REGISTRY: RwLock<HashMap<TypeId, &'static TypeTable>> =
        RwLock::new(make_registry());
    static ref VALID_ALCHEMY_TABLES: RwLock<HashSet<usize>> = RwLock::new(HashSet::new());
}

/// An object-safe clone trait. Useful to have around as a marker for when a type is [`Clone`], and
/// for easily/efficiently performing the clone.
pub trait CloneProxy {
    #[doc(hidden)]
    unsafe fn clone_into_ptr(&self, ptr: *mut u8);

    /// Get a function usable to clone `Self` without dereferencing the `self` pointer or statically
    /// enforcing `Clone`, and without referencing the actual types involved (so that it is
    /// object-safe.)
    ///
    /// # Safety
    ///
    /// *This is safe to call on a null pointer, as it must never dereference `self` and acts
    /// strictly as a vtable lookup.*
    ///
    /// This should later be cast to a function pointer of the type `fn(&Self) ->
    /// Self`, and is unsafe to use otherwise.
    #[doc(hidden)]
    unsafe fn clone_fn(self: *const Self) -> fn();
}

impl<T: Clone> CloneProxy for T {
    unsafe fn clone_into_ptr(&self, ptr: *mut u8) {
        (&mut *ptr.cast::<T>()).clone_from(self);
    }

    unsafe fn clone_fn(self: *const T) -> fn() {
        core::mem::transmute(<T as Clone>::clone as fn(&T) -> T)
    }
}

/// An object-safe copy trait. Useful to have around as a marker for when a type is [`Copy`], and
/// for easily/efficiently performing the copy.
pub trait CopyProxy {
    #[doc(hidden)]
    unsafe fn copy_into_ptr(&self, ptr: *mut u8);
}

impl<T: Copy> CopyProxy for T {
    unsafe fn copy_into_ptr(&self, ptr: *mut u8) {
        *ptr.cast::<T>() = *self;
    }
}

static_assertions::assert_obj_safe!(CloneProxy, CopyProxy);

/// An object-safe `From` trait (`Self` from `T`), for converting some `T` into a dynamically-known
/// type for which you only have its [`TypeTable`].
///
/// Usually, you will want [`IntoProxy`] instead; `FromProxy` specializes in allowing you to convert
/// a statically known type, into a dynamically known type. Usually you have a statically known
/// "destination" type and a dynamically known "source" type, which is what [`IntoProxy`] deals
/// with.
pub trait FromProxy<T> {
    /// Convert a `T` into `Self`.
    ///
    /// # Safety
    ///
    /// `self` must be a valid pointer, and if it contains an already valid `Self`, then that value
    /// will not be dropped; just overwritten. `ptr` must also be a valid pointer to a `T`, and if
    /// `T` is not `Copy`, then the value at `ptr` should be considered moved after this operation.
    unsafe fn convert_from_ptr(self: *mut Self, ptr: *const T);
}

impl<T: From<U>, U> FromProxy<U> for T {
    unsafe fn convert_from_ptr(self: *mut Self, ptr: *const U) {
        core::ptr::write(self, T::from(core::ptr::read(ptr.cast::<U>())));
    }
}

/// An object-safe `Into` trait (`Self` into `T`), for converting some dynamic `Self` type into a
/// statically-known type.
///
/// For conversions from a statically-known type to a dynamically-known type, use [`FromProxy`].
pub trait IntoProxy<T> {
    /// Convert a `Self` into `T`.
    ///
    /// # Safety
    ///
    /// `ptr` must be a valid pointer, and if it contains an already valid `T`, then that value will
    /// not be dropped; just overwritten. `self` must also be a valid pointer, and if `Self` is not
    /// `Copy`, then the value at `ptr` should be considered moved after this operation.
    unsafe fn convert_into_ptr(self: *const Self, ptr: *mut T);
}

impl<T: Into<U>, U> IntoProxy<U> for T {
    unsafe fn convert_into_ptr(self: *const Self, ptr: *mut U) {
        core::ptr::write(ptr.cast::<U>(), T::into(core::ptr::read(self)));
    }
}

static_assertions::assert_obj_safe!(FromProxy<()>);

/// An auto-implemented marker trait indicating that a type is a subtype of/is convertible to some
/// type `U`. In most cases, this means that `Self` implements some `Trait` such that `U` is `dyn
/// Trait` and a reference to `Self` can be converted to a reference to `dyn Trait`/`U`.
pub trait Alchemical<U: ?Sized + Alchemy>: Any {
    #[doc(hidden)]
    fn cast_ptr(this: *const Self) -> *const U;
    #[doc(hidden)]
    fn cast_mut_ptr(this: *mut Self) -> *mut U;
}

impl<T: ?Sized + Any + Unsize<U>, U: ?Sized + Alchemy> Alchemical<U> for T {
    #[inline]
    fn cast_ptr(this: *const Self) -> *const U {
        this as *const U
    }

    #[inline]
    fn cast_mut_ptr(this: *mut Self) -> *mut U {
        this as *mut U
    }
}

/// An auto-implemented marker trait indicating that a type is a trait object type.
pub trait Alchemy: Any + Pointee<Metadata = DynMetadata<Self>> {}
impl<T> Alchemy for T where T: ?Sized + Any + Pointee<Metadata = DynMetadata<T>> {}

static_assertions::assert_impl_all!(dyn Any: Alchemy);
static_assertions::assert_impl_all!((): CloneProxy);

/// A table of information about a type, effectively acting as a superpowered [`TypeId`].
pub struct TypeTable {
    /// The `TypeId` of this table's type.
    pub id: TypeId,
    /// The layout of this table's type, for allocating/deallocating/copying around.
    pub layout: Layout,
    /// A type-erased destructor for this table's type, which drops a value of that type in place.
    pub drop: unsafe fn(*mut u8),
    /// The string name for this table's type, for debug usage.
    pub type_name: &'static str,

    // Private because of interior mutability, so we want to only expose iteration (and insertion)
    // from the outside.
    vtables: RwLock<HashMap<TypeId, &'static DynVtable>>,

    // Always-there vtables, not stored as part of the monotonic list
    pub(crate) alchemical_any: DynVtable,
    pub(crate) alchemical_clone: AtomSetOnce<&'static DynVtable>,
    pub(crate) alchemical_copy: AtomSetOnce<&'static DynVtable>,
    pub(crate) send: AtomSetOnce<&'static DynVtable>,
    pub(crate) sync: AtomSetOnce<&'static DynVtable>,

    pub(crate) send_shim: AtomicPtr<()>,
    pub(crate) sync_shim: AtomicPtr<()>,
    pub(crate) clone_shim: AtomicPtr<()>,
    pub(crate) copy_shim: AtomicPtr<()>,
}

impl TypeTable {
    fn new<T: 'static>() -> &'static Self {
        unsafe fn drop_ptr<T>(x: *mut u8) {
            x.cast::<T>().drop_in_place()
        }

        let this = Self {
            id: TypeId::of::<T>(),
            layout: Layout::new::<T>(),
            drop: drop_ptr::<T>,
            type_name: core::any::type_name::<T>(),
            vtables: RwLock::new(HashMap::new()),
            alchemical_any: DynVtable::new::<T, dyn AlchemicalAny>(core::ptr::null()),
            alchemical_clone: AtomSetOnce::empty(),
            alchemical_copy: AtomSetOnce::empty(),
            send: AtomSetOnce::empty(),
            sync: AtomSetOnce::empty(),
            send_shim: AtomicPtr::new(core::ptr::null_mut()),
            sync_shim: AtomicPtr::new(core::ptr::null_mut()),
            clone_shim: AtomicPtr::new(core::ptr::null_mut()),
            copy_shim: AtomicPtr::new(core::ptr::null_mut()),
        };

        Box::leak(Box::new(this))
    }

    /// Get the alchemy table for some type `T`. This function will always return the same
    /// `&'static` for the same type `T`.
    pub fn of<T: 'static>() -> &'static Self {
        let type_id = TypeId::of::<T>();
        if let Some(table) = ALCHEMY_TABLE_REGISTRY.read().get(&type_id).copied() {
            return table;
        }

        ALCHEMY_TABLE_REGISTRY
            .write()
            .entry(type_id)
            .or_insert_with(|| {
                let eternal = Self::new::<T>();
                VALID_ALCHEMY_TABLES
                    .write()
                    .insert(eternal as *const _ as usize);
                eternal
            })
    }

    /// Check whether the type implements [`Clone`] (through [`CloneProxy`]).
    pub fn is_clone(&self) -> bool {
        !self.alchemical_clone.is_none()
    }

    /// Check whether the type implements [`Copy`] (through [`CopyProxy`]).
    pub fn is_copy(&self) -> bool {
        !self.alchemical_copy.is_none()
    }

    /// Check whether the type implements some object-safe trait representable as `dyn Trait` type
    /// `U`.
    pub fn is<U: ?Sized + Alchemy>(&self) -> bool {
        self.get::<U>().is_some()
    }

    /// Get the [`DynVtable`] corresponding to an object-safe trait `U`, if this type has an
    /// implementation registered.
    pub fn get<U>(&self) -> Option<&DynVtable>
    where
        U: ?Sized + Alchemy,
    {
        let id = TypeId::of::<U>();
        if id == TypeId::of::<dyn AlchemicalAny>() {
            return Some(&self.alchemical_any);
        } else if id == TypeId::of::<dyn CloneProxy>() {
            return self.alchemical_clone.get();
        } else if id == TypeId::of::<dyn CopyProxy>() {
            return self.alchemical_copy.get();
        } else if id == TypeId::of::<dyn Send>() {
            return self.send.get();
        } else if id == TypeId::of::<dyn Sync>() {
            return self.sync.get();
        }

        self.vtables.read().get(&id).copied()
    }

    /// Get the [`DynVtable`] corresponding to an object-safe trait `U`'s implementation for some
    /// type `T` (which is also the type that this `TypeTable` corresponds to), and insert the
    /// implementation into the `TypeTable` if it's not already present.
    ///
    /// Unlike [`TypeTable::get_or_insert_sized`], this function can deal with an unsized `T`,
    /// but needs a pointer to convert in order to extract a vtable.
    ///
    /// Will panic if `T` is not the type this `TypeTable` corresponds to.
    pub fn get_or_insert<T, U>(&self, ptr: *const T) -> &DynVtable
    where
        T: ?Sized + Alchemical<U>,
        U: ?Sized + Alchemy,
    {
        assert_eq!(TypeId::of::<T>(), self.id);
        match self.get::<U>() {
            Some(table) => table,
            None => {
                let id = TypeId::of::<U>();
                let vtable = Box::leak(Box::new(DynVtable::new::<T, U>(ptr)));
                if id == TypeId::of::<dyn CloneProxy>() {
                    panic!("use `add_clone` instead of explicitly adding `dyn CloneProxy`!");
                } else if id == TypeId::of::<dyn CopyProxy>() {
                    panic!("use `add_copy` instead of explicitly adding `dyn CopyProxy`!");
                } else if id == TypeId::of::<dyn Send>() {
                    panic!("use `add_send` instead of explicitly adding `dyn Send`!");
                } else if id == TypeId::of::<dyn Sync>() {
                    panic!("use `add_sync` instead of explicitly adding `dyn Sync`!");
                } else {
                    self.vtables.write().insert(id, vtable);
                }

                vtable
            }
        }
    }

    /// Get the [`DynVtable`] corresponding to an object-safe trait `U`'s implementation for some
    /// type `T` (where `T` is also the type this `TypeTable` corresponds to.) If `T` is
    /// `?Sized`, you'll need to use [`TypeTable::get_or_insert`] instead.
    ///
    /// Will panic if `T` is not the type for this table.
    pub fn get_or_insert_sized<T, U>(&self) -> &DynVtable
    where
        T: Alchemical<U>,
        U: ?Sized + Alchemy,
    {
        self.get_or_insert::<T, U>(core::ptr::null())
    }

    /// Mark that this table's type (`T`) is [`Clone`].
    ///
    /// Panics if `T` is not the type of this table.
    pub fn add_clone<T: Clone + 'static>(&self) -> &Self {
        fn clone_shim<T: Clone>(with: &mut dyn WithClone<T>) {
            with.with_clone();
        }

        assert_eq!(TypeId::of::<T>(), self.id);
        self.alchemical_clone.set_if_none(Box::leak(Box::new(
            DynVtable::new::<T, dyn CloneProxy>(core::ptr::null()),
        )));
        let shim_ptr = clone_shim::<T> as *const () as *mut ();
        let _ = self.clone_shim.compare_exchange(
            core::ptr::null_mut(),
            shim_ptr,
            Ordering::AcqRel,
            Ordering::Acquire,
        );
        self
    }

    /// Mark that this table's type (`T`) is [`Copy`].
    ///
    /// Panics if `T` is not the type of this table.
    pub fn add_copy<T: Copy + 'static>(&self) -> &Self {
        fn copy_shim<T: Copy>(with: &mut dyn WithCopy<T>) {
            with.with_copy();
        }

        assert_eq!(TypeId::of::<T>(), self.id);
        self.alchemical_copy
            .set_if_none(Box::leak(Box::new(DynVtable::new::<T, dyn CopyProxy>(
                core::ptr::null(),
            ))));

        let shim_ptr = copy_shim::<T> as *const () as *mut ();
        let _ = self.copy_shim.compare_exchange(
            core::ptr::null_mut(),
            shim_ptr,
            Ordering::AcqRel,
            Ordering::Acquire,
        );
        self
    }

    /// Mark that this table's type (`T`) is [`Send`].
    ///
    /// Panics if `T` is not the type of this table.
    pub fn add_send<T: Send + 'static>(&self) -> &Self {
        self.add_send_with::<T>(core::ptr::null())
    }

    /// Mark that this table's type (`T`) is [`Sync`].
    ///
    /// Panics if `T` is not the type of this table.
    pub fn add_sync<T: Sync + 'static>(&self) -> &Self {
        self.add_sync_with::<T>(core::ptr::null())
    }

    /// Mark that this table's type (`T`) is [`Send`], given a pointer to cast and extract the
    /// vtable from.
    ///
    /// Panics if `T` is not the type of this table.
    pub fn add_send_with<T>(&self, ptr: *const T) -> &Self
    where
        T: ?Sized + Send + Alchemical<dyn Send> + 'static,
    {
        fn send_shim<T: ?Sized + Send>(with: &mut dyn WithSend<T>) {
            with.with_send();
        }

        assert_eq!(TypeId::of::<T>(), self.id);
        self.send
            .set_if_none(Box::leak(Box::new(DynVtable::new::<T, dyn Send>(ptr))));

        let shim_ptr = send_shim::<T> as *const () as *mut ();
        let _ = self.send_shim.compare_exchange(
            core::ptr::null_mut(),
            shim_ptr,
            Ordering::AcqRel,
            Ordering::Acquire,
        );

        self
    }

    /// Mark that this table's type (`T`) is [`Sync`], given a pointer to cast and extract the
    /// vtable from.
    ///
    /// Panics if `T` is not the type of this table.
    pub fn add_sync_with<T>(&self, ptr: *const T) -> &Self
    where
        T: ?Sized + Sync + Alchemical<dyn Sync> + 'static,
    {
        fn sync_shim<T: ?Sized + Sync>(with: &mut dyn WithSync<T>) {
            with.with_sync();
        }

        assert_eq!(TypeId::of::<T>(), self.id);
        self.sync
            .set_if_none(Box::leak(Box::new(DynVtable::new::<T, dyn Sync>(ptr))));

        let shim_ptr = sync_shim::<T> as *const () as *mut ();
        let _ = self.sync_shim.compare_exchange(
            core::ptr::null_mut(),
            shim_ptr,
            Ordering::AcqRel,
            Ordering::Acquire,
        );

        self
    }

    /// Register the vtable for some object-safe trait `U`'s implementation for `T`, the type of
    /// this `TypeTable`. If `T` is `?Sized`, use [`TypeTable::add_with`] (which will need a
    /// pointer.)
    ///
    /// Panics if `T` is not the type of this table.
    pub fn add<T, U>(&self) -> &Self
    where
        T: Alchemical<U>,
        U: ?Sized + Alchemy,
    {
        self.get_or_insert::<T, U>(core::ptr::null());
        self
    }

    /// Register the vtable for some object-safe trait `U`'s implementation for `T`, the type of
    /// this `TypeTable`.
    ///
    /// Panics if `T` is not the type of this table.
    pub fn add_with<T, U>(&self, ptr: *const T) -> &Self
    where
        T: ?Sized + Alchemical<U>,
        U: ?Sized + Alchemy,
    {
        self.get_or_insert::<T, U>(ptr);
        self
    }

    /// Convert this `&'static TypeTable` to a raw pointer.
    pub fn to_ptr(&'static self) -> *const TypeTable {
        self
    }

    /// Get an `&'static TypeTable` back from a raw pointer, ensuring it is valid (will return
    /// `None` if the pointer does not correspond to a previously fetched `TypeTable`.)
    pub fn from_ptr(ptr: *const TypeTable) -> Option<&'static TypeTable> {
        VALID_ALCHEMY_TABLES
            .read()
            .contains(&(ptr as usize))
            .then(|| unsafe { &*ptr })
    }
}

/// A statically-typed wrapper around an [`TypeTable`] which corresponds to the parameter type
/// `T`. Most of the time you'll want to use this when interfacing w/ `TypeTable`s, because it
/// lets you call almost every method on [`TypeTable`] without having to specify `T` every time.
pub struct Type<T: ?Sized>(&'static TypeTable, PhantomData<fn(T)>);

impl<T: ?Sized> Clone for Type<T> {
    fn clone(&self) -> Self {
        Self(self.0, self.1)
    }
}

impl<T: ?Sized> Copy for Type<T> {}

impl<T: ?Sized + 'static> Type<T> {
    fn new() -> Self
    where
        T: Sized,
    {
        Self(TypeTable::new::<T>(), PhantomData)
    }

    /// Get the typed alchemy table corresponding to `T`.
    pub fn of() -> Self
    where
        T: Sized,
    {
        Self(TypeTable::of::<T>(), PhantomData)
    }

    /// Check whether the type implements some object-safe trait representable as `dyn Trait` type
    /// `U`.
    pub fn is<U: ?Sized + Alchemy>(&self) -> bool {
        self.0.is::<U>()
    }

    /// Get the vtable of `T`'s implementation of some object-safe trait `U`, if it exists in the
    /// registry.
    pub fn get<U>(self) -> Option<&'static DynVtable>
    where
        U: ?Sized + Alchemy,
    {
        self.0.get::<U>()
    }

    /// Get the vtable of `T`'s implementation of some object-safe trait `U`. Requires a pointer to
    /// convert and extract a vtable from.
    pub fn get_or_insert<U>(self, ptr: *const T) -> &'static DynVtable
    where
        T: Alchemical<U>,
        U: ?Sized + Alchemy,
    {
        self.0.get_or_insert::<T, U>(ptr)
    }

    /// Get the vtable of `T`'s implementation of some object-safe trait `U`. Requires `T: Sized`;
    /// if your `T` is not `Sized`, use [`Type::get_or_insert`] and provide a pointer
    /// to extract from.
    pub fn get_or_insert_sized<U>(self) -> &'static DynVtable
    where
        T: Sized + Alchemical<U>,
        U: ?Sized + Alchemy,
    {
        self.0.get_or_insert_sized::<T, U>()
    }

    /// Register `T` as [`Clone`].
    pub fn add_clone(self) -> Self
    where
        T: Clone,
    {
        self.0.add_clone::<T>();
        self
    }

    /// Register `T` as [`Copy`].
    pub fn add_copy(self) -> Self
    where
        T: Copy,
    {
        self.0.add_copy::<T>();
        self
    }

    /// Register `T` as [`Send`].
    pub fn add_send(self) -> Self
    where
        T: Send + Sized,
    {
        self.0.add_send::<T>();
        self
    }

    /// Register `T` as [`Sync`].
    pub fn add_sync(self) -> Self
    where
        T: Sync + Sized,
    {
        self.0.add_sync::<T>();
        self
    }

    /// Register `T`'s implementation of some object-safe trait `U`, given a pointer to extract
    /// from, and return `Self` for convenience when adding multiple traits.
    pub fn add_with<U>(self, ptr: *const T) -> Self
    where
        T: Alchemical<U>,
        U: ?Sized + Alchemy,
    {
        self.0.add_with::<T, U>(ptr);
        self
    }

    /// Register `T`'s implementation of some object-safe trait `U`, and return `Self` for
    /// convenience when adding multiple traits.
    pub fn add<U>(self) -> Self
    where
        T: Sized + Alchemical<U>,
        U: ?Sized + Alchemy,
    {
        self.0.add::<T, U>();
        self
    }

    /// Get the [`&'static TypeTable`](TypeTable) underlying this `Type<T>`.
    pub fn as_untyped(self) -> &'static TypeTable {
        self.0
    }

    /// Run a function in a shim if `T: Copy`, with a static `Copy` bound on the shim (but not on
    /// this function.)
    pub fn try_prove_copy(self, with: &mut dyn WithCopy<T>) {
        let ptr = self.0.copy_shim.load(Ordering::Acquire);

        if !ptr.is_null() {
            let f = unsafe { core::mem::transmute::<*mut (), fn(&mut dyn WithCopy<T>)>(ptr) };
            f(with);
        }
    }

    /// Run a function in a shim if `T: Clone`, with a static `Clone` bound on the shim (but not on
    /// this function.)
    pub fn try_prove_clone(self, with: &mut dyn WithClone<T>) {
        let ptr = self.0.clone_shim.load(Ordering::Acquire);

        if !ptr.is_null() {
            let f = unsafe { core::mem::transmute::<*mut (), fn(&mut dyn WithClone<T>)>(ptr) };
            f(with);
        }
    }

    /// Run a function in a shim if `T: Send`, with a static `Send` bound on the shim (but not on
    /// this function.)
    pub fn try_prove_send(self, with: &mut dyn WithSend<T>) {
        let ptr = self.0.send_shim.load(Ordering::Acquire);

        if !ptr.is_null() {
            let f = unsafe { core::mem::transmute::<*mut (), fn(&mut dyn WithSend<T>)>(ptr) };
            f(with);
        }
    }

    /// Run a function in a shim if `T: Sync`, with a static `Sync` bound on the shim (but not on
    /// this function.)
    pub fn try_prove_sync(self, with: &mut dyn WithSync<T>) {
        let ptr = self.0.sync_shim.load(Ordering::Acquire);

        if !ptr.is_null() {
            let f = unsafe { core::mem::transmute::<*mut (), fn(&mut dyn WithSync<T>)>(ptr) };
            f(with);
        }
    }
}

impl<T: 'static> Type<T> {
    /// Mark `T` as `From<U>`.
    pub fn add_from<U: 'static>(self) -> Self
    where
        T: From<U>,
    {
        self.add::<dyn FromProxy<U>>()
    }

    /// Mark `T` as `Into<U>`.
    pub fn add_into<U: 'static>(self) -> Self
    where
        T: Into<U>,
    {
        self.add::<dyn IntoProxy<U>>()
    }

    /// Mark `T` as `Into<U>` and mark `U` as `From<T>`.
    pub fn add_conversion_into<U: 'static>(self) -> Self
    where
        U: From<T>,
    {
        of::<U>().add_from::<T>();
        self.add_into::<U>()
    }

    /// Mark `T` as `From<U>` and mark `U` as `Into<T>`.
    pub fn add_conversion_from<U: 'static>(self) -> Self
    where
        T: From<U>,
    {
        of::<U>().add_into::<T>();
        self.add_from::<U>()
    }

    /// Without statically requiring that `T: Clone`, get the function pointer corresponding to `<T
    /// as Clone>::clone`.
    pub fn get_clone(self) -> Option<fn(&T) -> T> {
        let vtable = self.get::<dyn CloneProxy>()?;
        let clone_fn = unsafe {
            let null_obj_ptr = vtable.to_dyn_object_ptr::<dyn CloneProxy>(core::ptr::null());
            // This is safe to call on a null pointer, as it never dereferences the pointer!
            // Effectively a vtable lookup.
            let untyped_clone_fn: fn() = CloneProxy::clone_fn(null_obj_ptr);
            core::mem::transmute::<_, fn(&T) -> T>(untyped_clone_fn)
        };

        Some(clone_fn)
    }
}

/// A vtable for some type `T`'s implementation of some object-safe trait `U`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DynVtable {
    obj_type_id: TypeId,
    dyn_type_id: TypeId,
    metadata: *const (),
}

unsafe impl Send for DynVtable {}
unsafe impl Sync for DynVtable {}

impl DynVtable {
    /// Extract a `DynVtable` from a pointer. No need for the pointer to be valid here; this
    /// function doesn't end up dereferencing the pointer, so if it's just a null pointer, that's
    /// fine. Note that [`std::ptr::null`] does require `T` to be sized, hence why this method takes
    /// a pointer instead of just constructing a null pointer internally.
    pub fn new<T, U>(ptr: *const T) -> Self
    where
        T: ?Sized + Alchemical<U>,
        U: ?Sized + Alchemy,
    {
        let cast_ptr = <T as Alchemical<U>>::cast_ptr(ptr);
        Self {
            obj_type_id: TypeId::of::<T>(),
            dyn_type_id: TypeId::of::<U>(),
            metadata: unsafe {
                let u_metadata = core::ptr::metadata::<U>(cast_ptr);
                core::mem::transmute::<DynMetadata<U>, *const ()>(u_metadata)
            },
        }
    }

    /// Construct a `DynVtable` from its type-erased components.
    ///
    /// # Safety
    ///
    /// `metadata` must be the transmutation or bit-equivalent represeentation of a
    /// [`DynMetadata<U>`] for some `T` where `T: ?Sized + Alchemical<U>` and `U: ?Sized +
    /// Alchemy<T>`, and where `TypeId::of::<T>() == obj_type_id && TypeId::of::<U>() ==
    /// dyn_type_id`.
    pub unsafe fn new_from_parts(
        obj_type_id: TypeId,
        dyn_type_id: TypeId,
        metadata: *const (),
    ) -> Self {
        Self {
            obj_type_id,
            dyn_type_id,
            metadata,
        }
    }

    /// The [`TypeId`] of the type `T` which implements the trait object type `U`.
    pub fn obj_type_id(&self) -> TypeId {
        self.obj_type_id
    }

    /// The [`TypeId`] of the trait object type `U` implemented by the type `T`.
    pub fn dyn_type_id(&self) -> TypeId {
        self.dyn_type_id
    }

    /// Construct a `*const` pointer to the `dyn Trait` object corresponding to this vtable, from an
    /// untyped pointer to a value of the same type as this vtable was created from.
    ///
    /// # Safety
    ///
    /// `object` must be a valid pointer to a value which is of some type `T` where
    /// `TypeId::of::<T>() == self.obj_type_id()`, the type `T` implements `Alchemical<U>` for some
    /// type `U` where `TypeId::of::<U>() == self.dyn_type_id()`, and `U` implements `Alchemy<T>`.
    pub unsafe fn to_dyn_object_ptr<U: Any + ?Sized + Pointee<Metadata = DynMetadata<U>>>(
        &self,
        object: *const (),
    ) -> *const U {
        core::ptr::from_raw_parts::<U>(
            object,
            core::mem::transmute::<*const (), DynMetadata<U>>(self.metadata),
        )
    }

    /// Construct a `*mut` pointer to the `dyn Trait` object corresponding to this vtable, from an
    /// untyped pointer to a value of the same type as this vtable was created from.
    ///
    /// # Safety
    ///
    /// `object` must be a valid pointer to a value which is of some type `T` where
    /// `TypeId::of::<T>() == self.obj_type_id()`, the type `T` implements `Alchemical<U>` for some
    /// type `U` where `TypeId::of::<U>() == self.dyn_type_id()`, and `U` implements `Alchemy<T>`.
    pub unsafe fn to_dyn_object_mut_ptr<U: Any + ?Sized + Pointee<Metadata = DynMetadata<U>>>(
        &self,
        object: *mut (),
    ) -> *mut U {
        core::ptr::from_raw_parts_mut::<U>(
            object,
            core::mem::transmute::<*const (), DynMetadata<U>>(self.metadata),
        )
    }
}

/// A type-erased pointer which knows about some set of vtables belonging to the type of the object
/// it points to.
#[derive(Clone, Copy)]
pub struct AlchemicalPtr {
    data: *mut (),
    table: &'static TypeTable,
}

impl AlchemicalPtr {
    /// Create a new `AlchemicalPtr` from a statically-typed pointer.
    pub fn new<T: Any>(ptr: *mut T) -> Self {
        Self {
            data: ptr.cast(),
            table: TypeTable::of::<T>(),
        }
    }

    /// Get the data pointer of this `AlchemicalPtr`.
    pub fn as_ptr(self) -> *mut () {
        self.data
    }

    /// Get the [`TypeTable`] of the type pointed to by this `AlchemicalPtr`.
    pub fn table(self) -> &'static TypeTable {
        self.table
    }

    /// Equivalent to `AlchemicalPtr::downcast_dyn_ptr::<dyn AlchemicalAny>`, but faster.
    ///
    /// # Safety
    ///
    /// Same safety considerations as [`AlchemicalPtr::downcast_dyn_ptr`].
    pub unsafe fn as_alchemical_any(self) -> *const dyn AlchemicalAny {
        self.table.alchemical_any.to_dyn_object_ptr(self.data)
    }

    /// Equivalent to `AlchemicalPtr::downcast_dyn_mut_ptr::<dyn AlchemicalAny>`, but faster.
    ///
    /// # Safety
    ///
    /// Same safety considerations as [`AlchemicalPtr::downcast_dyn_mut_ptr`].
    pub unsafe fn as_alchemical_any_mut(self) -> *mut dyn AlchemicalAny {
        self.table.alchemical_any.to_dyn_object_mut_ptr(self.data)
    }

    /// Reconstruct an `AlchemicalNonNull` from its raw parts.
    ///
    /// # Safety
    ///
    /// `data` must be a valid pointer, and `table`  must be the `TypeTable` corresponding to the
    /// actual un-erased type that `data` points to.
    pub unsafe fn from_raw_parts(data: *mut (), table: &'static TypeTable) -> Self {
        Self { data, table }
    }

    /// If the vtable corresponding to the type `U` is found in this pointer's `TypeTable`,
    /// construct a "fat" `*const` pointer from it and the data pointer of this `AlchemicalNonNull`.
    pub fn downcast_dyn_ptr<U: ?Sized + Alchemy>(self) -> Option<*const U> {
        self.table
            .get::<U>()
            .map(|vtable| unsafe { vtable.to_dyn_object_ptr::<U>(self.data.cast()) })
    }

    /// If the vtable corresponding to the type `U` is found in this pointer's `TypeTable`,
    /// construct a "fat" `*mut` pointer from it and the data pointer of this `AlchemicalNonNull`.
    pub fn downcast_dyn_mut_ptr<U: ?Sized + Alchemy>(self) -> Option<*mut U> {
        self.table
            .get::<U>()
            .map(|vtable| unsafe { vtable.to_dyn_object_mut_ptr::<U>(self.data.cast()) })
    }

    /// Downcast the pointer to an immutable reference to a given trait object, if it implements it.
    ///
    /// # Safety
    ///
    /// This `AlchemicalNonNull` must point to a valid object of the type registered in its internal
    /// `TypeTable`. In addition, it must be safe to immutably borrow that object. This function
    /// also returns a completely arbitrary lifetime, so be sure that it does not outlive the
    /// pointee.
    pub unsafe fn downcast_dyn_ref<'a, U: ?Sized + Alchemy>(self) -> Option<&'a U> {
        self.table
            .get::<U>()
            .map(|vtable| &*unsafe { vtable.to_dyn_object_ptr::<U>(self.data.cast()) })
    }

    /// Downcast the pointer to a mutable reference to a given trait object, if it implements it.
    ///
    /// # Safety
    ///
    /// This `AlchemicalNonNull` must point to a valid object of the type registered in its internal
    /// `TypeTable`. In addition, it must be safe to mutably borrow that object. This function
    /// also returns a completely arbitrary lifetime, so be sure that it does not outlive the
    /// pointee.
    pub unsafe fn downcast_dyn_mut<'a, U: ?Sized + Alchemy>(self) -> Option<&'a mut U> {
        self.table
            .get::<U>()
            .map(|vtable| &mut *unsafe { vtable.to_dyn_object_mut_ptr::<U>(self.data.cast()) })
    }
}

/// A superpowered version of [`Any`] which provides a [`TypeTable`] rather than a [`TypeId`].
pub trait AlchemicalAny {
    /// Get the alchemy table of the underlying type.
    fn type_table(&self) -> &'static TypeTable;
}

impl<T: Any> AlchemicalAny for T {
    fn type_table(&self) -> &'static TypeTable {
        TypeTable::of::<T>()
    }
}

/// An object-unsafe extension trait which provides
pub trait AlchemicalAnyExt: AlchemicalAny {
    /// Try to cast this `&dyn AlchemicalAny` to some other trait object `U`.
    fn dyncast_ref<U: Alchemy + ?Sized>(&self) -> Option<&U> {
        let type_table = (*self).type_table();
        let downcast_alchemy = type_table.get::<U>()?;
        unsafe { Some(&*downcast_alchemy.to_dyn_object_ptr::<U>((self as *const Self).cast())) }
    }

    /// Try to cast this `&mut dyn AlchemicalAny` to some other trait object `U`.
    fn dyncast_mut<U: Alchemy + ?Sized>(&mut self) -> Option<&mut U> {
        let type_table = (*self).type_table();
        let downcast_alchemy = type_table.get::<U>()?;
        unsafe {
            Some(&mut *downcast_alchemy.to_dyn_object_mut_ptr::<U>((self as *mut Self).cast()))
        }
    }

    /// Try to cast this `Box<dyn AlchemicalAny>` to some other trait object `U`.
    fn dyncast<U: Alchemy + ?Sized>(self: Box<Self>) -> Option<Box<U>> {
        let type_table = (*self).type_table();
        let downcast_alchemy = type_table.get::<U>()?;
        unsafe {
            let ptr = Box::into_raw(self);
            Some(Box::from_raw(
                downcast_alchemy.to_dyn_object_mut_ptr::<U>(ptr as *mut _),
            ))
        }
    }

    /// Try to cast this `&dyn AlchemicalAny` to some type `T`.
    fn downcast_ref<T: Any>(&self) -> Option<&T> {
        let tt = (*self).type_table();
        (tt.id == TypeId::of::<T>()).then(|| unsafe { &*(self as *const _ as *const T) })
    }

    /// Try to cast this `&mut dyn AlchemicalAny` to some type `T`.
    fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> {
        let tt = (*self).type_table();
        (tt.id == TypeId::of::<T>()).then(|| unsafe { &mut *(self as *mut _ as *mut T) })
    }

    /// Try to cast this `Box<dyn AlchemicalAny>` to some type `T`.
    fn downcast<T: Any>(self: Box<Self>) -> Option<Box<T>> {
        let tt = (*self).type_table();
        (tt.id == TypeId::of::<T>())
            .then(|| unsafe { Box::from_raw(Box::into_raw(self) as *mut T) })
    }

    /// Try to copy this value into a `Box<dyn AlchemicalAny>`. If it succeeds, a copy is created
    /// and the original type is not moved (because it implements [`Copy`].)
    fn try_copy(&self) -> Option<Box<dyn AlchemicalAny>> {
        let at = self.type_table();
        let as_alchemical_copy = at.get::<dyn CopyProxy>()?;
        unsafe {
            let ptr = alloc::alloc::alloc(at.layout);
            (*as_alchemical_copy.to_dyn_object_ptr::<dyn CopyProxy>((self as *const Self).cast()))
                .copy_into_ptr(ptr);
            let recast_ptr = at
                .alchemical_any
                .to_dyn_object_mut_ptr::<dyn AlchemicalAny>(ptr as *mut _);
            Some(Box::from_raw(recast_ptr))
        }
    }

    /// Try to clone this value into a `Box<dyn AlchemicalAny>`. If it succeeds, a clone is created
    /// and the original type is not moved (because it implements [`Clone`].)
    fn try_clone(&self) -> Option<Box<dyn AlchemicalAny>> {
        let at = self.type_table();
        let as_alchemical_clone = at.get::<dyn CloneProxy>()?;
        unsafe {
            let ptr = alloc::alloc::alloc(at.layout);
            (*as_alchemical_clone
                .to_dyn_object_ptr::<dyn CloneProxy>((self as *const Self).cast()))
            .clone_into_ptr(ptr);
            let recast_ptr = at
                .alchemical_any
                .to_dyn_object_mut_ptr::<dyn AlchemicalAny>(ptr as *mut _);
            Some(Box::from_raw(recast_ptr))
        }
    }
}

impl<T: AlchemicalAny + ?Sized> AlchemicalAnyExt for T {}

/// Returns true if the values were moved.
///
/// # Safety
///
/// Pointers must be valid and DST should be uninitialized (it will *not* be dropped if it is
/// already initialized and will be treated as uninitialized.)
pub unsafe fn clone_or_move(src: *mut dyn AlchemicalAny, dst: *mut u8) -> bool {
    let table = <dyn AlchemicalAny>::type_table(&*src);
    if let Some(clone_vt) = table.get::<dyn CloneProxy>() {
        clone_vt
            .to_dyn_object_ptr::<dyn CloneProxy>(src as *mut ())
            .clone_into_ptr(dst as *mut u8);
        false
    } else {
        core::ptr::copy_nonoverlapping(src as *const u8, dst as *mut u8, table.layout.size());
        true
    }
}

/// Returns true if the values were moved.
///
/// # Safety
///
/// Pointers must be valid. The will *not* be dropped if it is already initialized. It is safe to
/// use this function with a destination that has a valid vtable but uninitialized data.
pub unsafe fn copy_clone_or_move_to(
    src: *mut dyn AlchemicalAny,
    dst: *mut dyn AlchemicalAny,
) -> bool {
    let table = <dyn AlchemicalAny>::type_table(&*src);
    assert!(core::ptr::eq(table, <dyn AlchemicalAny>::type_table(&*dst)));
    if let Some(copy_vt) = table.get::<dyn CopyProxy>() {
        copy_vt
            .to_dyn_object_ptr::<dyn CopyProxy>(src as *mut ())
            .copy_into_ptr(dst as *mut u8);
        false
    } else if let Some(clone_vt) = table.get::<dyn CloneProxy>() {
        clone_vt
            .to_dyn_object_ptr::<dyn CloneProxy>(src as *mut ())
            .clone_into_ptr(dst as *mut u8);
        false
    } else {
        core::ptr::copy_nonoverlapping(src as *const u8, dst as *mut u8, table.layout.size());
        true
    }
}

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

    #[test]
    fn alchemical_clone() {
        // `mark_clone` is shorthand for `.register_sized::<i32, dyn CloneProxy>` (where
        // `CloneProxy` is an object-safe but very unsafe trait implementing `Clone`-ing into a
        // pointer) and where `register_sized` is shorthand for `.register::<i32, dyn
        // CloneProxy>(core::ptr::null())`
        Type::<i32>::of().add_clone();

        let boxed: Box<dyn AlchemicalAny> = Box::new(5i32);
        let other: Box<dyn AlchemicalAny> = (*boxed).try_clone().unwrap();

        let a = *(*boxed).downcast_ref::<i32>().unwrap();
        let b = *(*other).downcast_ref::<i32>().unwrap();

        assert_eq!(a, b);
    }

    #[test]
    fn alchemical_copy() {
        // `mark_clone` is shorthand for `.register_sized::<i32, dyn CloneProxy>` (where
        // `CloneProxy` is an object-safe but very unsafe trait implementing `Clone`-ing into a
        // pointer) and where `register_sized` is shorthand for `.register::<i32, dyn
        // CloneProxy>(core::ptr::null())`
        Type::<i32>::of().add_copy();

        let boxed: Box<dyn AlchemicalAny> = Box::new(5i32);
        let other: Box<dyn AlchemicalAny> = (*boxed).try_copy().unwrap();

        let a = *(*boxed).downcast_ref::<i32>().unwrap();
        let b = *(*other).downcast_ref::<i32>().unwrap();

        assert_eq!(a, b);
    }
}