-
Notifications
You must be signed in to change notification settings - Fork 3
Docs/add no std usage examples #32
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
Merged
AteebNoOne
merged 3 commits into
iTeebot:main
from
anidroid1184:docs/add-no-std-usage-examples
Jul 24, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| # `no_std` Usage | ||
|
|
||
| The `tinyxml2` crate supports `no_std` builds that rely on the `alloc` crate for | ||
| dynamic memory (`Vec`, `String`, `Box`). It does **not** target bare-metal | ||
| `core`-only environments β a global allocator must be present. | ||
|
|
||
| The `std` feature is **enabled by default**. Disable it for embedded kernels, | ||
| RTOS environments, or custom targets where the standard library is unavailable. | ||
|
|
||
| > [!NOTE] | ||
| > This page focuses on embedded / non-WASM `no_std` targets. For | ||
| > WebAssembly-specific details (Emscripten, WASI SDK, browser host boundary), | ||
| > see [`architecture/wasm.md`](wasm.md). | ||
|
|
||
| ## Cargo.toml Configuration | ||
|
|
||
| ```toml | ||
| [dependencies] | ||
| tinyxml2 = { version = "1", default-features = false } | ||
| ``` | ||
|
|
||
| Disabling `default-features` removes the `std` feature flag. The crate then | ||
| applies `#![no_std]` and links against `alloc` exclusively. | ||
|
|
||
| If you are building a `no_std` binary crate (not just a library), you must also | ||
| provide: | ||
|
|
||
| ```rust | ||
| // src/main.rs or src/lib.rs of the consuming crate | ||
| #![no_std] | ||
|
|
||
| extern crate alloc; | ||
|
|
||
| #[panic_handler] | ||
| fn panic(_info: &core::panic::PanicInfo) -> ! { | ||
| loop {} | ||
| } | ||
|
|
||
| // Example: use `embedded-alloc`, `wee_alloc`, or a custom allocator | ||
| // #[global_allocator] | ||
| // static ALLOCATOR: MyAllocator = MyAllocator; | ||
| ``` | ||
|
|
||
| ## Minimal `no_std` Example | ||
|
|
||
| ```rust | ||
| #![no_std] | ||
|
|
||
| extern crate alloc; | ||
|
|
||
| use tinyxml2::Document; | ||
|
|
||
| fn parse_and_extract(xml: &str) -> Option<()> { | ||
| let mut doc = Document::parse(xml).ok()?; | ||
| let root = doc.first_child_element(doc.root(), Some("config"))?; | ||
|
|
||
| // Read an attribute | ||
| let version = doc | ||
| .element_ref(root)? | ||
| .attribute("version") | ||
| .unwrap_or("unknown"); | ||
| // -- snip: use `version` via a no_std-compatible output like defmt or serial | ||
|
|
||
| // Serialize back to compact XML (always available, returns alloc::string::String) | ||
| let compact = doc.to_string_compact(); | ||
| // -- snip: send `compact` over UART, defmt, log buffer, etc. | ||
|
|
||
| Some(()) | ||
| } | ||
| ``` | ||
|
|
||
| All in-memory parsing and DOM operations work without `std`: | ||
|
|
||
| - `Document::parse`, `Document::parse_str`, `Document::parse_bytes`, | ||
| `Document::parse_bytes_mut` | ||
| - DOM creation, mutation, and traversal (`NodeId`, iterators, `NodeRef`, | ||
| `ElementRef`) | ||
| - Entity encode/decode | ||
| - `XmlVisitor` trait and `Document::accept` | ||
| - `Document::to_string`, `Document::to_string_compact` | ||
| - `XmlPrinter` (push-based streaming serialiser) | ||
| - `Handle` / `HandleMut` navigation wrappers | ||
|
|
||
| ## API Availability | ||
|
|
||
| | API | `default-features = true` (std) | `default-features = false` (no\_std) | | ||
| |---|---|---| | ||
| | `Document::parse` / `parse_str` / `parse_bytes` / `parse_bytes_mut` | β | β | | ||
| | `Document::load_file` / `load_file_mut` | β | β | | ||
| | `Document::save_file` / `save_file_compact` | β | β | | ||
| | `Document::save_writer` / `save_writer_compact` | β | β | | ||
| | `Document::to_string` / `to_string_compact` | β | β | | ||
| | DOM mutation, iterators, visitors | β | β | | ||
| | `XmlPrinter` | β | β | | ||
| | `XmlError::Io` variant | β | β | | ||
| | `impl std::error::Error for XmlError` | β | β | | ||
| | `impl From<std::io::Error> for XmlError` | β | β | | ||
|
|
||
| The six file-oriented methods (`load_file`, `load_file_mut`, `save_file`, | ||
| `save_file_compact`, `save_writer`, `save_writer_compact`) and the | ||
| `XmlError::Io` variant are compiled only when the `std` feature is active. Their | ||
| signatures reference `std::path::Path` and `std::io::Write`, which are | ||
| unavailable without the standard library. | ||
|
|
||
| ## Known Limitations | ||
|
|
||
| 1. **A global allocator is required.** The DOM stores node names, attribute | ||
| values, text content, and child lists in `String` and `Vec` from `alloc`. | ||
| `core`-only (no heap) targets are not supported. | ||
|
|
||
| 2. **No file I/O.** `Document::load_file` and `Document::save_file` need the | ||
| `std` feature. Use `Document::parse_bytes` / `to_string` and let the | ||
| application layer handle loading bytes from a filesystem, flash, or network. | ||
|
|
||
| 3. **No `std::io::Write` streaming.** `Document::save_writer` and | ||
| `save_writer_compact` are `std`-only. Use `to_string` / `to_string_compact` | ||
| or the `XmlPrinter` streaming builder instead. | ||
|
|
||
| 4. **No `std::error::Error` impl.** The `std::error::Error` trait | ||
| implementation for `XmlError` is absent without `std`. Use | ||
| `XmlError::code()` (returns `u32`) or match on variants directly for | ||
| error handling. | ||
|
|
||
| 5. **All Cargo examples require `std`.** The registered `[[example]]` targets | ||
| are executables and need `fn main()`. The code patterns inside | ||
| `examples/wasm_parse.rs` are portable and can be copied into a `no_std` | ||
| binary crate. | ||
|
|
||
| ## Verification | ||
|
|
||
| The project's CI validates these build variants (`wasm-build.yml`): | ||
|
|
||
| ```bash | ||
| # Native target, no_std (library only) | ||
| cargo build -p tinyxml2 --no-default-features --lib | ||
|
|
||
| # wasm32-unknown-unknown (browser), with and without std | ||
| cargo build -p tinyxml2 --target wasm32-unknown-unknown | ||
| cargo build -p tinyxml2 --no-default-features --target wasm32-unknown-unknown --lib | ||
|
|
||
| # wasm32-wasip1 (WASI hosts), with and without std | ||
| cargo build -p tinyxml2 --target wasm32-wasip1 | ||
| cargo build -p tinyxml2 --no-default-features --target wasm32-wasip1 --lib | ||
| ``` | ||
|
|
||
| To check against an embedded ARM target (requires `rustup target add`): | ||
|
|
||
| ```bash | ||
| rustup target add thumbv7em-none-eabihf | ||
| cargo check -p tinyxml2 --no-default-features --target thumbv7em-none-eabihf --lib | ||
| ``` | ||
|
|
||
| Replace `thumbv7em-none-eabihf` with your target triple (e.g. | ||
| `riscv32imac-unknown-none-elf`, `aarch64-unknown-none`). | ||
|
|
||
| > [!TIP] | ||
| > Use `cargo check --lib` for no\_std targets β it validates the crate | ||
| > compiles without needing a linker script, panic handler, or allocator in | ||
| > scope. Full `cargo build` requires a binary entry point from a consuming | ||
| > crate. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| # `no_std` Usage | ||
|
|
||
| The `tinyxml2` crate supports `no_std` builds that rely on the `alloc` crate for | ||
| dynamic memory (`Vec`, `String`, `Box`). It does **not** target bare-metal | ||
| `core`-only environments β a global allocator must be present. | ||
|
|
||
| The `std` feature is **enabled by default**. Disable it for embedded kernels, | ||
| RTOS environments, or custom targets where the standard library is unavailable. | ||
|
|
||
| > [!NOTE] | ||
| > This page focuses on embedded / non-WASM `no_std` targets. For | ||
| > WebAssembly-specific details (Emscripten, WASI SDK, browser host boundary), | ||
| > see [`docs/architecture/wasm.md`](wasm.md). | ||
|
|
||
| ## Cargo.toml Configuration | ||
|
|
||
| ```toml | ||
| [dependencies] | ||
| tinyxml2 = { version = "1", default-features = false } | ||
| ``` | ||
|
|
||
| Disabling `default-features` removes the `std` feature flag. The crate then | ||
| applies `#![no_std]` and links against `alloc` exclusively. | ||
|
|
||
| If you are building a `no_std` binary crate (not just a library), you must also | ||
| provide: | ||
|
|
||
| ```rust | ||
| // src/main.rs or src/lib.rs of the consuming crate | ||
| #![no_std] | ||
|
|
||
| extern crate alloc; | ||
|
|
||
| #[panic_handler] | ||
| fn panic(_info: &core::panic::PanicInfo) -> ! { | ||
| loop {} | ||
| } | ||
|
|
||
| // Example: use `embedded-alloc`, `wee_alloc`, or a custom allocator | ||
| // #[global_allocator] | ||
| // static ALLOCATOR: MyAllocator = MyAllocator; | ||
| ``` | ||
|
|
||
| ## Minimal `no_std` Example | ||
|
|
||
| ```rust | ||
| #![no_std] | ||
|
|
||
| extern crate alloc; | ||
|
|
||
| use tinyxml2::Document; | ||
|
|
||
| fn parse_and_extract(xml: &str) -> Option<()> { | ||
| let mut doc = Document::parse(xml).ok()?; | ||
| let root = doc.first_child_element(doc.root(), Some("config"))?; | ||
|
|
||
| // Read an attribute | ||
| let version = doc | ||
| .element_ref(root)? | ||
| .attribute("version") | ||
| .unwrap_or("unknown"); | ||
| // -- snip: use `version` via a no_std-compatible output like defmt or serial | ||
|
|
||
| // Serialize back to compact XML (always available, returns alloc::string::String) | ||
| let compact = doc.to_string_compact(); | ||
| // -- snip: send `compact` over UART, defmt, log buffer, etc. | ||
|
|
||
| Some(()) | ||
| } | ||
| ``` | ||
|
|
||
| All in-memory parsing and DOM operations work without `std`: | ||
|
|
||
| - `Document::parse`, `Document::parse_str`, `Document::parse_bytes`, | ||
| `Document::parse_bytes_mut` | ||
| - DOM creation, mutation, and traversal (`NodeId`, iterators, `NodeRef`, | ||
| `ElementRef`) | ||
| - Entity encode/decode | ||
| - `XmlVisitor` trait and `Document::accept` | ||
| - `Document::to_string`, `Document::to_string_compact` | ||
| - `XmlPrinter` (push-based streaming serialiser) | ||
| - `Handle` / `HandleMut` navigation wrappers | ||
|
|
||
| ## API Availability | ||
|
|
||
| | API | `default-features = true` (std) | `default-features = false` (no\_std) | | ||
| |---|---|---| | ||
| | `Document::parse` / `parse_str` / `parse_bytes` / `parse_bytes_mut` | β | β | | ||
| | `Document::load_file` / `load_file_mut` | β | β | | ||
| | `Document::save_file` / `save_file_compact` | β | β | | ||
| | `Document::save_writer` / `save_writer_compact` | β | β | | ||
| | `Document::to_string` / `to_string_compact` | β | β | | ||
| | DOM mutation, iterators, visitors | β | β | | ||
| | `XmlPrinter` | β | β | | ||
| | `XmlError::Io` variant | β | β | | ||
| | `impl std::error::Error for XmlError` | β | β | | ||
| | `impl From<std::io::Error> for XmlError` | β | β | | ||
|
|
||
| The six file-oriented methods (`load_file`, `load_file_mut`, `save_file`, | ||
| `save_file_compact`, `save_writer`, `save_writer_compact`) and the | ||
| `XmlError::Io` variant are compiled only when the `std` feature is active. Their | ||
| signatures reference `std::path::Path` and `std::io::Write`, which are | ||
| unavailable without the standard library. | ||
|
|
||
| ## Known Limitations | ||
|
|
||
| 1. **A global allocator is required.** The DOM stores node names, attribute | ||
| values, text content, and child lists in `String` and `Vec` from `alloc`. | ||
| `core`-only (no heap) targets are not supported. | ||
|
|
||
| 2. **No file I/O.** `Document::load_file` and `Document::save_file` need the | ||
| `std` feature. Use `Document::parse_bytes` / `to_string` and let the | ||
| application layer handle loading bytes from a filesystem, flash, or network. | ||
|
|
||
| 3. **No `std::io::Write` streaming.** `Document::save_writer` and | ||
| `save_writer_compact` are `std`-only. Use `to_string` / `to_string_compact` | ||
| or the `XmlPrinter` streaming builder instead. | ||
|
|
||
| 4. **No `std::error::Error` impl.** The `std::error::Error` trait | ||
| implementation for `XmlError` is absent without `std`. Use | ||
| `XmlError::code()` (returns `u32`) or match on variants directly for | ||
| error handling. | ||
|
|
||
| 5. **All Cargo examples require `std`.** The registered `[[example]]` targets | ||
| are executables and need `fn main()`. The code patterns inside | ||
| `examples/wasm_parse.rs` are portable and can be copied into a `no_std` | ||
| binary crate. | ||
|
|
||
| ## Verification | ||
|
|
||
| The project's CI validates these build variants (`wasm-build.yml`): | ||
|
|
||
| ```bash | ||
| # Native target, no_std (library only) | ||
| cargo build -p tinyxml2 --no-default-features --lib | ||
|
|
||
| # wasm32-unknown-unknown (browser), with and without std | ||
| cargo build -p tinyxml2 --target wasm32-unknown-unknown | ||
| cargo build -p tinyxml2 --no-default-features --target wasm32-unknown-unknown --lib | ||
|
|
||
| # wasm32-wasip1 (WASI hosts), with and without std | ||
| cargo build -p tinyxml2 --target wasm32-wasip1 | ||
| cargo build -p tinyxml2 --no-default-features --target wasm32-wasip1 --lib | ||
| ``` | ||
|
|
||
| To check against an embedded ARM target (requires `rustup target add`): | ||
|
|
||
| ```bash | ||
| rustup target add thumbv7em-none-eabihf | ||
| cargo check -p tinyxml2 --no-default-features --target thumbv7em-none-eabihf --lib | ||
| ``` | ||
|
|
||
| Replace `thumbv7em-none-eabihf` with your target triple (e.g. | ||
| `riscv32imac-unknown-none-elf`, `aarch64-unknown-none`). | ||
|
|
||
| > [!TIP] | ||
| > Use `cargo check --lib` for no\_std targets β it validates the crate | ||
| > compiles without needing a linker script, panic handler, or allocator in | ||
| > scope. Full `cargo build` requires a binary entry point from a consuming | ||
| > crate. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.