8404cd40a61d8e66c5d675041c1ab1e609deb66a
[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 as StdAny;
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 }
45
46 #[allow(missing_docs)] // Bogus warning (it’s not public outside the crate), ☹
47 pub trait UncheckedAnyExt: Any {
48 unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T;
49 unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T;
50 unsafe fn downcast_unchecked<T: Any>(self: Box<Self>) -> Box<T>;
51 }
52
53 #[doc(hidden)]
54 /// A trait for the conversion of an object into a boxed trait object.
55 pub trait IntoBox<A: ?Sized + UncheckedAnyExt>: Any {
56 /// Convert self into the appropriate boxed form.
57 fn into_box(self) -> Box<A>;
58 }
59
60 macro_rules! implement {
61 ($base:ident, $(+ $bounds:ident)*) => {
62 impl fmt::Debug for dyn $base $(+ $bounds)* {
63 #[inline]
64 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65 f.pad(stringify!(dyn $base $(+ $bounds)*))
66 }
67 }
68
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 /// A type to emulate dynamic typing.
96 ///
97 /// Every type with no non-`'static` references implements `Any`.
98 /// See the [`std::any` documentation](https://doc.rust-lang.org/std/any/index.html) for
99 /// more details on `Any` in general.
100 ///
101 /// This trait is not `std::any::Any` but rather a type extending that for this library’s
102 /// purposes so that it can be combined with marker traits like
103 /// <code><a class=trait title=core::marker::Send
104 /// href=http://doc.rust-lang.org/std/marker/trait.Send.html>Send</a></code> and
105 /// <code><a class=trait title=core::marker::Sync
106 /// href=http://doc.rust-lang.org/std/marker/trait.Sync.html>Sync</a></code>.
107 ///
108 /// See also [`CloneAny`](trait.CloneAny.html) for a cloneable version of this trait.
109 pub trait Any: StdAny { }
110 impl<T: StdAny> Any for T { }
111 implement!(Any,);
112 implement!(Any, + Send);
113 implement!(Any, + Sync);
114 implement!(Any, + Send + Sync);
115
116 /// A type to emulate dynamic typing with cloning.
117 ///
118 /// Every type with no non-`'static` references that implements `Clone` implements `Any`.
119 /// See the [`std::any` documentation](https://doc.rust-lang.org/std/any/index.html) for
120 /// more details on `Any` in general.
121 ///
122 /// This trait is not `std::any::Any` but rather a type extending that for this library’s
123 /// purposes so that it can be combined with marker traits like
124 /// <code><a class=trait title=core::marker::Send
125 /// href=http://doc.rust-lang.org/std/marker/trait.Send.html>Send</a></code> and
126 /// <code><a class=trait title=core::marker::Sync
127 /// href=http://doc.rust-lang.org/std/marker/trait.Sync.html>Sync</a></code>.
128 ///
129 /// See also [`Any`](trait.Any.html) for a version without the `Clone` requirement.
130 pub trait CloneAny: Any + CloneToAny { }
131 impl<T: StdAny + Clone> CloneAny for T { }
132 implement!(CloneAny,);
133 implement!(CloneAny, + Send);
134 implement!(CloneAny, + Sync);
135 implement!(CloneAny, + Send + Sync);
136 impl_clone!(dyn CloneAny);
137 impl_clone!(dyn CloneAny + Send);
138 impl_clone!(dyn CloneAny + Sync);
139 impl_clone!(dyn CloneAny + Send + Sync);