Trait object with associated types in field - requires generic param?

2026-10-08来源 Rust Users ForumEnglish

I currently have:

trait ListModel { fn get_stuff(&self, row: i) -> Stuff; ... } struct List { model: Rc<dyn ListModel>, ... } 

I think I want to change my model to use row IDs instead of usize indices. If I do this:

trait ListModel { type Key: Eq + Hash + Clone; fn get_stuff(&self, row: Key) -> Stuff; } 

There is no way to keep List non-generic, correct? I need to have something like these? :

struct List<M: ListModel> { model: Rc<M>, } // or struct List<K> { model: Rc<dyn ListModel<Key = K>>, ... } 

1 post - 1 participant

Read full topic

原文:https://www.a8q9.cn/en/article/trait-object-with-associated-types-in-field-requires-generic-param-3be75c.html

相关文章