Skip to content
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

Impl MaxSize for heapless::Vec #198

Closed
HaoboGu opened this issue Dec 27, 2024 · 1 comment
Closed

Impl MaxSize for heapless::Vec #198

HaoboGu opened this issue Dec 27, 2024 · 1 comment

Comments

@HaoboGu
Copy link

HaoboGu commented Dec 27, 2024

Hello, is it possible to implement MaxSize for heapless::Vec? I found that Se/De traits are already implemented, but MaxSize does not:

My Cargo.toml:

heapless = {version = "0.8.0", features = ["serde"] }
postcard = { version = "1", features = ["experimental-derive", "use-defmt", "heapless"] }

My structs:

#[derive(Serialize, Deserialize, Clone, Debug, Format, MaxSize)]
pub struct A<const N: usize> {
    pub value: Vec<u8, N>
}

Error log:

error[E0277]: the trait bound `Vec<u8, N>: MaxSize` is not satisfied
  --> /Users/haobogu/Projects/keyboard/rmk/rmk/src/event.rs:29:16
   |
29 |     pub value: Vec<u8, N>
   |                ^^^^^^^^^^ the trait `MaxSize` is not implemented for `Vec<u8, N>`
   |
   = help: the following other types implement trait `MaxSize`:
             &T
             &mut T
             ()
             (A, B)
             (A, B, C)
             (A, B, C, D)
             (A, B, C, D, E)
             (A, B, C, D, E, F)
           and 41 others


@max-heller
Copy link
Collaborator

postcard does actually implement MaxSize for heapless::Vec

#[cfg(feature = "heapless")]
#[cfg_attr(docsrs, doc(cfg(feature = "heapless")))]
impl<T: MaxSize, const N: usize> MaxSize for heapless::Vec<T, N> {
const POSTCARD_MAX_SIZE: usize = <[T; N]>::POSTCARD_MAX_SIZE + varint_size(N);
}

... just for a different version of heapless (0.7).
[dependencies.heapless]
version = "0.7.0"

Unfortunately, postcard can't upgrade its heapless dependency without a breaking change (see #165). However, MaxSize will likely soon be based on postcard-schema's Schema trait (see #179), and postcard-schema has a way to support multiple versions of heapless based on crate features, so the MaxSize rework will likely fix this before postcard 2.0 releases.

[dependencies.heapless_v0_7]
package = "heapless"
version = "0.7"
default-features = false
optional = true
[dependencies.heapless_v0_8]
package = "heapless"
version = "0.8"
default-features = false
optional = true

Closing as redundant with #165 and #179.

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

No branches or pull requests

2 participants