e9cef36b51bd4a4fbd9b5449db03df4db61ab20b
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<dyn CloneAny>` object.
12 fn clone_to_any(&self) -> Box
<dyn CloneAny
>;
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
;
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
;
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
;
24 impl<T
: Any
+ Clone
> CloneToAny
for T
{
26 fn clone_to_any(&self) -> Box
<dyn CloneAny
> {
27 Box
::new(self.clone())
31 fn clone_to_any_send(&self) -> Box
<dyn CloneAny
+ Send
> where Self: Send
{
32 Box
::new(self.clone())
36 fn clone_to_any_sync(&self) -> Box
<dyn CloneAny
+ Sync
> where Self: Sync
{
37 Box
::new(self.clone())
41 fn clone_to_any_send_sync(&self) -> Box
<dyn CloneAny
+ Send
+ Sync
> where Self: Send
+ Sync
{
42 Box
::new(self.clone())
46 macro_rules
! impl_clone
{
47 ($t
:ty
, $method
:ident
) => {
48 impl Clone
for Box
<$t
> {
50 fn clone(&self) -> Box
<$t
> {
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
>;
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
>;
71 macro_rules
! implement
{
72 ($base
:ident
, $
(+ $bounds
:ident
)*) => {
73 impl fmt
::Debug
for dyn $base $
(+ $bounds
)* {
75 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
76 f
.pad(stringify!(dyn $base $
(+ $bounds
)*))
80 impl UncheckedAnyExt
for dyn $base $
(+ $bounds
)* {
82 unsafe fn downcast_ref_unchecked
<T
: '
static>(&self) -> &T
{
83 &*(self as *const Self as *const T
)
87 unsafe fn downcast_mut_unchecked
<T
: '
static>(&mut self) -> &mut T
{
88 &mut *(self as *mut Self as *mut T
)
92 unsafe fn downcast_unchecked
<T
: '
static>(self: Box
<Self>) -> Box
<T
> {
93 Box
::from_raw(Box
::into_raw(self) as *mut T
)
97 impl<T
: $base $
(+ $bounds
)*> IntoBox
<dyn $base $
(+ $bounds
)*> for T
{
99 fn into_box(self) -> Box
<dyn $base $
(+ $bounds
)*> {
106 /// A type to emulate dynamic typing.
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.
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>.
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
{ }
123 implement!(Any
, + Send
);
124 implement!(Any
, + Sync
);
125 implement!(Any
, + Send
+ Sync
);
127 /// A type to emulate dynamic typing with cloning.
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.
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>.
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
);