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