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`.
7 use std
::any
::Any
as StdAny
;
10 pub trait CloneToAny
{
11 /// Clone `self` into a new `Box<CloneAny>` object.
12 fn clone_to_any(&self) -> Box
<CloneAny
>;
14 /// Clone `self` into a new `Box<CloneAny + Send>` object.
15 fn clone_to_any_send(&self) -> Box
<CloneAny
+ Send
> where Self: Send
;
17 /// Clone `self` into a new `Box<CloneAny + Sync>` object.
18 fn clone_to_any_sync(&self) -> Box
<CloneAny
+ Sync
> where Self: Sync
;
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
;
24 impl<T
: Any
+ Clone
> CloneToAny
for T
{
26 fn clone_to_any(&self) -> Box
<CloneAny
> {
27 Box
::new(self.clone())
31 fn clone_to_any_send(&self) -> Box
<CloneAny
+ Send
> where Self: Send
{
32 Box
::new(self.clone())
36 fn clone_to_any_sync(&self) -> Box
<CloneAny
+ Sync
> where Self: Sync
{
37 Box
::new(self.clone())
41 fn clone_to_any_send_sync(&self) -> Box
<CloneAny
+ Send
+ Sync
> where Self: Send
+ Sync
{
42 Box
::new(self.clone())
48 /// A type to emulate dynamic typing.
50 /// Every type with no non-`'static` references implements `Any`.
51 define!(CloneAny remainder
);
54 /// A type to emulate dynamic typing with cloning.
56 /// Every type with no non-`'static` references that implements `Clone` implements `Any`.
57 define!(Any remainder
);
59 ($t
:ident remainder
) => {
60 /// See the [`std::any` documentation](https://doc.rust-lang.org/std/any/index.html) for
61 /// more details on `Any` in general.
63 /// This trait is not `std::any::Any` but rather a type extending that for this library’s
64 /// purposes so that it can be combined with marker traits like
65 /// <code><a class=trait title=core::marker::Send
66 /// href=http://doc.rust-lang.org/std/marker/trait.Send.html>Send</a></code> and
67 /// <code><a class=trait title=core::marker::Sync
68 /// href=http://doc.rust-lang.org/std/marker/trait.Sync.html>Sync</a></code>.
73 /// See also [`Any`](trait.Any.html) for a version without the `Clone` requirement.
74 pub trait CloneAny
: Any
+ CloneToAny
{ }
75 impl<T
: StdAny
+ Clone
> CloneAny
for T
{ }
78 /// See also [`CloneAny`](trait.CloneAny.html) for a cloneable version of this trait.
79 pub trait Any
: StdAny
{ }
80 impl<T
: StdAny
> Any
for T
{ }
84 macro_rules
! impl_clone
{
85 ($t
:ty
, $method
:ident
) => {
86 impl Clone
for Box
<$t
> {
88 fn clone(&self) -> Box
<$t
> {
95 #
[allow(missing_docs
)] // Bogus warning (it’s not public outside the crate), ☹
96 pub trait UncheckedAnyExt
: Any
{
97 unsafe fn downcast_ref_unchecked
<T
: Any
>(&self) -> &T
;
98 unsafe fn downcast_mut_unchecked
<T
: Any
>(&mut self) -> &mut T
;
99 unsafe fn downcast_unchecked
<T
: Any
>(self: Box
<Self>) -> Box
<T
>;
103 /// A trait for the conversion of an object into a boxed trait object.
104 pub trait IntoBox
<A
: ?Sized
+ UncheckedAnyExt
>: Any
{
105 /// Convert self into the appropriate boxed form.
106 fn into_box(self) -> Box
<A
>;
109 macro_rules
! implement
{
110 ($base
:ident
, $
(+ $bounds
:ident
)*) => {
111 impl fmt
::Debug
for $base $
(+ $bounds
)* {
113 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
114 f
.pad(stringify!($base $
(+ $bounds
)*))
118 impl UncheckedAnyExt
for $base $
(+ $bounds
)* {
120 unsafe fn downcast_ref_unchecked
<T
: '
static>(&self) -> &T
{
121 &*(self as *const Self as *const T
)
125 unsafe fn downcast_mut_unchecked
<T
: '
static>(&mut self) -> &mut T
{
126 &mut *(self as *mut Self as *mut T
)
130 unsafe fn downcast_unchecked
<T
: '
static>(self: Box
<Self>) -> Box
<T
> {
131 Box
::from_raw(Box
::into_raw(self) as *mut T
)
135 impl<T
: $base $
(+ $bounds
)*> IntoBox
<$base $
(+ $bounds
)*> for T
{
137 fn into_box(self) -> Box
<$base $
(+ $bounds
)*> {
146 implement!(Any
, + Send
);
147 implement!(Any
, + Sync
);
148 implement!(Any
, + Send
+ Sync
);
149 implement!(CloneAny
,);
150 implement!(CloneAny
, + Send
);
151 implement!(CloneAny
, + Sync
);
152 implement!(CloneAny
, + Send
+ Sync
);
155 impl_clone!(CloneAny
, clone_to_any
);
156 impl_clone!((CloneAny
+ Send
), clone_to_any_send
);
157 impl_clone!((CloneAny
+ Sync
), clone_to_any_sync
);
158 impl_clone!((CloneAny
+ Send
+ Sync
), clone_to_any_send_sync
);