Skip to content

refactor(core)!: pass composable &dyn Diagnostic and forward Arc and static refs - #251

Merged
tisonkun merged 8 commits into
mainfrom
refactor/core-diagnostics-forwarding
Nov 26, 2026
Merged

tisonkun merged 8 commits into
mainfrom
refactor/core-diagnostics-forwarding

Conversation

@tisonkun

@tisonkun tisonkun commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Replace &[Box<dyn Diagnostic>] in extension trait signatures with a composable &dyn Diagnostic trait object, relax the 'static bound on Diagnostic to the Box<dyn Diagnostic> registration boundary, and add Arc<T> and reference forwarding across all core extension traits so pipeline components can be shared across dispatches and loggers as a DAG.

  • Composable &dyn Diagnostic boundary (logforth-core, logforth): Omit the 'static supertrait bound on Diagnostic (enforcing 'static on From<T> for Box<dyn Diagnostic> for long-lived Dispatch registration), keep Arc<T> and &T forwarding in diagnostic/mod.rs alongside the other core traits, and mirror log::kv::Source's four containers in diagnostic/impls.rs ((), plus Option, [..], [..; N], and Vec for both T: Diagnostic and Box<dyn Diagnostic>). Decorators and callers can pass stack-resident or chained borrowed contexts such as &[diags, &extra] in two registers (16B) without wrapper types or heap allocations.
  • Zero-allocation async worker diagnostic handoff (logforth-append-async): Pass stack-resident &diag directly to inner appenders on the worker thread instead of allocating Box<dyn Diagnostic> per log record.
  • Complete Arc<T> and &'static T trait forwarding (logforth-core, logforth-append-opentelemetry): Implement Append, Filter (forwarding both enabled and matches), Layout, and MakeBody for Arc<T> and &'static T where T: ?Sized, alongside Arc<T> and &T on Diagnostic.

Breaking Changes

  • Filter::enabled, Filter::matches, Layout::format, Append::append, and MakeBody::create now accept diags: &dyn Diagnostic instead of diags: &[Box<dyn Diagnostic>].
  • Diagnostic no longer implies 'static as a supertrait; generic code storing diagnostics inside Box<dyn Diagnostic> should bound T: Diagnostic + 'static.
  • Callers traversing diagnostics should call diags.visit(&mut visitor)? instead of iterating for d in diags { d.visit(&mut visitor)?; }, and callers passing an empty diagnostic context directly should pass &().

…c refs

Passing `&[Box<dyn Diagnostic>]` to `Filter`, `Layout`, and `Append` leaked the `Dispatch` container representation into every extension trait signature, forced `Async` workers to allocate a `Box<dyn Diagnostic>` per log record to forward materialized context, and made ad-hoc composition from stack-resident or borrowed trait-object references awkward. Separately, only `ErrorSink` forwarded `Arc<T>` and `&'static T`, preventing callers from sharing stateful `Append`, `Layout`, `Filter`, or `Diagnostic` components directly across multiple dispatches or loggers without manual wrapper types.

Introduce `Diagnostics<'a>` in `logforth-core` (re-exported by `logforth`) as a `Copy` view over empty, boxed-slice, single-item, and trait-object-slice diagnostic sources, and replace `&[Box<dyn Diagnostic>]` with `Diagnostics` across `Filter::{enabled, matches}`, `Layout::format`, `Append::append`, and `MakeBody::create`. Update `Async` worker dispatch to pass stack-resident `Diagnostics` without per-record heap boxing.

Implement `Append`, `Diagnostic`, `Filter` (explicitly forwarding both `enabled` and `matches`), and `Layout` in `logforth-core`, plus `MakeBody` in `logforth-append-opentelemetry`, for `Arc<T>` and `&'static T` where `T: ?Sized`, removing the now-redundant concrete `Diagnostic` implementation on `&'static StaticDiagnostic`.

Signed-off-by: tison <wander4096@gmail.com>
`Diagnostic` sources are dynamic context providers (such as thread-local, task-local, or active span contexts) rather than materialized key-value slices: a view with attached sources can still emit zero key-value pairs on `visit`, making a source-count `is_empty()` check misleading for callers while adding no fast-path benefit over `Diagnostics::visit`.

