Skip to content

feature/state arc builder#497

Open
uemuradevexe wants to merge 3 commits intorapina-rs:mainfrom
uemuradevexe:feature/state-arc-builder
Open

feature/state arc builder#497
uemuradevexe wants to merge 3 commits intorapina-rs:mainfrom
uemuradevexe:feature/state-arc-builder

Conversation

@uemuradevexe
Copy link
Copy Markdown
Collaborator

@uemuradevexe uemuradevexe commented Apr 14, 2026

Summary

Changes:

  • AppState::with_arc<T: ?Sized>() — stores Arc directly in the state map under TypeId::of::<Arc>(), no newtype needed
  • Rapina::state_arc() — builder method exposing with_arc on the public app API
  • State<Arc> extractor works without modification — deref chain gives direct access to trait methods
  • State extractor error message updated to mention .state_arc()

Tests added:

  • state.rs: concrete type, trait object, coexistence with .with(), missing state, overwrite
  • extract/mod.rs: end-to-end extraction of Arc via FromRequestParts

Related Issues

Closes #495

Checklist

  • cargo fmt passes
  • cargo clippy has no warnings
  • Tests pass
  • Documentation updated (if needed)

@github-actions github-actions bot added the area: core Core framework label Apr 14, 2026
@uemuradevexe uemuradevexe self-assigned this Apr 14, 2026
@uemuradevexe uemuradevexe requested a review from ShiraiEd April 14, 2026 22:10
/// Internally the value is stored under `TypeId::of::<Arc<T>>()` wrapped in
/// one additional `Arc` (as required by the state map). Handlers receive
/// `State<Arc<dyn MyTrait>>` and can call methods directly via auto-deref,
/// or clone the inner arc with `(*state).clone()`.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be clearer written as Arc::clone(&*state) instead of (*state).clone() since that makes the intent explicit

Copy link
Copy Markdown
Collaborator

@ShiraiEd ShiraiEd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

The feature is still missing in the dependency injection docs, worth mentioning there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: core Core framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Suggestion]: Add state_arc() builder to support Arc<dyn Trait> without double-wrapping

2 participants