-
Notifications
You must be signed in to change notification settings - Fork 88
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
Optionally implement Schema
for uuid::Uuid
#160
base: main
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for cute-starship-2d9c9b canceled.
|
It's a very commonly used type so it makes sense to provide optional support for it out of the box.
I will plan to merge this in a bit, but this impl is wrong, I believe. Uuid serializes to a #[cfg(feature = "uuid-v1_0")]
impl Schema for uuid::Uuid {
const SCHEMA: &'static NamedType = &NamedType {
name: "Uuid",
ty: <[u8; 16]>::SCHEMA.ty,
};
} |
👍
The |
Actually, it seems Uuid is serialized as a byte slice, so I think my impl is correct. |
Ahh, you're absolutely right - I saw that |
Double confirming: use uuid::Uuid;
fn main() {
let val = Uuid::now_v7();
let bytes = postcard::to_stdvec(&val).unwrap();
assert_eq!(bytes.len(), 16);
}
// thread 'main' panicked at src/main.rs:6:5:
// assertion `left == right` failed
// left: 17
// right: 16
// note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace Disappointing! |
Thanks.
Well, arrays are generally serialized as structs in serde and depending on the format, that could be much worse (besides it being very strange to encode a byte sequence as a struct). OTOH, if arrays were serialized as a sequence type, you'd still end up with the same length. |
In postcard at least, fixed sized arrays are serialized as essentially tuples, so I'm definitely less familiar with other serde formats would handle this tho! |
I meant tuples, yeah. In the D-Bus format, they've the same encoding so I sometimes get them confused. :)
That's saving one bytes but there is more processing and calls then because serde will call you encoder to serialize each byte separately. It could be that the compilers can optimize the 16 loops+calls but I wouldn't know about that. Still, postcard could choose to serialize byte slice like a tuple, if it wanted since there are special Serializer and Deserializer method for this type. |
It's a very commonly used type so it makes sense to provide optional support for it out of the box.