Skip to content

postcard-schema-ng: Implement bounding on types - #306

Merged
jamesmunns merged 13 commits into
mainfrom
james/max-size-attempt-2
Sep 21, 2026
Merged

jamesmunns merged 13 commits into
mainfrom
james/max-size-attempt-2

Conversation

@jamesmunns

Copy link
Copy Markdown
Owner

This allows us to calculate max size.

This is a second attempt at what I started in #179.

This allows us to calculate max size.
@netlify

netlify Bot commented Sep 5, 2026

Copy link
Copy Markdown

✅ Deploy Preview for cute-starship-2d9c9b canceled.

Name Link
🔨 Latest commit 26f5aae
🔍 Latest deploy log https://app.netlify.com/projects/cute-starship-2d9c9b/deploys/6a9b5bf2f3e5a60008d57e76

@netlify

netlify Bot commented Sep 5, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for cute-starship-2d9c9b canceled.

Name Link
🔨 Latest commit 6750f40
🔍 Latest deploy log https://app.netlify.com/projects/cute-starship-2d9c9b/deploys/6ab0b516d6a07a000863e22f

Comment thread source/postcard-schema-ng/src/key/hash.rs
Comment thread source/postcard-schema-ng/src/impls/chrono_v0_4.rs Outdated
Comment thread source/postcard-schema-ng/src/schema/mod.rs Outdated
Comment thread source/postcard-schema-ng/src/schema/mod.rs
Comment thread source/postcard-schema-ng/src/schema/mod.rs Outdated
Comment thread source/postcard-schema-ng/src/schema/mod.rs Outdated
Comment thread source/postcard-schema-ng/src/schema/mod.rs Outdated
Comment thread source/postcard-schema-ng/src/schema/fmt.rs Outdated
Comment thread source/postcard-schema-ng/src/schema/fmt.rs Outdated
Comment thread source/postcard-schema-ng/src/schema/fmt.rs Outdated
Comment thread source/postcard-schema-ng/src/key/hash.rs
@max-heller

Copy link
Copy Markdown
Collaborator

Thanks for reviving this! Very excited to remove our MaxSize derive

@jamesmunns

Copy link
Copy Markdown
Owner Author

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 {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that [u8] can't be specialized to use serialize_bytes() is more of an implementation limitation than anything

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread source/postcard-schema-ng/src/schema/fmt.rs Outdated
Comment thread source/postcard-schema-ng/src/impls/chrono_v0_4.rs Outdated
Comment thread source/postcard-schema-ng/src/schema/mod.rs Outdated
let Some(bound) = bounds else {
return state;
};
let mut state = hash_update(state, &[0x2B]);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 }

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think #217 captures this, we should probably fix that for -ng.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That probably covers it... though it still gives me a bad feeling to do it conditionally

Comment thread source/postcard-schema-ng/src/key/hash.rs
@jamesmunns
jamesmunns merged commit 872b908 into main Sep 21, 2026
5 checks passed
@jamesmunns
jamesmunns deleted the james/max-size-attempt-2 branch September 21, 2026 05:32
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 this pull request may close these issues.

2 participants