You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tracking Issue for #![feature(downcast_unchecked)] #90850
? I suppose you lose the debug_assert! for correct typing which is possible if you have dyn Any, though it is true that you can already any.downcast().unwrap_unchecked() for that.
To immediately answer my own question: Box doesn't want to add methods to arbitrary types like that. cast_unchecked is still useful but would probably instead be pub unsafe fn Box::<T: ?Sized, A>::cast<U>(this: Self) -> Box<U, A> to make it an associated function rather than a method which could shadow a pointee's method.
The semantics of a.downcast_ref_unchecked() and a.downcast_ref().unwrap_unchecked() are still different. For example, with a.downcast_ref_unchecked() one could "transmute" between two types that are different but #[repr(C)] compatible in layout, whereas with a.downcast_ref().unwrap_unchecked() you'd likely be dereferencing a null pointer.
I'd like to point out that in all other places in the std, the ref/mut part comes after the unchecked part. Therefore this should be downcast_unchecked_ref and downcast_unchecked_mut to stay concistent AFAICS.
Reacted by radiish, Gabriel Bjørnager Jensen, Techcable and Andrew Hlynskyi
Sorry, I'm new to Rust contribution. Does this mean that if we rename to downcast_unchecked_ref and downcast_unchecked_mut we can consider this API ready for stabilization @marc0246 ?
@taj-p if you have the time to make the PR, I would very much appreciate it. You can link my comment above for the justification. But note that I have no decision-making power here. You could make a stabilization PR right now, although I would raise the same concern and hopefully the powers that be would take it into consideration. At least, that's how the governance is supposed to work.
Has this API not been stabilized yet? It would be very useful in reflection-related libraries:
use std::any::Any;traitReflect:Any{}impldynReflect{fndowncast<T:Reflect>(self:Box<dynReflect>) -> Result<Box<T>,Box<dynReflect>>{// `<Box<dyn Any>>::downcast` returns `Result<Box<T>, Box<dyn Any>>`// Unable to convert `Box<dyn Any>` back to `Box<dyn Reflect>`,// therefore it is necessary to check the type in advance.ifself.is::<T>(){unsafe{// // `TypeId` will be checked again here?// Ok(<Box<dyn Any>>::downcast(self).unwrap_unchecked())// Good, `TypeId` will not be checked again.Ok(<Box<dynAny>>::downcast_unchecked(self))}}else{Err(self)}}}
Although compiler optimizations might eliminate the inner check in downcast after inlining (since the two checks are identical), it's still beneficial to explicitly remove one redundant check.
Activity
david-perez commentedon Jan 10, 2022
Should these be added to
std::error::Errortoo?ibraheemdev commentedon Jan 11, 2022
I don't see how that would be useful. You generally don't know the concrete type of an error before downcasting.
lunabunn commentedon May 1, 2022
This would allow me to shed my dependency on
unsafe_any. What's the state of this feature? Is it ready for an FCP?ibraheemdev commentedon May 1, 2022
Now that
{Option, Result}::unwrap_uncheckedare stabilized, I wonder if there is much benefit to these methods.Rollup merge of rust-lang#96609 - ibraheemdev:arc-downcast-unchecked,…
CAD97 commentedon Oct 6, 2022
Instead of
downcast_unchecked, is there a good reason to not just have the more general? I suppose you lose the
debug_assert!for correct typing which is possible if you havedyn Any, though it is true that you can alreadyany.downcast().unwrap_unchecked()for that.To immediately answer my own question:
Boxdoesn't want to add methods to arbitrary types like that.cast_uncheckedis still useful but would probably instead bepub unsafe fn Box::<T: ?Sized, A>::cast<U>(this: Self) -> Box<U, A>to make it an associated function rather than a method which could shadow a pointee's method.yshui commentedon Nov 13, 2022
@ibraheemdev this still generates better code than
downcast_ref().unwrap_unchecked().compiles to
whereas
becomes
Both compiled with
opt-level=3yshui commentedon Nov 13, 2022
I think llvm didn't realize that
T::type_idcall is pure (because it's called through the vtable?), so it kept the call.yshui commentedon Nov 13, 2022
huh, why is type_id not stored directly in the vtable?
CAD97 commentedon Nov 14, 2022
The short version is that Rust doesn't have a way to get associated constant data from a trait object (yet).
gh2o commentedon Dec 9, 2022
The semantics of
a.downcast_ref_unchecked()anda.downcast_ref().unwrap_unchecked()are still different. For example, witha.downcast_ref_unchecked()one could "transmute" between two types that are different but#[repr(C)]compatible in layout, whereas witha.downcast_ref().unwrap_unchecked()you'd likely be dereferencing a null pointer.marc0246 commentedon May 5, 2023
I'd like to point out that in all other places in the std, the
ref/mutpart comes after theuncheckedpart. Therefore this should bedowncast_unchecked_refanddowncast_unchecked_mutto stay concistent AFAICS.Wasabi375 commentedon Jun 26, 2024
Hey, is there a reason that
Box::downcastand similar functions requireTto be sized? AFAICT they should work with unsized types.6 remaining items
taj-p commentedon Oct 20, 2025
Sorry, I'm new to Rust contribution. Does this mean that if we rename to
downcast_unchecked_refanddowncast_unchecked_mutwe can consider this API ready for stabilization @marc0246 ?I'm happy to open a PR with that change.
marc0246 commentedon Oct 23, 2025
@taj-p if you have the time to make the PR, I would very much appreciate it. You can link my comment above for the justification. But note that I have no decision-making power here. You could make a stabilization PR right now, although I would raise the same concern and hopefully the powers that be would take it into consideration. At least, that's how the governance is supposed to work.
Rollup merge of rust-lang#148079 - taj-p:master, r=Mark-Simulacrum
Unrolled build for #148079
Rollup merge of #148079 - taj-p:master, r=Mark-Simulacrum
Rollup merge of #148079 - taj-p:master, r=Mark-Simulacrum
Rollup merge of #148079 - taj-p:master, r=Mark-Simulacrum
Rollup merge of rust-lang#148079 - taj-p:master, r=Mark-Simulacrum
Rollup merge of #148079 - taj-p:master, r=Mark-Simulacrum
Rollup merge of rust-lang#148079 - taj-p:master, r=Mark-Simulacrum
Mysvac commentedon Dec 16, 2025
Has this API not been stabilized yet? It would be very useful in reflection-related libraries:
Although compiler optimizations might eliminate the inner check in
downcastafter inlining (since the two checks are identical), it's still beneficial to explicitly remove one redundant check.Rollup merge of #148079 - taj-p:master, r=Mark-Simulacrum
Rollup merge of #148079 - taj-p:master, r=Mark-Simulacrum