Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

json! unusable with no_std and feature alloc #1164

Closed
jendrikw opened this issue Aug 1, 2024 · 1 comment · Fixed by #1166
Closed

json! unusable with no_std and feature alloc #1164

jendrikw opened this issue Aug 1, 2024 · 1 comment · Fixed by #1166

Comments

@jendrikw
Copy link

jendrikw commented Aug 1, 2024

When you use an array inside the json! macro, the expansion calls vec! unqualified. This is a problem in a no_std environment, because it requires the user to use alloc::vec. Which is weird and no expected from a macro.

Reproducer on rustexplorer: https://www.rustexplorer.com/b#LyoKW2RlcGVuZGVuY2llc10Kc2VyZGVfanNvbiA9IHsgdmVyc2lvbiA9ICIxLjAuMTIwIiwgZGVmYXVsdC1mZWF0dXJlcyA9IGZhbHNlLCBmZWF0dXJlcyA9IFsiYWxsb2MiXSB9CiovCgojIVtub19zdGRdCgpleHRlcm4gY3JhdGUgYWxsb2M7CgpmbiBtYWluKCkgewogICAgc2VyZGVfanNvbjo6anNvbiEoWyIiLCAiIl0pOwp9Cg==

Code:

/*
[dependencies]
serde_json = { version = "1.0.120", default-features = false, features = ["alloc"] }
*/

#![no_std]

extern crate alloc;

fn main() {
    serde_json::json!(["", ""]);
}

I think this issue has a simple solution by changing vec! to ::alloc::vec!, unless I'm missing something.

@jendrikw
Copy link
Author

jendrikw commented Aug 1, 2024

Wow that was quick. Thank you.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant