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

Optional support for borsh #685

Closed
grovesNL opened this issue Jun 15, 2023 · 0 comments · Fixed by #686
Closed

Optional support for borsh #685

grovesNL opened this issue Jun 15, 2023 · 0 comments · Fixed by #686

Comments

@grovesNL
Copy link
Contributor

Hi! 👋

I'm serializing some types using borsh but I can't implement traits like BorshSerialize and BorshDeserialize directly on Uuid types.

Currently I workaround this by using a newtype that implements those traits (using the u128 representation for borsh), like

pub struct Id(Uuid);

impl BorshSerialize for Id {
    fn serialize<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
        BorshSerialize::serialize(&self.0.as_u128(), writer)
    }
}

impl BorshDeserialize for Id{
    fn deserialize_reader<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
        Ok(Self(Uuid::from_u128(BorshDeserialize::deserialize_reader(
            reader,
        )?)))
    }
}

In practice these newtypes aren't very convenient though, because I end up converting between Uuid and Id pretty often. I was wondering if it might be useful to upstream this directly onto Uuid, like the optional serde feature?

I'd be happy to submit a PR if there's any interest in this.

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 a pull request may close this issue.

1 participant