Add no_std support#7
Conversation
|
Hi, sorry for this unprompted PR. I hope you'll find it useful. I needed to be able to implement certain feature as no_std, as this allows for a smaller shared library to be generated. Currently there are mature enough no_std yaml deserializers, and since this library now exsist and libyaml-rs is already no-std. Then a simple change would be required to allow This is quite straightforward PR. I've run all tests locally and they pass. LMK if you'd like to see anything changed in this PR |
e4f3c6d to
88ab3d8
Compare
748ae47 to
c7ad78d
Compare
| use std::mem; | ||
|
|
||
| /// Simple deterministic hasher for order-independent hashing of Mapping entries. | ||
| struct FnvHasher(u64); |
There was a problem hiding this comment.
Instead of pulling something like hashbrown and/or complicating feature resolution. For no_std I added this super simple hasher.
It's not cryptographic, but neither is DefaultHasher/SipHash in a security sense for HashMaps — both just need good distribution to avoid collisions.
There was a problem hiding this comment.
Pull request overview
This PR adapts yaml_serde to support no_std + alloc builds by introducing a std feature (default-on) and refactoring the crate to use core/alloc equivalents while abstracting I/O behind a crate io module.
Changes:
- Add
stdfeature (default) and gate reader-based APIs (from_reader) behind it. - Introduce
src/io.rsabstraction to replace directstd::iousage and update serializer/emitter to use it. - Replace
stdimports withcore/alloc, and introduce a no_std-compatible hashing strategy forMapping.
Reviewed changes
Copilot reviewed 25 out of 26 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/lib.rs | Switch crate to #![no_std], add alloc, gate from_reader, add io module |
| Cargo.toml | Add std feature (default), disable default features for deps to support no_std |
| src/io.rs | New std/no_std I/O abstraction layer (re-export std::io or custom Write) |
| src/de.rs | Gate from_reader and Progress::Read behind std, move to core/alloc imports |
| src/loader.rs | Gate Progress::Read handling behind std, move to alloc containers |
| src/ser.rs | Switch to crate::io and core/alloc imports for serialization path |
| src/error.rs | Replace std types with alloc/core equivalents; use crate::io::Error |
| src/mapping.rs | Use core/alloc; add no_std hasher + hasher-aware IndexMap type alias |
| src/number.rs | Replace std imports with core equivalents |
| src/path.rs | Replace std fmt import with core fmt |
| src/value/mod.rs | Replace std imports with core/alloc equivalents |
| src/value/de.rs | Replace std imports with core/alloc equivalents |
| src/value/ser.rs | Replace std imports with core/alloc equivalents; use core::result::Result |
| src/value/tagged.rs | Replace std imports with core/alloc equivalents |
| src/value/index.rs | Replace std imports with core/alloc equivalents |
| src/value/from.rs | Replace std Cow import with alloc::borrow::Cow; add alloc imports |
| src/value/debug.rs | Replace std fmt import with core fmt |
| src/value/partial_eq.rs | Add alloc::string::String import for no_std build |
| src/with.rs | Replace std fmt import with core fmt; add alloc imports in modules |
| src/libyaml/emitter.rs | Replace std usage with core/alloc; route output through crate::io |
| src/libyaml/parser.rs | Replace std usage with core/alloc equivalents |
| src/libyaml/error.rs | Replace std usage with core equivalents; use core::result::Result |
| src/libyaml/cstr.rs | Replace std usage with core equivalents |
| src/libyaml/tag.rs | Replace std usage with core/alloc equivalents |
| src/libyaml/util.rs | Replace std usage with core/alloc equivalents; allocate via Box |
| .gitignore | Ignore rust_out |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
I hope it wasn't my settings which pulled in copilot. But it posted some interesting points, I've updated the PR. and await further feedback |
Make the crate usable in no_std + alloc environments, following the serde_json pattern for io abstraction. Features: - std feature (default) gates from_reader and std::io - io module: re-exports std::io in std mode, provides minimal public Write trait + Error type in no_std mode (custom writers supported) - All std:: imports replaced with core::/alloc:: equivalents - FNV hasher for order-independent Mapping hashing (no extra deps) - FnvBuildHasher for IndexMap in no_std mode CI: - Add no_std build check (cargo check --no-default-features)
Make the crate usable in no_std + alloc environments. Follow the serde_json pattern: a custom io module abstracts over std::io in std mode and provides a minimal Write trait with Vec impl for no_std.
This allows embedding serde yaml in a lot more contexts. e.g. as a low size overhead shared library.
no_stdcompilation means that artifacts of few tens of kilobyte size can be produced.Also this could be useful for some embedded projects, e.g. allow a uC parsing yaml config (I know yaml is a bit unorthodox there, but someone could find it useful.
I need it to reduce the binary size of a shared library we use in one of our projects.
This PR:
stdfeature (default) that gatesfrom_readerand std::iosrc/io/module: re-exports std::io in std mode, provides custom Write trait + fallback Error in no_std mode