#Object-safe trait {up,down}casting?

12 messages · Page 1 of 1 (latest)

teal siren
#

I'm making a GTK app that I want to interact with multiple different online services, but some online services offer more features than others. So, what I would like to do is somehow test the features that a service offers at runtime.

Lets say I have a trait -- Service, that is the supertrait of several other traits, AccountService, FeedService, etc. How might I make it so a Box<dyn Service> could be downcasted to a Box<dyn AccountService>?

#

Object-safe trait {up,down}casting?

humble nest
#

Ono, subtyping detected 🚨

#

To my understanding you can't do it in Rust, you would have to resort to full-on dynamic typing using Any

#

(Because the methods from Any trait only work on dyn Any, so a subtrait of Any won't work)

teal siren
#

Oh wonderful

humble nest
#

You don't want subtyping

#

Well in some edge cases like servo yes

teal siren
#

What might work better in this case?

humble nest
#

Is there anything stopping you from using enums?

teal siren
#

Hmmm... now that I think about it not really

#

I'm too used to writing library code, lol