You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The way the state is passed around in #1155 leads to it being cloned quite a bit so its important that the state is cheap to clone. We would like to always store the state in an Arc so users don't have to worry about that.
This should be possible because we only give references to the state and because FromRef<T> for T requires T: Clone.
Additionally this means we can remove all the S: Clone bounds we have today.
Proposal
Always store an Arc<S> inside Router, MethodRouter, and whatever else might store the state.
Something like this for constructing Router:
structRouter<S>{state:Arc<S>,
...
}impl<S>Router<S>{fnwith_state(state:S) -> Self{Self::with_state_arc(Arc::new(state))}// this way users can avoid a double `Arc` if they already have an `Arc<S>`fnwith_state_arc(state:Arc<S>) -> Self{Self{
state,
...}}}implRouter<()>{fnnew() -> Self{// making an `Arc<()>` does allocate but this is a single allocation// made during setup, so probably fineSelf::with_state(Arc::new(()))}}
Alternatives
Do nothing but document that users should make sure their state is cheaply cloneable.
The text was updated successfully, but these errors were encountered:
Feature Request
Motivation
The way the state is passed around in #1155 leads to it being cloned quite a bit so its important that the state is cheap to clone. We would like to always store the state in an
Arc
so users don't have to worry about that.This should be possible because we only give references to the state and because
FromRef<T> for T
requiresT: Clone
.Additionally this means we can remove all the
S: Clone
bounds we have today.Proposal
Always store an
Arc<S>
insideRouter
,MethodRouter
, and whatever else might store the state.Something like this for constructing
Router
:Alternatives
Do nothing but document that users should make sure their state is cheaply cloneable.
The text was updated successfully, but these errors were encountered: