@@ -23,10 +23,14 @@ use crate::{
23
23
try_init,
24
24
types:: { ForeignOwnable , Opaque } ,
25
25
} ;
26
+ #[ cfg( CONFIG_RUST_COERCE_POINTEE ) ]
27
+ use core:: marker:: CoercePointee ;
28
+ #[ cfg( not( CONFIG_RUST_COERCE_POINTEE ) ) ]
29
+ use core:: marker:: Unsize ;
26
30
use core:: {
27
31
alloc:: Layout ,
28
32
fmt,
29
- marker:: { CoercePointee , PhantomData } ,
33
+ marker:: PhantomData ,
30
34
mem:: { ManuallyDrop , MaybeUninit } ,
31
35
ops:: { Deref , DerefMut } ,
32
36
pin:: Pin ,
@@ -125,8 +129,8 @@ mod std_vendor;
125
129
/// let coerced: Arc<dyn MyTrait> = obj;
126
130
/// # Ok::<(), Error>(())
127
131
/// ```
128
- #[ repr( transparent) ]
129
- #[ derive( CoercePointee ) ]
132
+ #[ cfg_attr ( CONFIG_RUST_COERCE_POINTEE , repr( transparent) ) ]
133
+ #[ cfg_attr ( CONFIG_RUST_COERCE_POINTEE , derive( CoercePointee ) ) ]
130
134
pub struct Arc < T : ?Sized > {
131
135
ptr : NonNull < ArcInner < T > > ,
132
136
_p : PhantomData < ArcInner < T > > ,
@@ -172,6 +176,15 @@ impl<T: ?Sized> ArcInner<T> {
172
176
}
173
177
}
174
178
179
+ // This is to allow coercion from `Arc<T>` to `Arc<U>` if `T` can be converted to the
180
+ // dynamically-sized type (DST) `U`.
181
+ #[ cfg( not( CONFIG_RUST_COERCE_POINTEE ) ) ]
182
+ impl < T : ?Sized + Unsize < U > , U : ?Sized > core:: ops:: CoerceUnsized < Arc < U > > for Arc < T > { }
183
+
184
+ // This is to allow `Arc<U>` to be dispatched on when `Arc<T>` can be coerced into `Arc<U>`.
185
+ #[ cfg( not( CONFIG_RUST_COERCE_POINTEE ) ) ]
186
+ impl < T : ?Sized + Unsize < U > , U : ?Sized > core:: ops:: DispatchFromDyn < Arc < U > > for Arc < T > { }
187
+
175
188
// SAFETY: It is safe to send `Arc<T>` to another thread when the underlying `T` is `Sync` because
176
189
// it effectively means sharing `&T` (which is safe because `T` is `Sync`); additionally, it needs
177
190
// `T` to be `Send` because any thread that has an `Arc<T>` may ultimately access `T` using a
@@ -466,13 +479,21 @@ impl<T: ?Sized> From<Pin<UniqueArc<T>>> for Arc<T> {
466
479
/// obj.as_arc_borrow().use_reference();
467
480
/// # Ok::<(), Error>(())
468
481
/// ```
469
- #[ repr( transparent) ]
470
- #[ derive( CoercePointee ) ]
482
+ #[ cfg_attr ( CONFIG_RUST_COERCE_POINTEE , repr( transparent) ) ]
483
+ #[ cfg_attr ( CONFIG_RUST_COERCE_POINTEE , derive( CoercePointee ) ) ]
471
484
pub struct ArcBorrow < ' a , T : ?Sized + ' a > {
472
485
inner : NonNull < ArcInner < T > > ,
473
486
_p : PhantomData < & ' a ( ) > ,
474
487
}
475
488
489
+ // This is to allow `ArcBorrow<U>` to be dispatched on when `ArcBorrow<T>` can be coerced into
490
+ // `ArcBorrow<U>`.
491
+ #[ cfg( not( CONFIG_RUST_COERCE_POINTEE ) ) ]
492
+ impl < T : ?Sized + Unsize < U > , U : ?Sized > core:: ops:: DispatchFromDyn < ArcBorrow < ' _ , U > >
493
+ for ArcBorrow < ' _ , T >
494
+ {
495
+ }
496
+
476
497
impl < T : ?Sized > Clone for ArcBorrow < ' _ , T > {
477
498
fn clone ( & self ) -> Self {
478
499
* self
0 commit comments