1 //! The different types of `Any` for use in a map.
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`.
8 use std
::any
::Any
as StdAny
;
11 pub trait CloneToAny
{
12 /// Clone `self` into a new `Box<CloneAny>` object.
13 fn clone_to_any(&self) -> Box
<CloneAny
>;
15 /// Clone `self` into a new `Box<CloneAny + Send>` object.
16 fn clone_to_any_send(&self) -> Box
<CloneAny
+ Send
> where Self: Send
;
18 /// Clone `self` into a new `Box<CloneAny + Sync>` object.
19 fn clone_to_any_sync(&self) -> Box
<CloneAny
+ Sync
> where Self: Sync
;
21 /// Clone `self` into a new `Box<CloneAny + Send + Sync>` object.
22 fn clone_to_any_send_sync(&self) -> Box
<CloneAny
+ Send
+ Sync
> where Self: Send
+ Sync
;
25 impl<T
: Any
+ Clone
> CloneToAny
for T
{
26 fn clone_to_any(&self) -> Box
<CloneAny
> {
27 Box
::new(self.clone())
30 fn clone_to_any_send(&self) -> Box
<CloneAny
+ Send
> where Self: Send
{
31 Box
::new(self.clone())
34 fn clone_to_any_sync(&self) -> Box
<CloneAny
+ Sync
> where Self: Sync
{
35 Box
::new(self.clone())
38 fn clone_to_any_send_sync(&self) -> Box
<CloneAny
+ Send
+ Sync
> where Self: Send
+ Sync
{
39 Box
::new(self.clone())
45 /// A type to emulate dynamic typing.
47 /// Every type with no non-`'static` references implements `Any`.
48 define!(CloneAny remainder
);
51 /// A type to emulate dynamic typing with cloning.
53 /// Every type with no non-`'static` references that implements `Clone` implements `Any`.
54 define!(Any remainder
);
56 ($t
:ident remainder
) => {
57 /// See the [`std::any` documentation](https://doc.rust-lang.org/std/any/index.html) for
58 /// more details on `Any` in general.
60 /// This trait is not `std::any::Any` but rather a type extending that for this library’s
61 /// purposes so that it can be combined with marker traits like
62 /// <code><a class=trait title=core::marker::Send
63 /// href=http://doc.rust-lang.org/std/marker/trait.Send.html>Send</a></code> and
64 /// <code><a class=trait title=core::marker::Sync
65 /// href=http://doc.rust-lang.org/std/marker/trait.Sync.html>Sync</a></code>.
70 /// See also [`Any`](trait.Any.html) for a version without the `Clone` requirement.
71 pub trait CloneAny
: Any
+ CloneToAny
{ }
72 impl<T
: StdAny
+ Clone
> CloneAny
for T
{ }
75 /// See also [`CloneAny`](trait.CloneAny.html) for a cloneable version of this trait.
76 pub trait Any
: StdAny
{ }
77 impl<T
: StdAny
> Any
for T
{ }
81 macro_rules
! impl_clone
{
82 ($t
:ty
, $method
:ident
) => {
83 impl Clone
for Box
<$t
> {
84 fn clone(&self) -> Box
<$t
> {
91 #[cfg(feature = "nightly")]
92 use std
::raw
::TraitObject
;
94 #[cfg(not(feature = "nightly"))]
96 #[allow(raw_pointer_derive)]
97 #[derive(Copy, Clone)]
103 #[allow(missing_docs)] // Bogus warning (it’s not public outside the crate), ☹
104 pub trait UncheckedAnyExt
: Any
{
105 unsafe fn downcast_ref_unchecked
<T
: Any
>(&self) -> &T
;
106 unsafe fn downcast_mut_unchecked
<T
: Any
>(&mut self) -> &mut T
;
107 unsafe fn downcast_unchecked
<T
: Any
>(self: Box
<Self>) -> Box
<T
>;
111 /// A trait for the conversion of an object into a boxed trait object.
112 pub trait IntoBox
<A
: ?Sized
+ UncheckedAnyExt
>: Any
{
113 /// Convert self into the appropriate boxed form.
114 fn into_box(self) -> Box
<A
>;
117 macro_rules
! implement
{
118 ($base
:ident
, $
(+ $bounds
:ident
)*) => {
119 impl<'a
> fmt
::Debug
for &'
a ($base $
(+ $bounds
)*) {
120 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
121 f
.pad(stringify!(&($base $
(+ $bounds
)*)))
125 impl<'a
> fmt
::Debug
for Box
<$base $
(+ $bounds
)*> {
126 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
127 f
.pad(stringify!(Box
<$base $
(+ $bounds
)*>))
131 impl UncheckedAnyExt
for $base $
(+ $bounds
)* {
132 unsafe fn downcast_ref_unchecked
<T
: '
static>(&self) -> &T
{
133 mem
::transmute(mem
::transmute
::<_
, TraitObject
>(self).data
)
136 unsafe fn downcast_mut_unchecked
<T
: '
static>(&mut self) -> &mut T
{
137 mem
::transmute(mem
::transmute
::<_
, TraitObject
>(self).data
)
140 unsafe fn downcast_unchecked
<T
: '
static>(self: Box
<Self>) -> Box
<T
> {
141 mem
::transmute(mem
::transmute
::<_
, TraitObject
>(self).data
)
145 impl<T
: $base $
(+ $bounds
)*> IntoBox
<$base $
(+ $bounds
)*> for T
{
146 fn into_box(self) -> Box
<$base $
(+ $bounds
)*> {
155 implement!(Any
, + Send
);
156 implement!(Any
, + Sync
);
157 implement!(Any
, + Send
+ Sync
);
158 implement!(CloneAny
,);
159 implement!(CloneAny
, + Send
);
160 implement!(CloneAny
, + Sync
);
161 implement!(CloneAny
, + Send
+ Sync
);
164 impl_clone!(CloneAny
, clone_to_any
);
165 impl_clone!((CloneAny
+ Send
), clone_to_any_send
);
166 impl_clone!((CloneAny
+ Sync
), clone_to_any_sync
);
167 impl_clone!((CloneAny
+ Send
+ Sync
), clone_to_any_send_sync
);