Add as_inner helper method on trait objects#4160
Conversation
Avoids issues with calling `.as_any().downcast_ref()` on smart pointers.
98652e5 to
354891d
Compare
kchibisov
left a comment
There was a problem hiding this comment.
Could we remove the impl <T: Any> stuff from utils?
|
You'd also need a mutable counter-part, and I'm also not sure how casts that take ownership would work. E.g. when you cast something inside the I'd also name it as |
Not yet, but
Added
I chose |
There's nothing |
The point was that it's easy to mess things up with it, thus I want it removed. Not as any, but the |
I'd probably be fine with
But the problematic part isn't actually the If you want, we can rename the |
|
I mean, my solution solved all of that, method was present only where it was needed and expected. |
Yeah, but it also introduced an extra API that all backends have to implement, which feels ugly IMO. Besides, regardless of resolving the problem at hand, I believe the methods |
|
Sure. The reason why I've brought the API is to ensure that the casts actually cast to the right thing, despite of how we write them and e.g. you won't be tempted to use Like I was debugging an issue where my cast wasn't working during implementing an API, thus I wanted some mechanism in place to prevent issues like that to ever appear. Yes, it results in a bit more code to get things more type-safe, because of rust's implicit |
They can be difficult to use correctly.
I fully agree that this is a big issue, and that we should resolve it! I've renamed the My biggest problem with |
Done. I think I slightly prefer |
43d051b to
cb43200
Compare
|
@madsmtm could you check if the latest commit is fine? I just don't like that we'd have to duplicate the actual code for pretty much every opaque object we add into the We could probably use a |
8e8659c to
578f837
Compare
|
Added one more commit that restricts the casting, but uses macro for that. I think bounding by the |
madsmtm
left a comment
There was a problem hiding this comment.
Nice! I like the macro, pushed a commit to fix some docs, but otherwise excellent!
Avoids issues with calling
.as_any().downcast_ref()on smart pointers, and makes it simpler to convert into the backend type, and clearer which kind of conversion we support. Alternative to #4157.We should also be able to completely remove
AsAnyonce we have MSRV 1.86, which is why I've addedAnyas a supertrait of that.It's actually a miracle that
crate::monitor::MonitorHandle->dyn crate::monitor::MonitorHandleProvider->.as_any->platform_impl::MonitorHandlepath that we use currently works at all (which it only does because all method calls do an implicit deref).changelogmodule if knowledge of this change could be valuable to users