Commit 5748ea2
feat!: stack-based format buffer for
Adds and integrates `wdk::fmt::FormatBuffer`.
## Summary
- Introduced `crates/wdk/src/fmt.rs` with a public `wdk::fmt` module
containing two types:
- `FormatBuffer<N>` — a fixed-size, stack-allocated formatting buffer
implementing `fmt::Write`
- `FlushableFormatBuffer<F, N>` — a wrapper that auto-flushes via
closure on overflow and on drop
- Swapped `print.rs` WDM/KMDF path to use `FlushableFormatBuffer`
instead of `DbgPrintBufWriter`; removed that module and its tests.
- Turns the `alloc` feature from the `wdk` crate into a no-op — it had
no remaining uses after the stack-buffer migration. Avoids removal of
feature to prevent a breaking change
- Bumps MSRV from 1.85 to 1.91 due to use of
[`str::char_floor_boundary`](https://doc.rust-lang.org/core/primitive.str.html#method.floor_char_boundary)
## Details
- `FormatBuffer<N>` stores a zero-initialized `[u8; N]` and tracks
`used` bytes. The last byte is reserved for a NUL terminator, so usable
capacity is `N - 1`. `N` must be at least 2; smaller values will not
compile (`const { assert!(N >= 2) }`).
- All buffer mutations go through a single `append_bytes` helper that
copies data and sets `buffer[used] = 0`, centralizing the NUL terminator
invariant.
- `as_str()` returns an infallible UTF-8 view — only valid UTF-8 enters
via `write_str` (both `FormatBuffer::write_str` and
`FlushableFormatBuffer::write_str` copy from `&str` sources).
- `as_c_str()` returns a `&CStr` view up to the first NUL. Infallible in
practice since the NUL invariant is always maintained.
- `fmt::Write` clamps overflow writes at a UTF-8 char boundary via
`floor_char_boundary` and signals truncation via `fmt::Error`.
- `FlushableFormatBuffer<F, N>` wraps `FormatBuffer` and auto-flushes
via a closure when the buffer fills. Remaining content is flushed on
drop. This allows arbitrarily long formatted output to be processed in
fixed-size chunks.
- `print.rs` WDM/KMDF path now formats into `FlushableFormatBuffer<_,
512>` and calls `DbgPrint` via `%s` using the resulting `CStr`.
- Removed the `alloc` feature gate from the WDM/KMDF print path —
`print!`/`println!` no longer require heap allocation and work at any
IRQL where `DbgPrint` is valid.
- Custom `Debug` impl on `FormatBuffer` shows only the used content, not
stale bytes after `clear()`.
## Testing
29 tests covering:
- basic write, `as_str()`, `as_c_str()` usage
- overflow, multi-write, and exact-fit scenarios
- empty writes and min-sized buffers (`N = 2`)
- clear-then-shorter-write regression (NUL terminator correctness)
- multi-byte UTF-8 char boundary handling in both buffer types
- explicit `flush()` mid-stream and flush-on-drop
- interior NUL truncation via `as_c_str()`
- `compile_fail` doctest for `N < 2`
```pwsh
cargo test --package wdk --lib --all-features -- fmt --nocapture
```
## Notes / Follow-ups
- `_print` ignores `fmt::write` errors — partial output is acceptable
for debug printing. Errors from individual `Display` impls are silently
dropped.
- The old `DbgPrintBufWriter` stripped interior NUL bytes and continued
printing. The new WDM/KMDF path truncates each chunk at the first NUL
(via `as_c_str`). UMDF still strips NULs and prints the remainder.
---------
Signed-off-by: Leon Durrenberger <leon.durrenberger@gmail.com>
Co-authored-by: leon-xd <leondu@microsoft.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>wdk crate (#611)1 parent 2940ef8 commit 5748ea2
6 files changed
Lines changed: 713 additions & 437 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
90 | | - | |
| 90 | + | |
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | | - | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| |||
0 commit comments