Storing Strings in a data structure that requires Copy types?

I'm trying to use the petgraph GraphMap data structure to store OsStrings. However, that container requires that node types implement {Copy + Ord + Hash}, which isn't true for OsStrings.

What's the right way to handle this? I thought using Rc might work as a wrapper, but Rc isn't Copy either.

(I have a workaround for now that uses a normal Graph and a separate HashMap to check for existing nodes, both of which contain cloned copies of the strings, but this seems a bit silly.)

GraphMap is not suitable for that, because the doc says β€œThe node weight N must implement Copy and will be used as node identifier, duplicated into several places in the data structure”, so if it did work, it would be the same kind of sillyness.

I think GraphMap can be rewritten to work like you (maybe) imagined it would, but it hasn't happened yet.

In such a situation, if I could not change the library, I would use a map to assign integers to the strings (with a vector for the mapping in the other direction), and use only the integers as the node types. Depending on what you do, this could even be reasonably efficient.

I assume that the library does not create node types on its own, but I don't see how this is possible based on the NodeTrait definition.

1 Like