Match boxed type

I have a trait Component and a variable let components: Vec<Box<dyn Component>> i need a function which returns true if components contains a value of type T

I was thinking something like this

fn has_component<T: Component>(entities: Vec<Box<dyn Component>>) -> bool;

I have tried messing around with std::any::type_name and TypeId with no luck. :confused:

How would i implement this?

You could include the query in the trait. are_you_T methods for all T you want to check.

The Any trait with downcasting may help. This article has a section with good details.

Here's one way that doesn't force a 'static bound on your trait.

Based on this approach (which is based on the std trait Error).

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.