feat: add wrap_with_*_context and deserialize_to_snapshot APIs (v0.8.1)#26
Open
Yaming-Hub wants to merge 5 commits into
Open
feat: add wrap_with_*_context and deserialize_to_snapshot APIs (v0.8.1)#26Yaming-Hub wants to merge 5 commits into
Yaming-Hub wants to merge 5 commits into
Conversation
Add two wrap helpers that capture context without spawning: - wrap_with_sync_context(mode, fut) — sync → async context wrapping - wrap_with_async_context(mode, fut) — async → async context wrapping These decouple capture from spawn, enabling enqueue-then-spawn, retry, rate-limiting, and priority queue patterns. Add deserialize_to_snapshot(bytes) that converts wire bytes directly to a ContextSnapshot without mutating any live context store. Useful for sync actors, message routers, and context inspection. Bump version: 0.8.0 → 0.8.1 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Adds a clear semantic API to check if a task-local context store is available, replacing the pattern async_ctx::current_depth().is_some(). Updated SyncDcontextLayer to use the new API. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Usage guide §5: rewrite with ContextInheritance modes, spawn helpers, wrap helpers, message passing guidance, deserialize_to_snapshot - Design doc §4.4-4.7: update root-level helpers, add Fork vs Snapshot guidance, wrap helper patterns, direct wire deserialization - Design doc §11.2-11.6: update examples to use new API names, add cross-thread message passing pattern, use deserialize_to_snapshot in HTTP middleware example Key guidance: use Fork for parent-child task relationships, Snapshot for cross-thread message passing where sender's context may be gone. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Adds attach_named() that accepts a scope name parameter, making the attached scope visible in scope_chain() for debugging and tracing. The existing attach() remains unchanged (unnamed scope). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The layer no longer pushes/pops dcontext scopes per span enter/exit. Scope lifecycle is now entirely the caller's responsibility. This avoids issues with span/scope lifetime mismatches in async code and eliminates unnecessary scope overhead. The layer retains its core value as a span<->context data bridge: - Field extraction (span fields -> context values) - Span info (span metadata -> context value) - Span recording (context values -> span fields) - Log enrichment (context values -> log events) Removed guard_stack.rs (only used for scope guards). Removed 13 scoping tests, added 2 no-scoping behavior tests. Updated README, lib.rs docs, design doc. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.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
Adds two new API categories that close gaps in the context inheritance model:
1. Wrap helpers — capture context without spawning
wrap_with_sync_context(mode, fut)wrap_with_async_context(mode, fut)These decouple context capture from task spawning. The existing
spawn_with_*helpers always calltokio::spawnimmediately, but many patterns need to capture context now and spawn later:Both support
Fork(cheap Arc sharing) andSnapshot(full copy) modes.2.
deserialize_to_snapshot— wire bytes → ContextSnapshot directlyConverts wire bytes directly into a
ContextSnapshotwithout mutating any live context store. Previously, the only way was a three-step dance:Benefits:
Changes
dcontext/src/inheritance.rs— addedwrap_with_sync_context,wrap_with_async_contextdcontext/src/wire.rs— addeddeserialize_to_snapshotdcontext/src/lib.rs— re-exports for new APIsdcontext/src/tests.rs— 9 new tests (5 wrap, 4 deserialize)dcontext/Cargo.toml— version bump 0.8.0 → 0.8.1Test Results
All 88 tests pass, zero clippy warnings.