e9cef36b51bd4a4fbd9b5449db03df4db61ab20b
[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 /// Clone `self` into a new `Box<dyn CloneAny + Send>` object.
15 fn clone_to_any_send(&self) -> Box<dyn CloneAny + Send> where Self: Send;
16
17 /// Clone `self` into a new `Box<dyn CloneAny + Sync>` object.
18 fn clone_to_any_sync(&self) -> Box<dyn CloneAny + Sync> where Self: Sync;
19
20 /// Clone `self` into a new `Box<dyn CloneAny + Send + Sync>` object.
21 fn clone_to_any_send_sync(&self) -> Box<dyn CloneAny + Send + Sync> where Self: Send + Sync;
22 }
23
24 impl<T: Any + Clone> CloneToAny for T {
25 #[inline]
26 fn clone_to_any(&self) -> Box<dyn CloneAny> {
27 Box::new(self.clone())
28 }
29
30 #[inline]
31 fn clone_to_any_send(&self) -> Box<dyn CloneAny + Send> where Self: Send {
32 Box::new(self.clone())
33 }
34
35 #[inline]
36 fn clone_to_any_sync(&self) -> Box<dyn CloneAny + Sync> where Self: Sync {
37 Box::new(self.clone())
38 }
39
40 #[inline]
41 fn clone_to_any_send_sync(&self) -> Box<dyn CloneAny + Send + Sync> where Self: Send + Sync {
42 Box::new(self.clone())
43 }
44 }
45
46 macro_rules! impl_clone {
47 ($t:ty, $method:ident) => {
48 impl Clone for Box<$t> {
49 #[inline]
50 fn clone(&self) -> Box<$t> {
51 (**self).$method()
52 }
53 }
54 }
55 }
56
57 #[allow(missing_docs)] // Bogus warning (it’s not public outside the crate), ☹
58 pub trait UncheckedAnyExt: Any {
59 unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T;
60 unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T;
61 unsafe fn downcast_unchecked<T: Any>(self: Box<Self>) -> Box<T>;
62 }
63
64 #[doc(hidden)]
65 /// A trait for the conversion of an object into a boxed trait object.
66 pub trait IntoBox<A: ?Sized + UncheckedAnyExt>: Any {
67 /// Convert self into the appropriate boxed form.
68 fn into_box(self) -> Box<A>;
69 }
70
71 macro_rules! implement {
72 ($base:ident, $(+ $bounds:ident)*) => {
73 impl fmt::Debug for dyn $base $(+ $bounds)* {
74 #[inline]
75 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
76 f.pad(stringify!(dyn $base $(+ $bounds)*))
77 }
78 }
79
80 impl UncheckedAnyExt for dyn $base $(+ $bounds)* {
81 #[inline]
82 unsafe fn downcast_ref_unchecked<T: 'static>(&self) -> &T {
83 &*(self as *const Self as *const T)
84 }
85
86 #[inline]
87 unsafe fn downcast_mut_unchecked<T: 'static>(&mut self) -> &mut T {
88 &mut *(self as *mut Self as *mut T)
89 }
90
91 #[inline]
92 unsafe fn downcast_unchecked<T: 'static>(self: Box<Self>) -> Box<T> {
93 Box::from_raw(Box::into_raw(self) as *mut T)
94 }
95 }
96
97 impl<T: $base $(+ $bounds)*> IntoBox<dyn $base $(+ $bounds)*> for T {
98 #[inline]
99 fn into_box(self) -> Box<dyn $base $(+ $bounds)*> {
100 Box::new(self)
101 }
102 }
103 }
104 }
105
106 /// A type to emulate dynamic typing.
107 ///
108 /// Every type with no non-`'static` references implements `Any`.
109 /// See the [`std::any` documentation](https://doc.rust-lang.org/std/any/index.html) for
110 /// more details on `Any` in general.
111 ///
112 /// This trait is not `std::any::Any` but rather a type extending that for this library’s
113 /// purposes so that it can be combined with marker traits like
114 /// <code><a class=trait title=core::marker::Send
115 /// href=http://doc.rust-lang.org/std/marker/trait.Send.html>Send</a></code> and
116 /// <code><a class=trait title=core::marker::Sync
117 /// href=http://doc.rust-lang.org/std/marker/trait.Sync.html>Sync</a></code>.
118 ///
119 /// See also [`CloneAny`](trait.CloneAny.html) for a cloneable version of this trait.
120 pub trait Any: StdAny { }
121 impl<T: StdAny> Any for T { }
122 implement!(Any,);
123 implement!(Any, + Send);
124 implement!(Any, + Sync);
125 implement!(Any, + Send + Sync);
126
127 /// A type to emulate dynamic typing with cloning.
128 ///
129 /// Every type with no non-`'static` references that implements `Clone` implements `Any`.
130 /// See the [`std::any` documentation](https://doc.rust-lang.org/std/any/index.html) for
131 /// more details on `Any` in general.
132 ///
133 /// This trait is not `std::any::Any` but rather a type extending that for this library’s
134 /// purposes so that it can be combined with marker traits like
135 /// <code><a class=trait title=core::marker::Send
136 /// href=http://doc.rust-lang.org/std/marker/trait.Send.html>Send</a></code> and
137 /// <code><a class=trait title=core::marker::Sync
138 /// href=http://doc.rust-lang.org/std/marker/trait.Sync.html>Sync</a></code>.
139 ///
140 /// See also [`Any`](trait.Any.html) for a version without the `Clone` requirement.
141 pub trait CloneAny: Any + CloneToAny { }
142 impl<T: StdAny + Clone> CloneAny for T { }
143 implement!(CloneAny,);
144 implement!(CloneAny, + Send);
145 implement!(CloneAny, + Sync);
146 implement!(CloneAny, + Send + Sync);
147 impl_clone!(dyn CloneAny, clone_to_any);
148 impl_clone!(dyn CloneAny + Send, clone_to_any_send);
149 impl_clone!(dyn CloneAny + Sync, clone_to_any_sync);
150 impl_clone!(dyn CloneAny + Send + Sync, clone_to_any_send_sync);