petgraph icon indicating copy to clipboard operation
petgraph copied to clipboard

Implement `Graph::try_map` to map a Graph with potential errors.

Open andriyDev opened this issue 6 months ago • 2 comments

Today we have Graph::map which allows you to create a new graph after processing it with some closures. This works, unless you can have errors in that conversion process. Then you have to manually build up the graph yourself.

It would be nice if we had something like Graph::try_map where its closures return Result and returns Result<Graph>.

For a concrete case, I am writing a graph to disk. Some values in the graph are not serializable, so returning an error from the closures allows me to handle the case where the graph cannot be converted to my "serialized form".

andriyDev avatar Jun 13 '25 06:06 andriyDev

Looking at the large number of possible variations of xxx_map, the idea arises to develop a number of traits with default implementations.

starovoid avatar Jun 13 '25 07:06 starovoid

What other variants are there? The only one I can think of is Option, but that you could just do with try_map().ok(). A separate function isn't really necessary imo.

Personally I'm against traits for these kinds of methods. It adds additional importing to callers and makes it much more difficult to discover.

andriyDev avatar Jun 15 '25 17:06 andriyDev