Constrained anymap::AnyMap

An anymap::AnyMap can hold any value. Is there a way to constraint this to say

anymap::AnyMap where we only hold values T where T: SomeTrait ?

I think you may want to use typedmap for that. anymap doesn't have such functionality (unless the bound you want is any combination of Send, Sync and Clone). See typedmap::bounds - Rust for documentation about custom bounds.

You can make Any a supertrait bound of your trait, implement manual supertrait upcasting, and make your own HashMap<TypeId, Box<dyn YourTrait>>.

If you don't want the supertrait bound, you can still manually implement downcasting for 'static types via TypeId ala dyn Error.

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.