Skip to content

feat(core): unify serde field capture into lazy ValueSerde - #329

Merged
tisonkun merged 11 commits into
mainfrom
feat/lazy-value-serde
Dec 2, 2026
Merged

tisonkun merged 11 commits into
mainfrom
feat/lazy-value-serde

Conversation

@tisonkun

@tisonkun tisonkun commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Unify structured serde field capture in logforth-core (behind the serde feature) into kv::ValueSerde<'a, T> and expose Value::lazy_owned for lazily initialized ValueOwned fields, replacing ValueOwned::{from_serde, try_from_serde} with a single adapter across borrowed logging and owned snapshots.

  • Add Value::lazy_owned(&'a (dyn Deref<Target = ValueOwned> + Sync)) on Value<'a> so lazy ValueOwned containers (such as std::sync::LazyLock<ValueOwned, F> and ValueSerde) stay unevaluated until a dispatch inspects or clones the field.
  • Add kv::ValueSerde<'a, T: serde::Serialize + ?Sized> backed by OnceLock<ValueOwned> with const fn new(&'a T), into_owned(self) -> ValueOwned, try_into_owned(self) -> Result<ValueOwned, Error>, Deref<Target = ValueOwned>, From<ValueSerde<'_, T>> for ValueOwned, and LogValue (where T: Sync).
  • Defer serialization on borrowed logging paths until inspection (memoizing the snapshot across dispatches and repeated dereferences), while moving out any cached snapshot without cloning or serializing directly once in into_owned.
  • Keep Deref, into_owned, and try_into_owned available for !Sync Serialize types (such as DebugValue, DisplayValue, and log::kv::Value), and migrate logforth-bridge-log to ValueSerde::new(&value).into_owned().

Add `Value::serde`, `SerdeValue`, `ValueView::Serde`, and `ValueView::to_serde` in `logforth-core` so callers can borrow `serde::Serialize` values on the stack without allocation, skip serialization when a dispatch `Filter` rejects a record, stream directly into `serde::Serializer` outputs such as `JsonLayout` without an intermediate `ValueOwned` tree, and expand structured values across `OpentelemetryLog`, `LogfmtLayout`, `Journald`, and `Record::to_owned()`.

Implement `LogValue` and `From<...> for ValueOwned` for `Path`, `PathBuf` (plus `Box<Path>` and `Cow<'_, Path>` on `ValueOwned`), and `std::net` IP and socket address types (`IpAddr`, `Ipv4Addr`, `Ipv6Addr`, `SocketAddr`, `SocketAddrV4`, and `SocketAddrV6`), keeping `Value<'a>` at 24 bytes (`Copy + Send + Sync`) on 64-bit targets.

Signed-off-by: tison <wander4096@gmail.com>
Defer standard-library `Path`, `PathBuf`, and `std::net` `LogValue` and `ValueOwned` implementations to a separate follow-up so this change focuses exclusively on lazy borrowed `Value::serde` (`SerdeValue`, `ValueView::Serde`, and `ValueView::to_serde`).

Signed-off-by: tison <wander4096@gmail.com>
@tisonkun tisonkun changed the title feat(core): add lazy Value::serde and LogValue for Path and std::net feat(core): add lazy borrowed Value::serde Dec 2, 2026
Drop the optional `erased-serde` dependency from `logforth-core` and serialize `SerdeValue` through `SerdeValue::try_to_owned`, keeping `logforth-core`'s `serde` feature backed solely by `serde` and sharing identical `ValueSerializer` semantics across synchronous and asynchronous outputs while preserving zero-allocation capture and filter short-circuiting.

Signed-off-by: tison <wander4096@gmail.com>
Replace `Value::serde`, `SerdeValue`, and `ValueView::Serde` with `kv::serde` (`kv::Serde<'a, T>` backed by `&'a T` and `OnceLock<ValueOwned>`), keeping `ValueView` and all downstream layouts and appenders untouched while skipping serialization on rejected records and memoizing materialization across multiple dispatches.

Signed-off-by: tison <wander4096@gmail.com>
@tisonkun tisonkun changed the title feat(core): add lazy borrowed Value::serde feat(core): add lazy stack-memoized kv::serde Dec 2, 2026
Replace the private `LazyOwned` trait and `kv::Serde` wrapper struct with standard-library `std::ops::Deref<Target = ValueOwned>` inside `ValueState` and `impl LogValue for std::sync::LazyLock<ValueOwned, F>`, reducing `kv::serde` to a `LazyLock::new` adapter returning `impl LogValue` with no custom structs, traits, or extra `Value` constructors.

Signed-off-by: tison <wander4096@gmail.com>
Signed-off-by: tison <wander4096@gmail.com>
Replace unstable `OnceLock::get_or_try_init` on `ValueSerde` with stable `OnceLock::{get, get_or_init}` (`serialize` / `try_serialize`), implement `Deref<Target = ValueOwned>` and lazy `LogValue` via `ValueState::LazyOwned(&'a (dyn Deref<Target = ValueOwned> + Sync))`, relax `+ Sync` to the `LogValue` impl so `!Sync` serializable types work with `to_owned` and `try_to_owned`, and align `logforth-bridge-log`, docs, changelog, and integration tests.

Signed-off-by: tison <wander4096@gmail.com>
@tisonkun tisonkun changed the title feat(core): add lazy stack-memoized kv::serde feat(core): unify serde field capture into lazy ValueSerde Dec 2, 2026
Signed-off-by: tison <wander4096@gmail.com>
…Serde

Expose `Value::lazy_owned` on `Value` for `&'a (dyn Deref<Target = ValueOwned> + Sync)` cells, unify `&self` caching across `ValueSerde` (`Deref`, `LogValue`, `to_owned`, and `try_to_owned`), add consuming `into_owned` and `try_into_owned` conversions (plus `From<ValueSerde<'_, T>>` for `ValueOwned`), remove `Clone` on `ValueSerde`, and align crate documentation, changelog, and integration tests.

Signed-off-by: tison <wander4096@gmail.com>
…erde

Back `ValueSerde` directly with `OnceLock<ValueOwned>` and remove `to_owned` and `try_to_owned`, leaving `new`, `into_owned`, and `try_into_owned` alongside `Deref<Target = ValueOwned>`, `From<ValueSerde<'_, T>>` for `ValueOwned`, and `LogValue`.

Signed-off-by: tison <wander4096@gmail.com>
@tisonkun
tisonkun merged commit ac7e38d into main Dec 2, 2026
@tisonkun
tisonkun deleted the feat/lazy-value-serde branch December 2, 2026 17:31
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