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