4120e6ad91d98148115bd4b3bf7db399b61c1b3d
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
{
25 fn clone_to_any(&self) -> Box
<CloneAny
> {
26 Box
::new(self.clone())
29 fn clone_to_any_send(&self) -> Box
<CloneAny
+ Send
> where Self: Send
{
30 Box
::new(self.clone())
33 fn clone_to_any_sync(&self) -> Box
<CloneAny
+ Sync
> where Self: Sync
{
34 Box
::new(self.clone())
37 fn clone_to_any_send_sync(&self) -> Box
<CloneAny
+ Send
+ Sync
> where Self: Send
+ Sync
{
38 Box
::new(self.clone())
44 /// A type to emulate dynamic typing.
46 /// Every type with no non-`'static` references implements `Any`.
47 define!(CloneAny remainder
);
50 /// A type to emulate dynamic typing with cloning.
52 /// Every type with no non-`'static` references that implements `Clone` implements `Any`.
53 define!(Any remainder
);
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.
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>.
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
{ }
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
{ }
80 macro_rules
! impl_clone
{
81 ($t
:ty
, $method
:ident
) => {
82 impl Clone
for Box
<$t
> {
83 fn clone(&self) -> Box
<$t
> {
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
>;
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
>;
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
)*))
112 impl UncheckedAnyExt
for $base $
(+ $bounds
)* {
113 unsafe fn downcast_ref_unchecked
<T
: '
static>(&self) -> &T
{
114 &*(self as *const Self as *const T
)
117 unsafe fn downcast_mut_unchecked
<T
: '
static>(&mut self) -> &mut T
{
118 &mut *(self as *mut Self as *mut T
)
121 unsafe fn downcast_unchecked
<T
: '
static>(self: Box
<Self>) -> Box
<T
> {
122 Box
::from_raw(Box
::into_raw(self) as *mut T
)
126 impl<T
: $base $
(+ $bounds
)*> IntoBox
<$base $
(+ $bounds
)*> for T
{
127 fn into_box(self) -> Box
<$base $
(+ $bounds
)*> {
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
);
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
);