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