Skip to content

Add no_std support#7

Open
pawelchcki wants to merge 1 commit intoyaml:mainfrom
pawelchcki:no-std
Open

Add no_std support#7
pawelchcki wants to merge 1 commit intoyaml:mainfrom
pawelchcki:no-std

Conversation

@pawelchcki
Copy link
Copy Markdown

@pawelchcki pawelchcki commented Apr 7, 2026

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_std compilation 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:

  • Add std feature (default) that gates from_reader and std::io
  • Add src/io/ module: re-exports std::io in std mode, provides custom Write trait + fallback Error in no_std mode
  • Replace all std:: imports with core::/alloc:: equivalents
  • Replace DefaultHasher with a simple FNV hasher for order-independent Mapping hashing (no_std compatible, no extra dependencies)
  • Use FnvBuildHasher for IndexMap in no_std mode (avoids pulling in hashbrown)

@pawelchcki
Copy link
Copy Markdown
Author

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 no_std operation. I don't expect Embedded developers to parse a lot of yaml, although maybe?

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

Comment thread src/mapping.rs Outdated
use std::mem;

/// Simple deterministic hasher for order-independent hashing of Mapping entries.
struct FnvHasher(u64);
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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.

@pawelchcki pawelchcki marked this pull request as ready for review April 7, 2026 21:51
Copilot AI review requested due to automatic review settings April 7, 2026 21:51
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 std feature (default) and gate reader-based APIs (from_reader) behind it.
  • Introduce src/io.rs abstraction to replace direct std::io usage and update serializer/emitter to use it.
  • Replace std imports with core/alloc, and introduce a no_std-compatible hashing strategy for Mapping.

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.

Comment thread src/io.rs Outdated
Comment thread src/io.rs
Comment thread src/mapping.rs
Comment thread Cargo.toml
@pawelchcki
Copy link
Copy Markdown
Author

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)
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