does that mean the type of the key is opaque to the caller? if that's the case, than it can be an erased type chosed by the implementor.
however, you still need to query the key (let's call it the internal key) from the model in the first place, no? how do you do that? you must use some type (let's call it the external key type) that is known to the caller, like an integer index, right?
so if I understand this correctly, the opaque "key" type (the internal key) really is an implementation detail? then why expose it in the api? why not just use the external key directly?
my point is, an associated type is chosen by the implementor, not the caller. you can erase the type at compile time with some indirections, but you CANNOT eliminate the type check, it just moves to runtime.
in any case, I feel like this is probably an XY problem. whatever the referenced OOP pattern is, this is definitely not the best way to "rustify" it.
in traditional GUI framework, the interface between a widget and its data model is defined by the widget, not by the data. it's the implementor's job to create an adapter when the data shape does not fit the model api.
in the linked java doc, the tree traversal api of the TreeModel interface looks like this:
as you can see, it uses integer indices to locate a node, the implementor cannot choose a different key type.
I didn't find a link to a TreeModel from the java docs, but I imagine a trait that is most faithful to the java design might look like this:
// emulate the java `Object` class, to represent a fully erased Node
// use `Any` as super trait to support downcasting
trait Object: Any {
/// for display purpose, mirror of `ToString::to_string()`
/// alternatively, can also mirror `Display::fmt()`
fn to_string(&self) -> String;
fn eq(&self, other: &dyn Object) -> bool;
fn hash(&self, hasher: &mut dyn Hahser);
fn clone(&self) -> Box<dyn Object>;
}
trait ListModel {
fn stuff_count(&self) -> usize;
/// depending on the design, alternatives may return different type than Box
/// for example, `&'_ dyn Object`, `Arc<dyn Object>`, etc
fn get_stuff(&self, i: usize) -> Box<dyn Object>;
}
even if you were to mimic the java api, add an associated Key type (and then just to erase it when stored???) definitely makes little sense. but again, the java api isn't the most idomatic way to design a rust gui library anyway.
what's the best solution? well, it's hard to give one single best, or a better answer, without the full context of your use case. but many rust gui libraries either use some functional style api or immediate mode api, generally they don't use OOP style "abstract" interface to represent data models. you usually just use simple data structure of value types (e.g. &str) instead of some opaque "objects". to render arbitrary data structure, just convert them to strings.
for example, in iced, to display a list of items, you just use an array/slice of any type that implements Display:
let options = ["item 1", "item 2", "item 3"];
let selected = Some("item 2");
pick_list(&options, selected, on_selection_change).into()