Repository navigation
postcard-schema-ng: Implement bounding on types - #306
Conversation
This allows us to calculate max size.
✅ Deploy Preview for cute-starship-2d9c9b canceled.
|
✅ Deploy Preview for cute-starship-2d9c9b canceled.
|
|
Thanks for reviving this! Very excited to remove our |
|
I do still need to update postcard-dyn. |
|
|
||
| impl Schema for uuid_v1_0::Uuid { | ||
| const SCHEMA: &'static DataModelType = &DataModelType::ByteArray { bounds: Some(16) }; | ||
| const SCHEMA: &'static DataModelType = &DataModelType::Seq { |
There was a problem hiding this comment.
I think ByteArray was correct given that the implementation uses serialize_bytes(): https://docs.rs/uuid/1.26.0/src/uuid/external/serde_support.rs.html#31
There was a problem hiding this comment.
Yeah... postcard doesn't actually discriminate well between "byte array" and "[u8]", and we declare the schema of [u8] as
impl<T: Schema> Schema for [T] {
const SCHEMA: &'static DataModelType = &DataModelType::Seq {
element: T::SCHEMA,
max_len: None,
};
}
I wonder if we should actually just discard ByteArray in -ng to avoid this being confusing.
There was a problem hiding this comment.
Given it's a distinct type in the data model, I think it's important to keep ByteArray around -- serializers can choose to serialize them differently than a sequence of bytes (though Postcard doesn't)
There was a problem hiding this comment.
The fact that [u8] can't be specialized to use serialize_bytes() is more of an implementation limitation than anything
There was a problem hiding this comment.
serializers can choose to serialize them differently than a sequence of bytes (though Postcard doesn't)
Are you using postcard-schema for not-postcard-format things? A long while back you asked "is postcard-schema for postcard only, or serde in general", and these days, I'm feeling a bit stronger about it only being for postcard.
There was a problem hiding this comment.
Are you using postcard-schema for not-postcard-format things?
Kinda sorta not really. postcard-schema is close to being useful as a general serde schema, but I've somewhat lost hope in full fledged dynamic (de)serialization with serde. Where possible though (including here), I think it's still valuable to have a 1:1 match between postcard-schema and serde's data model or other abstractions.
| let Some(bound) = bounds else { | ||
| return state; | ||
| }; | ||
| let mut state = hash_update(state, &[0x2B]); |
There was a problem hiding this comment.
When we are going to be hashing bounds (according to the hashing options), don't we need to hash 0x2B everywhere the optional bounds appear (even if None) to avoid hash collisions? If we only hash this prime when bounds are present, I think we can get into ambiguities where something like { name: [0x1], max_len: Some(0x5) } feeds the same bytes into the hasher as { name: [0x1, 0x2B, 0x5], max_len: None }
There was a problem hiding this comment.
I think #217 captures this, we should probably fix that for -ng.
There was a problem hiding this comment.
That probably covers it... though it still gives me a bad feeling to do it conditionally
This allows us to calculate max size.
This is a second attempt at what I started in #179.