-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle token_swapper impossible swap mapping with Error (#971)
* Handle token_swapper impossible swap mapping with Error This commit fixes the handling of invalid mapping requests in the token_swapper rustworkx-core function and the graph_token_swapper function in rustworkx that uses it. Previously if an invalid mapping was requested the function would internally panic because it always assumed there was a path in the graph to fulfill the user requested mapping. However, because the rustworkx-core function didn't support error returns a breaking api change is needed to add a result return type. * Fix formatting --------- Co-authored-by: Edwin Navarro <enavarro@comcast.net>
- Loading branch information
1 parent
997ef79
commit ffb1973
Showing
5 changed files
with
154 additions
and
28 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
releasenotes/notes/handle-invalid-mapping-token_swapper-55d5b045b0b55345.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- | ||
features: | ||
- | | ||
Added a new exception class :class:`~.InvalidMapping` which is raised when a function receives an invalid | ||
mapping. The sole user of this exception is the :func:`~.graph_token_swapper` which will raise it when | ||
the user provided mapping is not feasible on the provided graph. | ||
upgrade: | ||
- | | ||
The rustworkx function :func:`~.graph_token_swapper` now will raise an :class:`~.InvalidMapping` exception | ||
instead of a ``PanicException`` when an invalid mapping is requested. This was done because a | ||
``PanicException`` is difficult to catch by design as it is used to indicate an unhandled error. Using | ||
- | | ||
The return type of the ``rustworkx-core`` function ``token_swapper()`` has been changed | ||
from ``Vec<(NodeIndex, NodeIndex)>`` to be ``Result<Vec<(NodeIndex, NodeIndex)>, MapNotPossible>``. | ||
This change was necessary to return an expected error condition if a mapping is requested for a graph | ||
that is not possible. For example is if you have a disjoint graph and you're trying to map | ||
nodes without any connectivity: | ||
.. code-block:: rust | ||
use rustworkx_core::token_swapper; | ||
use rustworkx_core::petgraph; | ||
let g = petgraph::graph::UnGraph::<(), ()>::from_edges(&[(0, 1), (2, 3) ]); | ||
let mapping = HashMap::from([ | ||
(NodeIndex::new(2), NodeIndex::new(0)), | ||
(NodeIndex::new(1), NodeIndex::new(1)), | ||
(NodeIndex::new(0), NodeIndex::new(2)), | ||
(NodeIndex::new(3), NodeIndex::new(3)), | ||
]); | ||
token_swapper(&g, mapping, Some(10), Some(4), Some(50)); | ||
will now return ``Err(MapNotPossible)`` instead of panicking. If you were using this | ||
funciton before you'll need to handle the result type. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.