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

Make &'a Uuid serializable

GaelicGabe opened this issue · comments

Currently this code won't compile due to &Uuid not implementing Serialize.

#[derive(Serialize, Deserialize)]
struct Foo<'a> {
    id: &'a Uuid,
}

This seems clunky, and implementing this for &Uuid seems trivial. Is there a reason it's not been done? The only reason I can imagine is that there is some anti-pattern found in using &Uuid and therefore you are discouraging it?

Hi @GaelicGabe 👋

I'm actually a little surprised that serde doesn't have a blanket impl<'a, S: Serialize + ?Sized> Serialize for &'a S. Since the Uuid type is Copy though you shouldn't really need to store it as &'a Uuid, unless there's a specific reason you've ended up wanting to do that.

Hi @GaelicGabe 👋

I'm actually a little surprised that serde doesn't have a blanket impl<'a, S: Serialize + ?Sized> Serialize for &'a S. Since the Uuid type is Copy though you shouldn't really need to store it as &'a Uuid, unless there's a specific reason you've ended up wanting to do that.

Yeah, that surprises me as well. I can't remember what prompted me to open the issue, so it obviously wasn't so critical as not to be overcome. I agree that it seems a bit silly to store a reference to Uuid since it's Copy.
The tradeoff is then between the slight improvement of ergonomics vs the extra code complexity. I leave that up to you guys to decide. Close this issue if it feels unnecessary! :)

I think Rust’s coherence rules will prevent us from implementing Serialize for &Uuid anyways so will go ahead and close this one.

I would be interested to see whether any attempt has been made in the past to add that blanket impl to serde since it seems like it’s almost always desirable.

Thanks again for the report!