@@ -164,7 +164,7 @@ impl fmt::Debug for dyn Any + Send + Sync {
164
164
}
165
165
166
166
impl dyn Any {
167
- /// Returns `true` if the boxed type is the same as `T`.
167
+ /// Returns `true` if the inner type is the same as `T`.
168
168
///
169
169
/// # Examples
170
170
///
@@ -195,7 +195,7 @@ impl dyn Any {
195
195
t == concrete
196
196
}
197
197
198
- /// Returns some reference to the boxed value if it is of type `T`, or
198
+ /// Returns some reference to the inner value if it is of type `T`, or
199
199
/// `None` if it isn't.
200
200
///
201
201
/// # Examples
@@ -221,13 +221,13 @@ impl dyn Any {
221
221
// SAFETY: just checked whether we are pointing to the correct type, and we can rely on
222
222
// that check for memory safety because we have implemented Any for all types; no other
223
223
// impls can exist as they would conflict with our impl.
224
- unsafe { Some ( & * ( self as * const dyn Any as * const T ) ) }
224
+ unsafe { Some ( self . downcast_ref_unchecked ( ) ) }
225
225
} else {
226
226
None
227
227
}
228
228
}
229
229
230
- /// Returns some mutable reference to the boxed value if it is of type `T`, or
230
+ /// Returns some mutable reference to the inner value if it is of type `T`, or
231
231
/// `None` if it isn't.
232
232
///
233
233
/// # Examples
@@ -257,15 +257,73 @@ impl dyn Any {
257
257
// SAFETY: just checked whether we are pointing to the correct type, and we can rely on
258
258
// that check for memory safety because we have implemented Any for all types; no other
259
259
// impls can exist as they would conflict with our impl.
260
- unsafe { Some ( & mut * ( self as * mut dyn Any as * mut T ) ) }
260
+ unsafe { Some ( self . downcast_mut_unchecked ( ) ) }
261
261
} else {
262
262
None
263
263
}
264
264
}
265
+
266
+ /// Returns a reference to the inner value as type `dyn T`.
267
+ ///
268
+ /// # Examples
269
+ ///
270
+ /// ```
271
+ /// #![feature(downcast_unchecked)]
272
+ ///
273
+ /// use std::any::Any;
274
+ ///
275
+ /// let x: Box<dyn Any> = Box::new(1_usize);
276
+ ///
277
+ /// unsafe {
278
+ /// assert_eq!(*x.downcast_ref_unchecked::<usize>(), 1);
279
+ /// }
280
+ /// ```
281
+ ///
282
+ /// # Safety
283
+ ///
284
+ /// The contained value must be of type `T`. Calling this method
285
+ /// with the incorrect type is *undefined behavior*.
286
+ #[ unstable( feature = "downcast_unchecked" , issue = "90850" ) ]
287
+ #[ inline]
288
+ pub unsafe fn downcast_ref_unchecked < T : Any > ( & self ) -> & T {
289
+ debug_assert ! ( self . is:: <T >( ) ) ;
290
+ // SAFETY: caller guarantees that T is the correct type
291
+ unsafe { & * ( self as * const dyn Any as * const T ) }
292
+ }
293
+
294
+ /// Returns a mutable reference to the inner value as type `dyn T`.
295
+ ///
296
+ /// # Examples
297
+ ///
298
+ /// ```
299
+ /// #![feature(downcast_unchecked)]
300
+ ///
301
+ /// use std::any::Any;
302
+ ///
303
+ /// let mut x: Box<dyn Any> = Box::new(1_usize);
304
+ ///
305
+ /// unsafe {
306
+ /// *x.downcast_mut_unchecked::<usize>() += 1;
307
+ /// }
308
+ ///
309
+ /// assert_eq!(*x.downcast_ref::<usize>().unwrap(), 2);
310
+ /// ```
311
+ ///
312
+ /// # Safety
313
+ ///
314
+ /// The contained value must be of type `T`. Calling this method
315
+ /// with the incorrect type is *undefined behavior*.
316
+ #[ unstable( feature = "downcast_unchecked" , issue = "90850" ) ]
317
+ #[ inline]
318
+ pub unsafe fn downcast_mut_unchecked < T : Any > ( & mut self ) -> & mut T {
319
+ debug_assert ! ( self . is:: <T >( ) ) ;
320
+ // SAFETY: caller guarantees that T is the correct type
321
+ unsafe { & mut * ( self as * mut dyn Any as * mut T ) }
322
+ }
265
323
}
266
324
267
325
impl dyn Any + Send {
268
- /// Forwards to the method defined on the type `Any`.
326
+ /// Forwards to the method defined on the type `dyn Any`.
269
327
///
270
328
/// # Examples
271
329
///
@@ -289,7 +347,7 @@ impl dyn Any + Send {
289
347
<dyn Any >:: is :: < T > ( self )
290
348
}
291
349
292
- /// Forwards to the method defined on the type `Any`.
350
+ /// Forwards to the method defined on the type `dyn Any`.
293
351
///
294
352
/// # Examples
295
353
///
@@ -313,7 +371,7 @@ impl dyn Any + Send {
313
371
<dyn Any >:: downcast_ref :: < T > ( self )
314
372
}
315
373
316
- /// Forwards to the method defined on the type `Any`.
374
+ /// Forwards to the method defined on the type `dyn Any`.
317
375
///
318
376
/// # Examples
319
377
///
@@ -340,6 +398,60 @@ impl dyn Any + Send {
340
398
pub fn downcast_mut < T : Any > ( & mut self ) -> Option < & mut T > {
341
399
<dyn Any >:: downcast_mut :: < T > ( self )
342
400
}
401
+
402
+ /// Forwards to the method defined on the type `dyn Any`.
403
+ ///
404
+ /// # Examples
405
+ ///
406
+ /// ```
407
+ /// #![feature(downcast_unchecked)]
408
+ ///
409
+ /// use std::any::Any;
410
+ ///
411
+ /// let x: Box<dyn Any> = Box::new(1_usize);
412
+ ///
413
+ /// unsafe {
414
+ /// assert_eq!(*x.downcast_ref_unchecked::<usize>(), 1);
415
+ /// }
416
+ /// ```
417
+ ///
418
+ /// # Safety
419
+ ///
420
+ /// Same as the method on the type `dyn Any`.
421
+ #[ unstable( feature = "downcast_unchecked" , issue = "90850" ) ]
422
+ #[ inline]
423
+ pub unsafe fn downcast_ref_unchecked < T : Any > ( & self ) -> & T {
424
+ // SAFETY: guaranteed by caller
425
+ unsafe { <dyn Any >:: downcast_ref_unchecked :: < T > ( self ) }
426
+ }
427
+
428
+ /// Forwards to the method defined on the type `dyn Any`.
429
+ ///
430
+ /// # Examples
431
+ ///
432
+ /// ```
433
+ /// #![feature(downcast_unchecked)]
434
+ ///
435
+ /// use std::any::Any;
436
+ ///
437
+ /// let mut x: Box<dyn Any> = Box::new(1_usize);
438
+ ///
439
+ /// unsafe {
440
+ /// *x.downcast_mut_unchecked::<usize>() += 1;
441
+ /// }
442
+ ///
443
+ /// assert_eq!(*x.downcast_ref::<usize>().unwrap(), 2);
444
+ /// ```
445
+ ///
446
+ /// # Safety
447
+ ///
448
+ /// Same as the method on the type `dyn Any`.
449
+ #[ unstable( feature = "downcast_unchecked" , issue = "90850" ) ]
450
+ #[ inline]
451
+ pub unsafe fn downcast_mut_unchecked < T : Any > ( & mut self ) -> & mut T {
452
+ // SAFETY: guaranteed by caller
453
+ unsafe { <dyn Any >:: downcast_mut_unchecked :: < T > ( self ) }
454
+ }
343
455
}
344
456
345
457
impl dyn Any + Send + Sync {
@@ -418,6 +530,52 @@ impl dyn Any + Send + Sync {
418
530
pub fn downcast_mut < T : Any > ( & mut self ) -> Option < & mut T > {
419
531
<dyn Any >:: downcast_mut :: < T > ( self )
420
532
}
533
+
534
+ /// Forwards to the method defined on the type `Any`.
535
+ ///
536
+ /// # Examples
537
+ ///
538
+ /// ```
539
+ /// #![feature(downcast_unchecked)]
540
+ ///
541
+ /// use std::any::Any;
542
+ ///
543
+ /// let x: Box<dyn Any> = Box::new(1_usize);
544
+ ///
545
+ /// unsafe {
546
+ /// assert_eq!(*x.downcast_ref_unchecked::<usize>(), 1);
547
+ /// }
548
+ /// ```
549
+ #[ unstable( feature = "downcast_unchecked" , issue = "90850" ) ]
550
+ #[ inline]
551
+ pub unsafe fn downcast_ref_unchecked < T : Any > ( & self ) -> & T {
552
+ // SAFETY: guaranteed by caller
553
+ unsafe { <dyn Any >:: downcast_ref_unchecked :: < T > ( self ) }
554
+ }
555
+
556
+ /// Forwards to the method defined on the type `Any`.
557
+ ///
558
+ /// # Examples
559
+ ///
560
+ /// ```
561
+ /// #![feature(downcast_unchecked)]
562
+ ///
563
+ /// use std::any::Any;
564
+ ///
565
+ /// let mut x: Box<dyn Any> = Box::new(1_usize);
566
+ ///
567
+ /// unsafe {
568
+ /// *x.downcast_mut_unchecked::<usize>() += 1;
569
+ /// }
570
+ ///
571
+ /// assert_eq!(*x.downcast_ref::<usize>().unwrap(), 2);
572
+ /// ```
573
+ #[ unstable( feature = "downcast_unchecked" , issue = "90850" ) ]
574
+ #[ inline]
575
+ pub unsafe fn downcast_mut_unchecked < T : Any > ( & mut self ) -> & mut T {
576
+ // SAFETY: guaranteed by caller
577
+ unsafe { <dyn Any >:: downcast_mut_unchecked :: < T > ( self ) }
578
+ }
421
579
}
422
580
423
581
///////////////////////////////////////////////////////////////////////////////
0 commit comments