@@ -243,6 +243,7 @@ fn main() {
243
243
```
244
244
*/
245
245
246
+ use std:: mem;
246
247
pub use stable_deref_trait:: { StableDeref as StableAddress , CloneStableDeref as CloneStableAddress } ;
247
248
248
249
/// An owning reference.
@@ -279,7 +280,7 @@ pub struct OwningRefMut<O, T: ?Sized> {
279
280
pub trait Erased { }
280
281
impl < T > Erased for T { }
281
282
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,
283
284
/// for example `Box<T> -> Box<Erased>`. This would be unneeded with
284
285
/// higher kinded types support in the language.
285
286
pub unsafe trait IntoErased < ' a > {
@@ -289,10 +290,20 @@ pub unsafe trait IntoErased<'a> {
289
290
fn into_erased ( self ) -> Self :: Erased ;
290
291
}
291
292
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,
293
304
/// for example `Box<T> -> Box<Erased + Send + Sync>`. This would be unneeded with
294
305
/// higher kinded types support in the language.
295
- pub unsafe trait IntoErasedSendSync < ' a > : Send + Sync {
306
+ pub unsafe trait IntoErasedSendSync < ' a > {
296
307
/// Owner with the dereference type substituted to `Erased + Send + Sync`.
297
308
type Erased : Send + Sync ;
298
309
/// Perform the type erasure.
@@ -472,6 +483,18 @@ impl<O, T: ?Sized> OwningRef<O, T> {
472
483
}
473
484
}
474
485
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
+
475
498
/// Erases the concrete base type of the owner with a trait object which implements `Send` and `Sync`.
476
499
///
477
500
/// 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> {
1161
1184
}
1162
1185
}
1163
1186
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 {
1167
1190
self
1168
1191
}
1169
1192
}
1170
1193
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
+
1171
1206
unsafe impl < ' a , T : Send + Sync + ' a > IntoErasedSendSync < ' a > for Arc < T > {
1172
1207
type Erased = Arc < Erased + Send + Sync + ' a > ;
1173
1208
fn into_erased_send_sync ( self ) -> Self :: Erased {
0 commit comments