Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Split serde bound for RaftError into serialize and deserialize #991

Merged
merged 1 commit into from
Jan 17, 2024

Conversation

tvsfx
Copy link
Contributor

@tvsfx tvsfx commented Jan 16, 2024

While implementing a struct that has a generic RaftError<E> as a field, I ran into some issues with trait bounds because RaftError does not make a distinction between the serde Serialize and Deserialize bounds. The consequence is that any impl block that requires my struct to be Serialize will require E to both be Serialize and DeserializeOwned, which is not necessary. This PR splits the two bounds.

Checklist

  • Updated guide with pertinent info (may not always apply).
  • Squash down commits to one or two logical commits which clearly describe the work you've done.
  • Unittest is a friend:)

This change is Reviewable

@drmingdrmer drmingdrmer self-requested a review January 17, 2024 03:22
Copy link
Member

@drmingdrmer drmingdrmer left a comment

Choose a reason for hiding this comment

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

Oh thank you! I've not considered such subtle scenario yet.

@drmingdrmer drmingdrmer added this pull request to the merge queue Jan 17, 2024
Merged via the queue into databendlabs:main with commit 55d9525 Jan 17, 2024
27 checks passed
@tvsfx
Copy link
Contributor Author

tvsfx commented Jan 17, 2024

FWIW, I was confused by the presence of this bound, so I looked into the way this concrete derivation works. I think the problem is not the bound on the E generic, but rather, the fact that NID is receiving two different Serialize/Deserialize bounds; one from the derive annotation, and one from the NID: NodeId trait bound. The following hence seems like a cleaner fix to me:

#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
+#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
-#[cfg_attr(
-    feature = "serde",
-    derive(serde::Deserialize, serde::Serialize),
-    serde(bound(serialize = "E: serde::Serialize")),
-    serde(bound(deserialize = "E: for <'d> serde::Deserialize<'d>"))
-)]
pub enum RaftError<NID, E = Infallible>
where NID: NodeId
{
    #[error(transparent)]
    APIError(E),

+  #[serde(bound = "")]
    #[error(transparent)]
    Fatal(#[from] Fatal<NID>),
}

drmingdrmer added a commit to drmingdrmer/openraft that referenced this pull request Jan 17, 2024
The problem is not the bound on the `E` generic, but rather, the fact
that `NID` is receiving two different `Serialize/Deserialize` bounds;
one from the derive annotation, and one from the `NID: NodeId` trait
bound.

This modification is from suggestions by @tvsfs about the serde bound:
databendlabs#991 (comment)
@drmingdrmer
Copy link
Member

Looks good. Let me check it in this PR:

drmingdrmer added a commit to drmingdrmer/openraft that referenced this pull request Jan 17, 2024
The problem is not the bound on the `E` generic, but rather, the fact
that `NID` is receiving two different `Serialize/Deserialize` bounds;
one from the derive annotation, and one from the `NID: NodeId` trait
bound.

This modification is from suggestions by @tvsfs about the serde bound:
databendlabs#991 (comment)
drmingdrmer added a commit to drmingdrmer/openraft that referenced this pull request Jan 17, 2024
The problem is not the bound on the `E` generic, but rather, the fact
that `NID` is receiving two different `Serialize/Deserialize` bounds;
one from the derive annotation, and one from the `NID: NodeId` trait
bound.

This modification is from suggestions by @tvsfx about the serde bound:
databendlabs#991 (comment)
drmingdrmer added a commit that referenced this pull request Jan 17, 2024
The problem is not the bound on the `E` generic, but rather, the fact
that `NID` is receiving two different `Serialize/Deserialize` bounds;
one from the derive annotation, and one from the `NID: NodeId` trait
bound.

This modification is from suggestions by @tvsfx about the serde bound:
#991 (comment)
@drmingdrmer drmingdrmer mentioned this pull request Mar 9, 2024
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