axum trait based factory #18
washanhanzi
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Plan: Adopt Axum's Trait-Based Factory Pattern
Problem
Current approach uses closures for factories:
Issue:
Cloneis not object-safe, so we can't clone the factory withoutArcor other workarounds.Goal
Adopt Axum's pattern using a custom trait with
clone_box()method to enable efficient cloning of type-erased factories.Current vs Proposed
Current (Closure)
Proposed (Trait)
Implementation Plan
Step 1: Define Core Traits
Step 2: Define Concrete Factory Struct
Step 3: Implement Trait for Concrete Struct
Step 4: Define Wrapper Type
Step 5: Update Generated Code
Before:
After:
Step 6: Update Builder Methods
Before:
After:
Streaming Variant
Same pattern for streaming:
File Changes Summary
connectrpc-axum/src/handler/tonic.rsconnectrpc-build/src/prost/connect.rsBoxedIntoCallconnectrpc-build/src/prost/tonic_compat.rsBenefits
HelloWorldServiceTonicCompatibleBuildercan deriveCloneclone_box()is efficientDrawbacks
Alternative: Keep Closure, Add Arc
If cloning isn't a hard requirement, simpler alternative:
Pros: Minimal changes
Cons: Arc overhead, not idiomatic with Axum
Recommendation
Adopt the trait-based approach if:
Keep closure approach if:
Questions to Resolve
clone_box()vsArc::clone().All reactions