@@ -243,6 +243,7 @@ fn main() {
243243```
244244*/
245245
246+ use std:: mem;
246247pub use stable_deref_trait:: { StableDeref as StableAddress , CloneStableDeref as CloneStableAddress } ;
247248
248249/// An owning reference.
@@ -279,7 +280,7 @@ pub struct OwningRefMut<O, T: ?Sized> {
279280pub trait Erased { }
280281impl < T > Erased for T { }
281282
282- /// Helper trait for erasing the concrete type of what an owner derferences to,
283+ /// Helper trait for erasing the concrete type of what an owner dereferences to,
283284/// for example `Box<T> -> Box<Erased>`. This would be unneeded with
284285/// higher kinded types support in the language.
285286pub unsafe trait IntoErased < ' a > {
@@ -289,10 +290,20 @@ pub unsafe trait IntoErased<'a> {
289290 fn into_erased ( self ) -> Self :: Erased ;
290291}
291292
292- /// Helper trait for erasing the concrete type of what an owner derferences to,
293+ /// Helper trait for erasing the concrete type of what an owner dereferences to,
294+ /// for example `Box<T> -> Box<Erased + Send>`. This would be unneeded with
295+ /// higher kinded types support in the language.
296+ pub unsafe trait IntoErasedSend < ' a > {
297+ /// Owner with the dereference type substituted to `Erased + Send`.
298+ type Erased : Send ;
299+ /// Perform the type erasure.
300+ fn into_erased_send ( self ) -> Self :: Erased ;
301+ }
302+
303+ /// Helper trait for erasing the concrete type of what an owner dereferences to,
293304/// for example `Box<T> -> Box<Erased + Send + Sync>`. This would be unneeded with
294305/// higher kinded types support in the language.
295- pub unsafe trait IntoErasedSendSync < ' a > : Send + Sync {
306+ pub unsafe trait IntoErasedSendSync < ' a > {
296307 /// Owner with the dereference type substituted to `Erased + Send + Sync`.
297308 type Erased : Send + Sync ;
298309 /// Perform the type erasure.
@@ -472,6 +483,18 @@ impl<O, T: ?Sized> OwningRef<O, T> {
472483 }
473484 }
474485
486+ /// Erases the concrete base type of the owner with a trait object which implements `Send`.
487+ ///
488+ /// This allows mixing of owned references with different owner base types.
489+ pub fn erase_send_owner < ' a > ( self ) -> OwningRef < O :: Erased , T >
490+ where O : IntoErasedSend < ' a > ,
491+ {
492+ OwningRef {
493+ reference : self . reference ,
494+ owner : self . owner . into_erased_send ( ) ,
495+ }
496+ }
497+
475498 /// Erases the concrete base type of the owner with a trait object which implements `Send` and `Sync`.
476499 ///
477500 /// This allows mixing of owned references with different owner base types.
@@ -1161,13 +1184,25 @@ unsafe impl<'a, T: 'a> IntoErased<'a> for Arc<T> {
11611184 }
11621185}
11631186
1164- unsafe impl < ' a , T : Send + Sync + ' a > IntoErasedSendSync < ' a > for Box < T > {
1165- type Erased = Box < Erased + Send + Sync + ' a > ;
1166- fn into_erased_send_sync ( self ) -> Self :: Erased {
1187+ unsafe impl < ' a , T : Send + ' a > IntoErasedSend < ' a > for Box < T > {
1188+ type Erased = Box < Erased + Send + ' a > ;
1189+ fn into_erased_send ( self ) -> Self :: Erased {
11671190 self
11681191 }
11691192}
11701193
1194+ unsafe impl < ' a , T : Send + ' a > IntoErasedSendSync < ' a > for Box < T > {
1195+ type Erased = Box < Erased + Sync + Send + ' a > ;
1196+ fn into_erased_send_sync ( self ) -> Self :: Erased {
1197+ let result: Box < Erased + Send + ' a > = self ;
1198+ // This is safe since Erased can always implement Sync
1199+ // Only the destructor is available and it takes &mut self
1200+ unsafe {
1201+ mem:: transmute ( result)
1202+ }
1203+ }
1204+ }
1205+
11711206unsafe impl < ' a , T : Send + Sync + ' a > IntoErasedSendSync < ' a > for Arc < T > {
11721207 type Erased = Arc < Erased + Send + Sync + ' a > ;
11731208 fn into_erased_send_sync ( self ) -> Self :: Erased {
0 commit comments