Repository navigation
refactor(core)!: pass composable &dyn Diagnostic and forward Arc and static refs - #251
Merged
Merged
Conversation
…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>
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>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Replace
&[Box<dyn Diagnostic>]in extension trait signatures with a composable&dyn Diagnostictrait object, relax the'staticbound onDiagnosticto theBox<dyn Diagnostic>registration boundary, and addArc<T>and reference forwarding across all core extension traits so pipeline components can be shared across dispatches and loggers as a DAG.&dyn Diagnosticboundary (logforth-core,logforth): Omit the'staticsupertrait bound onDiagnostic(enforcing'staticonFrom<T> for Box<dyn Diagnostic>for long-livedDispatchregistration), keepArc<T>and&Tforwarding indiagnostic/mod.rsalongside the other core traits, and mirrorlog::kv::Source's four containers indiagnostic/impls.rs((), plusOption,[..],[..; N], andVecfor bothT: DiagnosticandBox<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.logforth-append-async): Pass stack-resident&diagdirectly to inner appenders on the worker thread instead of allocatingBox<dyn Diagnostic>per log record.Arc<T>and&'static Ttrait forwarding (logforth-core,logforth-append-opentelemetry): ImplementAppend,Filter(forwarding bothenabledandmatches),Layout, andMakeBodyforArc<T>and&'static TwhereT: ?Sized, alongsideArc<T>and&TonDiagnostic.Breaking Changes
Filter::enabled,Filter::matches,Layout::format,Append::append, andMakeBody::createnow acceptdiags: &dyn Diagnosticinstead ofdiags: &[Box<dyn Diagnostic>].Diagnosticno longer implies'staticas a supertrait; generic code storing diagnostics insideBox<dyn Diagnostic>should boundT: Diagnostic + 'static.diags.visit(&mut visitor)?instead of iteratingfor d in diags { d.visit(&mut visitor)?; }, and callers passing an empty diagnostic context directly should pass&().