-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tracking Issue for #![feature(downcast_unchecked)]
#90850
Comments
Should these be added to |
I don't see how that would be useful. You generally don't know the concrete type of an error before downcasting. |
This would allow me to shed my dependency on |
Now that |
… r=m-ou-se Add `{Arc, Rc}::downcast_unchecked` Part of rust-lang#90850.
Instead of impl Box<T: ?Sized, A> {
pub unsafe fn cast_unchecked<U>(self) -> Box<U, A> {
debug_assert_eq!(Layout::for_value(&*self), Layout::new::<U>());
unsafe {
let (raw, alloc) = Box::into_raw_with_allocator(self);
Box::from_raw_in(raw.cast(), alloc)
}
}
} ? I suppose you lose the To immediately answer my own question: |
@ibraheemdev this still generates better code than pub fn cast(a: &dyn std::any::Any) -> &i32 {
unsafe { a.downcast_ref_unchecked() }
} compiles to example::cast:
mov rax, rdi
ret whereas pub fn cast(a: &dyn std::any::Any) -> &i32 {
unsafe { a.downcast_ref().unwrap_unchecked() }
} becomes example::xx:
push rbx
mov rbx, rdi
call qword ptr [rsi + 24]
mov rax, rbx
pop rbx
ret Both compiled with |
I think llvm didn't realize that |
huh, why is type_id not stored directly in the vtable? |
The short version is that Rust doesn't have a way to get associated constant data from a trait object (yet). |
The semantics of |
I'd like to point out that in all other places in the std, the |
Hey, is there a reason that |
@Wasabi375 It's not possible to unsize an unsized type, so it's equally not possible to downcast to an unsized type, because the source is already unsized. If you want to do something like that you need a double indirection. |
I think I might have found a bug with with type inference or something that confused me, because it claimed that I couldn't call |
I was curious if the code quality generated by https://rust.godbolt.org/z/77E7an7f1 Note that it seems to be condensing both functions to one since they're end product is identical. You can comment out the first one to explicitly see that the second one is equivalent. |
Feature gate:
#![feature(downcast_unchecked)]
This is a tracking issue for the
downcast_unchecked
,downcast_ref_unchecked
, anddowncast_mut_unchecked
methods.Public API
Steps / History
Unresolved Questions
The text was updated successfully, but these errors were encountered: