Skip to content

feat(core): add kv::lazy and kv::serde field helpers - #344

Open
tisonkun wants to merge 1 commit into
mainfrom
feat/kv-lazy
Open

tisonkun wants to merge 1 commit into
mainfrom
feat/kv-lazy

Conversation

@tisonkun

@tisonkun tisonkun commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add kv::lazy and kv::ValueLazy in logforth-core (re-exported at logforth::kv) to defer computing expensive structured fields or formatted payload arguments until first inspection and share the cached ValueOwned snapshot across dispatches.
  • Add kv::serde (behind the serde feature) in logforth-core as a concise constructor returning kv::ValueSerde<'_, T>.
  • Add integration tests in tests-integration covering level and dispatch-filter pruning, multi-dispatch memoization across structured fields and formatted payloads, non-Send single-thread initializers, and uninitialized Debug formatting, and update docs and CHANGELOG.md.

Design Notes

  • Named carrier types (ValueLazy<F>, ValueSerde<'a, T>) paired with concise kv::* constructors: Because Value<'a> is a Copy stack-borrowed carrier, lazy memoizing fields require a caller-stack owner for their OnceLock<ValueOwned> cache (Value::lazy_owned). Returning named carrier types rather than opaque impl LogValue keeps kv::lazy and kv::serde const fn, preserves clean rustdoc signatures, and supports conditional trait bounds (Deref<Target = ValueOwned> and Display for any FnOnce() -> T including !Send closures, with LogValue gated on F: Send, mirroring ValueSerde's T: Sync bound).
  • Safe single-parameter ValueLazy<F>: Using Mutex<Option<F>> alongside OnceLock<ValueOwned> avoids unsafe (unsafe_code = "deny"), performs zero heap allocations or syscalls when a record is rejected by level masks or downstream Filters, and lets F return any T: Into<ValueOwned> directly without unnameable adapter closures.

Add `kv::lazy` (`kv::ValueLazy`) and `kv::serde` (behind the `serde` feature) in `logforth-core::kv` and re-export them through `logforth::kv` so callers can capture lazily computed or serializable fields without spelling out carrier struct constructors or nested `LazyLock` wrappers.

`ValueLazy<F>` defers invoking its initializer until first dereference or `Display` formatting and caches the resulting `ValueOwned` snapshot across all dispatches and `Record::to_owned` promotions.

Signed-off-by: tison <wander4096@gmail.com>
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.

1 participant