3 #[cfg(not(feature = "std"))]
8 /// Clone `self` into a new `Box<dyn CloneAny>` object.
9 fn clone_to_any(&self) -> Box
<dyn CloneAny
>;
12 impl<T
: Any
+ Clone
> CloneToAny
for T
{
14 fn clone_to_any(&self) -> Box
<dyn CloneAny
> {
15 Box
::new(self.clone())
19 macro_rules
! impl_clone
{
21 impl Clone
for Box
<$t
> {
23 fn clone(&self) -> Box
<$t
> {
24 // SAFETY: this dance is to reapply any Send/Sync marker. I’m not happy about this
25 // approach, given that I used to do it in safe code, but then came a dodgy
26 // future-compatibility warning where_clauses_object_safety, which is spurious for
27 // auto traits but still super annoying (future-compatibility lints seem to mean
28 // your bin crate needs a corresponding allow!). Although I explained my plight¹
29 // and it was all explained and agreed upon, no action has been taken. So I finally
30 // caved and worked around it by doing it this way, which matches what’s done for
31 // core::any², so it’s probably not *too* bad.
33 // ¹ https://github.com/rust-lang/rust/issues/51443#issuecomment-421988013
34 // ² https://github.com/rust-lang/rust/blob/e7825f2b690c9a0d21b6f6d84c404bb53b151b38/library/alloc/src/boxed.rs#L1613-L1616
35 let clone
: Box
<dyn CloneAny
> = (**self).clone_to_any();
36 let raw
: *mut dyn CloneAny
= Box
::into_raw(clone
);
37 unsafe { Box
::from_raw(raw
as *mut $t
) }
41 impl fmt
::Debug
for $t
{
43 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
50 #[allow(missing_docs)] // Bogus warning (it’s not public outside the crate), ☹
51 pub trait UncheckedAnyExt
: Any
{
52 unsafe fn downcast_ref_unchecked
<T
: Any
>(&self) -> &T
;
53 unsafe fn downcast_mut_unchecked
<T
: Any
>(&mut self) -> &mut T
;
54 unsafe fn downcast_unchecked
<T
: Any
>(self: Box
<Self>) -> Box
<T
>;
58 /// A trait for the conversion of an object into a boxed trait object.
59 pub trait IntoBox
<A
: ?Sized
+ UncheckedAnyExt
>: Any
{
60 /// Convert self into the appropriate boxed form.
61 fn into_box(self) -> Box
<A
>;
64 macro_rules
! implement
{
65 ($base
:ident
, $
(+ $bounds
:ident
)*) => {
66 impl UncheckedAnyExt
for dyn $base $
(+ $bounds
)* {
68 unsafe fn downcast_ref_unchecked
<T
: '
static>(&self) -> &T
{
69 &*(self as *const Self as *const T
)
73 unsafe fn downcast_mut_unchecked
<T
: '
static>(&mut self) -> &mut T
{
74 &mut *(self as *mut Self as *mut T
)
78 unsafe fn downcast_unchecked
<T
: '
static>(self: Box
<Self>) -> Box
<T
> {
79 Box
::from_raw(Box
::into_raw(self) as *mut T
)
83 impl<T
: $base $
(+ $bounds
)*> IntoBox
<dyn $base $
(+ $bounds
)*> for T
{
85 fn into_box(self) -> Box
<dyn $base $
(+ $bounds
)*> {
93 implement!(Any
, + Send
);
94 implement!(Any
, + Send
+ Sync
);
96 /// [`Any`], but with cloning.
98 /// Every type with no non-`'static` references that implements `Clone` implements `CloneAny`.
99 /// See [`core::any`] for more details on `Any` in general.
100 pub trait CloneAny
: Any
+ CloneToAny
{ }
101 impl<T
: Any
+ Clone
> CloneAny
for T
{ }
102 implement!(CloneAny
,);
103 implement!(CloneAny
, + Send
);
104 implement!(CloneAny
, + Send
+ Sync
);
105 impl_clone!(dyn CloneAny
);
106 impl_clone!(dyn CloneAny
+ Send
);
107 impl_clone!(dyn CloneAny
+ Send
+ Sync
);