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

WIP: Add support for heapless::Vec #18

Closed
wants to merge 1 commit into from

Conversation

nickray
Copy link

@nickray nickray commented Dec 30, 2019

For embedded use cases, instead of alloc it's often preferable to use heapless: fixed-capacity strings and vectors. This patch would allow serializing Vec<u8> as byte strings instead of generic vectors.

A minimal example: https://github.com/nickray/serde-bytes-heapless-test/blob/master/src/lib.rs

I think it would be nice to have this patch here, would that make sense? The alternative being heapless having a Bytes type itself (rust-embedded/heapless#138).

Regarding tests, I'm not sure how they could be added.

Regarding error handling, I'm not sure if or how to map capacity errors when deserializing instead of horribly panicking.

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

I think I would prefer not to build this into serde_bytes, but it would be reasonable to maintain this in a separate crate. As long as it has:

use heapless::{ArrayLength, Vec};
use serde::{Deserialize, Deserializer, Serialize, Serializer};

pub fn serialize<T, N, S>(vec: &Vec<T, N>, serializer: S) -> Result<S::Ok, S::Error>
where
    T: Serialize,
    N: ArrayLength<T>,
    S: Serializer,
{...}

pub fn deserialize<'de, D, T, N>(deserializer: D) -> Result<Vec<T, N>, D::Error>
where
    D: Deserializer<'de>,
    T: Desererialize<'de>,
    N: ArrayLength<T>,
{...}

that will make #[serde(with = "heapless_serde_bytes")] work.

@dtolnay dtolnay closed this Jun 8, 2020
@nickray
Copy link
Author

nickray commented Jun 8, 2020

Understood. I am currently maintaining https://crates.io/crates/heapless-bytes and working towards merging this into heapless proper (rust-embedded/heapless#164).

@nickray nickray deleted the nickray-heapless-feature branch June 8, 2020 12:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants