453d7dd9fca5604f602a4426e147e32e0429b626
[anymap] / src / any.rs
1 //! The different types of `Any` for use in a map.
2 //!
3 //! This stuff is all based on `std::any`, but goes a little further, with `CloneAny` being a
4 //! cloneable `Any` and with the `Send` and `Sync` bounds possible on both `Any` and `CloneAny`.
5
6 use std::fmt;
7 use std::any::Any;
8
9 #[doc(hidden)]
10 pub trait CloneToAny {
11 /// Clone `self` into a new `Box<dyn CloneAny>` object.
12 fn clone_to_any(&self) -> Box<dyn CloneAny>;
13 }
14
15 impl<T: Any + Clone> CloneToAny for T {
16 #[inline]
17 fn clone_to_any(&self) -> Box<dyn CloneAny> {
18 Box::new(self.clone())
19 }
20 }
21
22 macro_rules! impl_clone {
23 ($t:ty) => {
24 impl Clone for Box<$t> {
25 #[inline]
26 fn clone(&self) -> Box<$t> {
27 // SAFETY: this dance is to reapply any Send/Sync marker. I’m not happy about this
28 // approach, given that I used to do it in safe code, but then came a dodgy
29 // future-compatibility warning where_clauses_object_safety, which is spurious for
30 // auto traits but still super annoying (future-compatibility lints seem to mean
31 // your bin crate needs a corresponding allow!). Although I explained my plight¹
32 // and it was all explained and agreed upon, no action has been taken. So I finally
33 // caved and worked around it by doing it this way, which matches what’s done for
34 // std::any², so it’s probably not *too* bad.
35 //
36 // ¹ https://github.com/rust-lang/rust/issues/51443#issuecomment-421988013
37 // ² https://github.com/rust-lang/rust/blob/e7825f2b690c9a0d21b6f6d84c404bb53b151b38/library/alloc/src/boxed.rs#L1613-L1616
38 let clone: Box<dyn CloneAny> = (**self).clone_to_any();
39 let raw: *mut dyn CloneAny = Box::into_raw(clone);
40 unsafe { Box::from_raw(raw as *mut $t) }
41 }
42 }
43
44 impl fmt::Debug for $t {
45 #[inline]
46 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47 f.pad(stringify!($t))
48 }
49 }
50 }
51 }
52
53 #[allow(missing_docs)] // Bogus warning (it’s not public outside the crate), ☹
54 pub trait UncheckedAnyExt: Any {
55 unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T;
56 unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T;
57 unsafe fn downcast_unchecked<T: Any>(self: Box<Self>) -> Box<T>;
58 }
59
60 #[doc(hidden)]
61 /// A trait for the conversion of an object into a boxed trait object.
62 pub trait IntoBox<A: ?Sized + UncheckedAnyExt>: Any {
63 /// Convert self into the appropriate boxed form.
64 fn into_box(self) -> Box<A>;
65 }
66
67 macro_rules! implement {
68 ($base:ident, $(+ $bounds:ident)*) => {
69 impl UncheckedAnyExt for dyn $base $(+ $bounds)* {
70 #[inline]
71 unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T {
72 &*(self as *const Self as *const T)
73 }
74
75 #[inline]
76 unsafe fn downcast_mut_unchecked<T: 'static>(&mut self) -> &mut T {
77 &mut *(self as *mut Self as *mut T)
78 }
79
80 #[inline]
81 unsafe fn downcast_unchecked<T: 'static>(self: Box<Self>) -> Box<T> {
82 Box::from_raw(Box::into_raw(self) as *mut T)
83 }
84 }
85
86 impl<T: $base $(+ $bounds)*> IntoBox<dyn $base $(+ $bounds)*> for T {
87 #[inline]
88 fn into_box(self) -> Box<dyn $base $(+ $bounds)*> {
89 Box::new(self)
90 }
91 }
92 }
93 }
94
95 implement!(Any,);
96 implement!(Any, + Send);
97 implement!(Any, + Send + Sync);
98
99 /// [`Any`], but with cloning.
100 ///
101 /// Every type with no non-`'static` references that implements `Clone` implements `CloneAny`.
102 /// See [`std::any`] for more details on `Any` in general.
103 pub trait CloneAny: Any + CloneToAny { }
104 impl<T: Any + Clone> CloneAny for T { }
105 implement!(CloneAny,);
106 implement!(CloneAny, + Send);
107 implement!(CloneAny, + Send + Sync);
108 impl_clone!(dyn CloneAny);
109 impl_clone!(dyn CloneAny + Send);
110 impl_clone!(dyn CloneAny + Send + Sync);