Remove `Diagnostics::is_empty`, represent `Diagnostics::empty()` directly as `DiagnosticsState::Boxed(&[])` to match `KeyValues::empty()`, and drop `DiagnosticsState::Empty` along with the redundant empty-slice normalization branches in `from_boxed` and `from_slice`.

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

Consumers of diagnostic context (`Filter`, `Layout`, `Append`, and `MakeBody`) only traverse key-value pairs via `visit`, meaning a collection or chain of diagnostic sources is itself a `Diagnostic`. Modeling producer storage shapes as a closed enum view couples constructors to specific containers and, while `Diagnostic` carries a `'static` supertrait bound, prevents decorators from chaining stack-borrowed per-call context onto incoming diagnostics without heap allocation.

Remove `Diagnostics`, drop the `'static` supertrait bound from `Diagnostic` while enforcing `'static` on `From<T> for Box<dyn Diagnostic>`, and pass `&dyn Diagnostic` directly across `Filter`, `Layout`, `Append`, and `MakeBody`. Implement `Diagnostic` for `&T`, `Arc<T>`, `()`, `Option<T>`, `(A, B)`, arrays, slices, boxed slices, and `Vec` (including `[Box<dyn Diagnostic>]`, `Box<[Box<dyn Diagnostic>]>`, and `Vec<Box<dyn Diagnostic>>`), and update `DESIGN.md`, `CHANGELOG.md`, and integration tests accordingly.

Signed-off-by: tison <wander4096@gmail.com>
@tisonkun tisonkun changed the title refactor(core)!: introduce Diagnostics view and forward Arc and static refs refactor(core)!: pass composable &dyn Diagnostic and forward Arc and static refs Nov 25, 2026
Keep `Diagnostic`'s rustdoc focused strictly on caller-facing contracts (`visit` multiplicity, side-effect freedom, and error short-circuiting), moving internal boundary rationale to code comments in a dedicated `core/src/diagnostic/impls.rs` module.

Standardize `Diagnostic` forwarding and composition in `impls.rs` across three orthogonal categories:
1. Reference and shared-ownership forwarding (`&T` and `Arc<T>`, plus `From<T> for Box<dyn Diagnostic>`).
2. Homogeneous sequence composition (`[T]`, `[T; N]`, and `Vec<T>`, alongside boxed trait-object equivalents).
3. Conditional and heterogeneous product composition (`Option<T>`, `()`, and 1..=12 tuples via `macro_rules!`).

Signed-off-by: tison <wander4096@gmail.com>
Rely directly on `impl Diagnostic for Vec<Box<dyn Diagnostic>>` in `Dispatch::enabled` and `Dispatch::log` instead of eagerly branching on slice length before filters run.

Signed-off-by: tison <wander4096@gmail.com>
Move `From<T> for Box<dyn Diagnostic>`, `Arc<T>`, and `&T` forwarding back into `core/src/diagnostic/mod.rs` to match the layout of `append`, `filter`, `layout`, and `error::sink`.

Follow `log::kv::Source`'s container pattern in `core/src/diagnostic/impls.rs` (`Option<T>`, `[T]`, `[T; N]`, and `Vec<T>`) plus the concrete empty `()` and boxed slice/`Vec` forms used in the workspace, dropping the speculative tuple macro and `[Box<dyn Diagnostic>; N]` impl.

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

Complete the 1:1 mirror between `T: Diagnostic` and `Box<dyn Diagnostic>` container implementations in `core/src/diagnostic/impls.rs` by adding `Option<Box<dyn Diagnostic>>` and `[Box<dyn Diagnostic>; N]` alongside `[Box<dyn Diagnostic>]` and `Vec<Box<dyn Diagnostic>>`.

Signed-off-by: tison <wander4096@gmail.com>
@tisonkun
tisonkun merged commit 848cd9d into main Nov 26, 2026
10 checks passed
@tisonkun
tisonkun deleted the refactor/core-diagnostics-forwarding branch November 26, 2026 03:40
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