I'm making a graph processing module in Rust. The core of the module models the idea of having multiple containers which hold the data in the graph. For example, I may have a graph whose inner structure is an HashMap or maybe AdjacencyMatrix etc.
These containers must implement a trait:
trait GraphData<V> {
fn has_edge(&self, v: &V, u: &V) -> bool;
fn nodes(&self) -> Iterator<V>; // Here's the problem...
}
I can't just return a trait in my trait definition. I know I must use trait object, but I don't want to Box it. I would like to make the container provide its own NodeIter struct. However, I would be stuck with the same problem explained in Associated type constructors, part 1: basic concepts and introduction. The post explains about associated type constructors (ATC) which do not exist in Rust now. My GraphData resembles the generic Collection described.
Is there any workaround I could use to "simulate" ATC or any pattern specific to Rust I could use for this situation?
I don't want to depend on dynamic dispatch and resort to using Box or the dyn keyword.
I thought to define a struct NodeIter for each type of graph container
I created in my module and add "nodes" inside the implementation of the container itself. However, I find this to be poor code reuse.