uuid-rs / uuid

Generate and parse UUIDs.

Home Page:https://www.crates.io/crates/uuid

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Optional support for borsh

grovesNL opened this issue Β· comments

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.