Skip to content

Implement Csr::try_add_edge#719

Merged
ABorgna merged 3 commits intopetgraph:masterfrom
starovoid:csr-rec-errors
Feb 1, 2025
Merged

Implement Csr::try_add_edge#719
ABorgna merged 3 commits intopetgraph:masterfrom
starovoid:csr-rec-errors

Conversation

@starovoid
Copy link
Copy Markdown
Collaborator

The Csr::try_add_edge function is implemented, which checks the given indexes and return possible error of type

/// The error type for fallible operations with `Csr`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CsrError {
    /// Both vertex indexes go outside the graph.
    IndicesOutBounds(usize, usize),
}

It is currently impractical to create checked counterparts of other Csr methods, as this will require significant code duplication. It's easier to make breaking changes in one of the further major releases, moving away from panic to Error and Option in the entire Csr API.

@ABorgna ABorgna self-requested a review February 1, 2025 10:37
Copy link
Copy Markdown
Member

@ABorgna ABorgna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

I realize we don't do it anywhere else, but should we derive std::error::Error for CsrError. (And similarly in your other PRs).

Comment thread src/csr.rs
@ABorgna
Copy link
Copy Markdown
Member

ABorgna commented Feb 1, 2025

It is currently impractical to create checked counterparts of other Csr methods, as this will require significant code duplication. It's easier to make breaking changes in one of the further major releases, moving away from panic to Error and Option in the entire Csr API.

Agreed. Mind adding a new issue, so we can track it in the milestone?

Co-authored-by: Agustín Borgna <agustinborgna@gmail.com>
@starovoid
Copy link
Copy Markdown
Collaborator Author

I realize we don't do it anywhere else, but should we derive std::error::Error for CsrError. (And similarly in your other PRs).

I think we shouldn't do this, since CsrError is already described using Display and Debug, for a combination of which the trait std::error::Error is implemented by default.

@ABorgna
Copy link
Copy Markdown
Member

ABorgna commented Feb 1, 2025

I don't think it is?

let err: &dyn std::error::Error = &CsrError::IndicesOutBounds(0, 0);
the trait bound `CsrError: StdError` is not satisfied

(btw, the tuple-struct error could use some named fields instead :) )

@starovoid
Copy link
Copy Markdown
Collaborator Author

Agreed. Mind adding a new issue, so we can track it in the milestone?

Good idea, opened #724

@starovoid
Copy link
Copy Markdown
Collaborator Author

I don't think it is?

let err: &dyn std::error::Error = &CsrError::IndicesOutBounds(0, 0);
the trait bound `CsrError: StdError` is not satisfied

Oh, I'll add such blancked implementation:

impl std::error::Error for CsrError {}

(btw, the tuple-struct error could use some named fields instead :) )

In this particular error, both tuple fields are equivalent, and the naming options node_ix_1 and node_ix_2 seem too verbose. I am glad for suggestions if there is a better naming option for the fields.

@ABorgna ABorgna merged commit 3fbb808 into petgraph:master Feb 1, 2025
@starovoid starovoid deleted the csr-rec-errors branch February 1, 2025 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants