utils: implement AsAny only on OpaqueObject#4157
utils: implement AsAny only on OpaqueObject#4157kchibisov wants to merge 1 commit intorust-windowing:masterfrom
Conversation
|
That ensures that the we cast to the right thing, since in some cases it was able to cast to wrapping container instead of the type that is wrapped. It's a bit annoying, but should help with random bugs. |
Given that `AsAny` was implemented for pretty much anything, it was really easy to use it on objects that were not really implementing AsAny.
There was a problem hiding this comment.
Hmm, I don't really see the value of this, so will neither "approve" nor "request changes" (if you're really blocked by this, then I'm fine with merging it).
The plan is to get rid of the platform modules anyhow, right? When would you hit this otherwise? In user code? If so, then I'd rather expose something like the following:
impl dyn Window {
pub fn as_inner<T: 'static + Window>(&self) -> Option<&T> {
let this: &(dyn std::any::Any + '_) = self.as_any();
this.downcast_ref::<T>()
}
}
// Similarly for ActiveEventLoop, MonitorHandle, etc.This is also a bit cleaner than the self.as_any().downcast_ref::<crate::platform_impl::Window>().unwrap() that we do everywhere now, would simplify to self.as_inner::<crate::platform_impl::Window>().unwrap().
(I consider AsAny a hack anyhow, and one that we'll be able to remove once we have MSRV 1.86, since by then rust-lang/rust#65991 will have landed).
|
The problem here is that It's mostly to not make a mistake in internal code, since |
|
I've opened #4160, I prefer that alternative. |
Given that
AsAnywas implemented for pretty much anything, it was really easy to use it on objects that were not really implementing AsAny.