Skip to content

Add a MaxSize trait and derive macro#54

Merged
jamesmunns merged 6 commits intojamesmunns:mainfrom
lachlansneff:max-serialization-size
Jun 20, 2022
Merged

Add a MaxSize trait and derive macro#54
jamesmunns merged 6 commits intojamesmunns:mainfrom
lachlansneff:max-serialization-size

Conversation

@lachlansneff
Copy link
Copy Markdown
Contributor

@lachlansneff lachlansneff commented Jan 30, 2022

This adds a trait MaxSize:

pub trait MaxSize {
    /// The maximum possible size that the serialization of this
    /// type can have, in bytes.
    const POSTCARD_MAX_SIZE: usize;
}

and a derive macro that implements it for structs and enums.

I think this just needs a little more documentation and then it'll be ready to merge. I would like someone else to double check my logic for computing the varint size though.

The naming needs a little more work too. I'm not super happy with the trait and constant name.

Comment thread postcard-derive/src/lib.rs
@lachlansneff
Copy link
Copy Markdown
Contributor Author

Here's what the error looks like when the type of a field doesn't implement postcard::MaxSize.

error[E0277]: the size for values of type `str` cannot be known at compilation time
  --> tests/max_size.rs:62:9
   |
62 |         a: &'static str,
   |         ^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `str`
   = note: required because of the requirements on the impl of `MaxSize` for `&'static str`

error[E0277]: the trait bound `str: MaxSize` is not satisfied
  --> tests/max_size.rs:62:9
   |
62 |         a: &'static str,
   |         ^ the trait `MaxSize` is not implemented for `str`
   |
   = note: required because of the requirements on the impl of `MaxSize` for `&'static str`

@netlify
Copy link
Copy Markdown

netlify bot commented Jun 2, 2022

Deploy Preview for cute-starship-2d9c9b canceled.

Name Link
🔨 Latest commit f31d226
🔍 Latest deploy log https://app.netlify.com/sites/cute-starship-2d9c9b/deploys/6298e2d042bd3f0008cda7b0

@lachlansneff-parallel lachlansneff-parallel deleted the max-serialization-size branch June 2, 2022 16:18
@jamesmunns jamesmunns merged commit f31d226 into jamesmunns:main Jun 20, 2022
rosslannen pushed a commit to lucimobility/postcard that referenced this pull request Jan 30, 2026
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