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