@@ -101,6 +101,8 @@ pub trait TraitcastableAnyInfra<Target: ?Sized>: 'static {
101
101
fn downcast_ref ( & self ) -> Option < & Target > ;
102
102
103
103
/// Unchecked variant of `downcast_ref`
104
+ /// # Safety
105
+ /// This function is unsafe because the caller must ensure that the cast is valid.
104
106
#[ cfg( feature = "downcast_unchecked" ) ]
105
107
#[ doc( cfg( feature = "downcast_unchecked" ) ) ]
106
108
unsafe fn downcast_ref_unchecked ( & self ) -> & Target ;
@@ -114,6 +116,8 @@ pub trait TraitcastableAnyInfra<Target: ?Sized>: 'static {
114
116
fn downcast_mut ( & mut self ) -> Option < & mut Target > ;
115
117
116
118
/// Unchecked variant of `downcast_ref`
119
+ /// # Safety
120
+ /// This function is unsafe because the caller must ensure that the cast is valid.
117
121
#[ cfg( feature = "downcast_unchecked" ) ]
118
122
#[ doc( cfg( feature = "downcast_unchecked" ) ) ]
119
123
unsafe fn downcast_mut_unchecked ( & mut self ) -> & mut Target ;
@@ -140,6 +144,8 @@ pub trait TraitcastableAnyInfraExt<Target: ?Sized + 'static>: Sized {
140
144
fn downcast ( self ) -> Result < Self :: Output , Self > ;
141
145
142
146
/// Unchecked variant of `downcast`
147
+ /// # Safety
148
+ /// This function is unsafe because the caller must ensure that the cast is valid.
143
149
#[ cfg( feature = "downcast_unchecked" ) ]
144
150
#[ doc( cfg( feature = "downcast_unchecked" ) ) ]
145
151
unsafe fn downcast_unchecked ( self ) -> Self :: Output ;
@@ -202,7 +208,8 @@ macro_rules! implement_with_markers {
202
208
}
203
209
#[ cfg( feature = "downcast_unchecked" ) ]
204
210
default unsafe fn downcast_ref_unchecked( & self ) -> & Target {
205
- self . downcast_ref( ) . unwrap_unchecked( )
211
+ // SAFETY: The caller must ensure that the cast is valid.
212
+ unsafe { self . downcast_ref( ) . unwrap_unchecked( ) }
206
213
}
207
214
208
215
default fn downcast_mut( & mut self ) -> Option <& mut Target > {
@@ -224,7 +231,8 @@ macro_rules! implement_with_markers {
224
231
}
225
232
#[ cfg( feature = "downcast_unchecked" ) ]
226
233
default unsafe fn downcast_mut_unchecked( & mut self ) -> & mut Target {
227
- self . downcast_mut( ) . unwrap_unchecked( )
234
+ // SAFETY: The caller must ensure that the cast is valid.
235
+ unsafe { self . downcast_mut( ) . unwrap_unchecked( ) }
228
236
}
229
237
}
230
238
impl <Target : Sized + ' static + $( $traits +) * > TraitcastableAnyInfra <Target > for dyn TraitcastableAny $( + $traits) * {
@@ -239,15 +247,17 @@ macro_rules! implement_with_markers {
239
247
}
240
248
#[ cfg( feature = "downcast_unchecked" ) ]
241
249
unsafe fn downcast_ref_unchecked( & self ) -> & Target {
242
- <dyn Any >:: downcast_ref_unchecked:: <Target >( self )
250
+ // SAFETY: We are just forwarding the call to the `Any` trait.
251
+ unsafe { <dyn Any >:: downcast_ref_unchecked:: <Target >( self ) }
243
252
}
244
253
245
254
fn downcast_mut( & mut self ) -> Option <& mut Target > {
246
255
<dyn Any >:: downcast_mut:: <Target >( self )
247
256
}
248
257
#[ cfg( feature = "downcast_unchecked" ) ]
249
258
unsafe fn downcast_mut_unchecked( & mut self ) -> & mut Target {
250
- <dyn Any >:: downcast_mut_unchecked:: <Target >( self )
259
+ // SAFETY: We are just forwarding the call to the `Any` trait.
260
+ unsafe { <dyn Any >:: downcast_mut_unchecked:: <Target >( self ) }
251
261
}
252
262
}
253
263
} ;
@@ -277,7 +287,8 @@ impl<Src: TraitcastableAnyInfra<Target> + ?Sized, Target: ?Sized + 'static>
277
287
}
278
288
#[ cfg( feature = "downcast_unchecked" ) ]
279
289
default unsafe fn downcast_unchecked ( self ) -> Self :: Output {
280
- <Self as TraitcastableAnyInfraExt < Target > >:: downcast ( self ) . unwrap_unchecked ( )
290
+ // SAFETY: The caller must ensure that the cast is valid.
291
+ unsafe { <Self as TraitcastableAnyInfraExt < Target > >:: downcast ( self ) . unwrap_unchecked ( ) }
281
292
}
282
293
}
283
294
@@ -304,7 +315,8 @@ impl<Src: TraitcastableAnyInfra<Target>, Target: Sized + 'static> TraitcastableA
304
315
305
316
#[ cfg( feature = "downcast_unchecked" ) ]
306
317
unsafe fn downcast_unchecked ( self ) -> Self :: Output {
307
- <Box < dyn Any > >:: downcast_unchecked :: < Target > ( self )
318
+ // SAFETY: The caller must ensure that the cast is valid.
319
+ unsafe { <Box < dyn Any > >:: downcast_unchecked :: < Target > ( self ) }
308
320
}
309
321
}
310
322
@@ -332,7 +344,8 @@ impl<Src: TraitcastableAnyInfra<Target> + ?Sized, Target: ?Sized + 'static>
332
344
}
333
345
#[ cfg( feature = "downcast_unchecked" ) ]
334
346
default unsafe fn downcast_unchecked ( self ) -> Self :: Output {
335
- <Self as TraitcastableAnyInfraExt < Target > >:: downcast ( self ) . unwrap_unchecked ( )
347
+ // SAFETY: The caller must ensure that the cast is valid.
348
+ unsafe { <Self as TraitcastableAnyInfraExt < Target > >:: downcast ( self ) . unwrap_unchecked ( ) }
336
349
}
337
350
}
338
351
@@ -359,7 +372,8 @@ impl<Src: TraitcastableAnyInfra<Target>, Target: Sized + 'static> TraitcastableA
359
372
360
373
#[ cfg( feature = "downcast_unchecked" ) ]
361
374
unsafe fn downcast_unchecked ( self ) -> Self :: Output {
362
- <Rc < dyn Any > >:: downcast_unchecked :: < Target > ( self )
375
+ // SAFETY: The caller must ensure that the cast is valid.
376
+ unsafe { <Rc < dyn Any > >:: downcast_unchecked :: < Target > ( self ) }
363
377
}
364
378
}
365
379
@@ -389,7 +403,8 @@ impl<
389
403
}
390
404
#[ cfg( feature = "downcast_unchecked" ) ]
391
405
default unsafe fn downcast_unchecked ( self ) -> Self :: Output {
392
- <Self as TraitcastableAnyInfraExt < Target > >:: downcast ( self ) . unwrap_unchecked ( )
406
+ // SAFETY: The caller must ensure that the cast is valid.
407
+ unsafe { <Self as TraitcastableAnyInfraExt < Target > >:: downcast ( self ) . unwrap_unchecked ( ) }
393
408
}
394
409
}
395
410
@@ -416,7 +431,8 @@ impl<Src: TraitcastableAnyInfra<Target> + Send + Sync, Target: Sized + 'static +
416
431
417
432
#[ cfg( feature = "downcast_unchecked" ) ]
418
433
unsafe fn downcast_unchecked ( self ) -> Self :: Output {
419
- <Arc < dyn Any + Send + Sync > >:: downcast_unchecked :: < Target > ( self )
434
+ // SAFETY: The caller must ensure that the cast is valid.
435
+ unsafe { <Arc < dyn Any + Send + Sync > >:: downcast_unchecked :: < Target > ( self ) }
420
436
}
421
437
}
422
438
0 commit comments