1mod owned;
2
3use crate::{
4 ConcreteStateKind, DiscriminatedState, Initial, StateConcreteProvenState,
5 StateConcreteTransitionProof, StateKind, StateMachineImpl, StateUnionDiscriminant,
6 StateUnionDiscriminatedTransition, StateUnionErased, StateUnionProofTarget,
7 StateUnionProvenState, StateUnionSharedEffect, StateUnionSharedTransitionEffect,
8 StateUnionTransitionProof, StateWithProof, Transition, UnionStateKind, state_trait,
9};
10use core::marker::PhantomData;
11use core::ops::{Deref, DerefMut};
12#[cfg(feature = "tracing")]
13use core::panic::Location;
14use core::pin::Pin;
15#[cfg(feature = "unique-rc-arc")]
16use std::rc::UniqueRc;
17#[cfg(feature = "unique-rc-arc")]
18use std::sync::UniqueArc;
19
20pub use owned::{SOwned, StorageStateOwned, StorageStateOwnedBox, StorageStateOwnedPinBox};
21#[cfg(feature = "unique-rc-arc")]
22pub use owned::{StorageStateOwnedUniqueArc, StorageStateOwnedUniqueRc};
23
24pub type SBox<T, S> = State<StorageStateOwnedBox, T, S>;
35pub type SPinBox<T, S> = State<StorageStateOwnedPinBox, T, S>;
57
58fn retag_owned<T, From, To>(inner: crate::StateOwned<T, From>) -> crate::StateOwned<T, To> {
59 crate::StateOwned {
60 value: inner.value,
61 state: PhantomData,
62 #[cfg(feature = "tracing")]
63 trace: inner.trace,
64 }
65}
66
67type StateMarker<Storage, T, S> = PhantomData<fn() -> (Storage, T, S)>;
68type TransitionMarker<Storage, T, From, To> = PhantomData<fn() -> (Storage, T, From, To)>;
69
70#[doc(hidden)]
72pub trait TransitionEffectSelector<From, To>: StateMachineImpl {
73 type Effect;
74}
75
76#[doc(hidden)]
78pub trait PinnedTransitionEffectSelector<From, To>: StateMachineImpl {
79 type Effect;
80}
81
82#[doc(hidden)]
84pub trait TransitionEffect<T, From, To, Args>
85where
86 T: StateMachineImpl,
87{
88 fn apply(value: &mut T, args: Args);
89}
90
91#[doc(hidden)]
99pub trait PinnedTransitionEffect<T, From, To, Args>
100where
101 T: StateMachineImpl,
102{
103 fn apply(value: Pin<&mut T>, args: Args);
104}
105
106#[doc(hidden)]
108pub trait InferenceKind {
109 type Inference: StateInference;
110}
111
112#[doc(hidden)]
114pub trait StateInference {
115 fn new<Storage, T, S>(inner: &Storage::Inner<T, S>) -> Self
116 where
117 Storage: StateStorage,
118 T: StateMachineImpl,
119 S: crate::ConcreteStateTrait;
120
121 fn from_erased(state: state_trait::ErasedState) -> Self;
122
123 fn state<Storage, T, S>(&self, inner: &Storage::Inner<T, S>) -> state_trait::ErasedState
124 where
125 Storage: StateStorage,
126 T: StateMachineImpl,
127 S: crate::StateTrait;
128}
129
130#[doc(hidden)]
132pub struct OuterInference;
133
134#[doc(hidden)]
136pub struct InnerInference;
137
138#[doc(hidden)]
140#[derive(Clone, Copy)]
141pub struct InnerStateInference;
142
143impl InferenceKind for OuterInference {
144 type Inference = state_trait::ErasedState;
145}
146
147impl InferenceKind for InnerInference {
148 type Inference = InnerStateInference;
149}
150
151impl StateInference for state_trait::ErasedState {
152 fn new<Storage, T, S>(_inner: &Storage::Inner<T, S>) -> Self
153 where
154 Storage: StateStorage,
155 T: StateMachineImpl,
156 S: crate::ConcreteStateTrait,
157 {
158 state_trait::erased_state::<S>()
159 }
160
161 fn from_erased(state: state_trait::ErasedState) -> Self {
162 state
163 }
164
165 fn state<Storage, T, S>(&self, _inner: &Storage::Inner<T, S>) -> state_trait::ErasedState
166 where
167 Storage: StateStorage,
168 T: StateMachineImpl,
169 S: crate::StateTrait,
170 {
171 state_trait::clone_erased(self)
172 }
173}
174
175impl StateInference for InnerStateInference {
176 fn new<Storage, T, S>(_inner: &Storage::Inner<T, S>) -> Self
177 where
178 Storage: StateStorage,
179 T: StateMachineImpl,
180 S: crate::ConcreteStateTrait,
181 {
182 Self
183 }
184
185 fn from_erased(_state: state_trait::ErasedState) -> Self {
186 Self
187 }
188
189 fn state<Storage, T, S>(&self, inner: &Storage::Inner<T, S>) -> state_trait::ErasedState
190 where
191 Storage: StateStorage,
192 T: StateMachineImpl,
193 S: crate::StateTrait,
194 {
195 Storage::inferred_state(inner)
196 }
197}
198
199pub trait StateStorage: Sized {
240 type Inference: InferenceKind = OuterInference;
242
243 type Inner<T, S>
245 where
246 T: StateMachineImpl;
247
248 type Machine<T>: StateMachineImpl<Standin = T::Standin, Impl = T::Impl, TransitionToken = T::TransitionToken>
250 where
251 T: StateMachineImpl;
252
253 #[doc(hidden)]
254 fn retag<T, From, To>(inner: Self::Inner<T, From>) -> Self::Inner<T, To>
255 where
256 T: StateMachineImpl;
257
258 #[doc(hidden)]
259 fn inferred_state<T, State>(inner: &Self::Inner<T, State>) -> state_trait::ErasedState
260 where
261 T: StateMachineImpl,
262 State: crate::StateTrait,
263 {
264 let _ = inner;
265 state_trait::static_erased_state::<State>()
266 }
267}
268
269pub trait MayTransition: StateStorage {
281 fn complete_transition<T, From, To, Args>(
287 state: State<Self, T, From>,
288 args: Args,
289 callsite: TransitionCallsite,
290 ) -> State<Self, T, To>
291 where
292 T: StateMachineImpl,
293 From: crate::StateTrait,
294 To: crate::ConcreteStateTrait,
295 T::Standin: Transition<From, To>,
296 <T::Standin as Transition<From, To>>::F: crate::TransitionSignature<Args>;
297
298 #[doc(hidden)]
299 fn complete_transition_after_effect<T, From, To>(
300 state: State<Self, T, From>,
301 callsite: TransitionCallsite,
302 ) -> State<Self, T, To>
303 where
304 T: StateMachineImpl,
305 From: crate::StateTrait,
306 To: crate::ConcreteStateTrait;
307}
308
309#[doc(hidden)]
310pub fn complete_transition_after_effect<Storage, T, From, To>(
311 state: State<Storage, T, From>,
312 callsite: TransitionCallsite,
313) -> State<Storage, T, To>
314where
315 Storage: MayTransition,
316 T: StateMachineImpl,
317 From: crate::StateTrait,
318 To: crate::ConcreteStateTrait,
319{
320 Storage::complete_transition_after_effect(state, callsite)
321}
322
323pub trait StateStorageNew: StateStorage {
328 fn new<T, State>(value: T) -> Self::Inner<T, State>
330 where
331 T: StateMachineImpl,
332 <Self::Machine<T> as StateMachineImpl>::Standin: Initial<State>;
333}
334
335pub trait SRef: StateStorage {
341 fn s_ref<T, State>(inner: &Self::Inner<T, State>) -> &T
343 where
344 T: StateMachineImpl;
345}
346
347pub trait SMut: SRef + MayTransition {
354 fn s_mut<T, State>(inner: &mut Self::Inner<T, State>) -> &mut T
356 where
357 T: StateMachineImpl;
358}
359
360pub trait SPinRef: SRef {
380 fn s_pin_ref<T, State>(inner: &Self::Inner<T, State>) -> Pin<&T>
382 where
383 T: StateMachineImpl;
384}
385
386pub trait SPinMut: SPinRef + MayTransition {
392 fn s_pin_mut<T, State>(inner: &mut Self::Inner<T, State>) -> Pin<&mut T>
394 where
395 T: StateMachineImpl;
396}
397
398pub trait SMove: MayTransition {}
404
405pub trait SMapRuntime<FromRuntime, ToRuntime>: SMove
414where
415 FromRuntime: StateMachineImpl,
416 ToRuntime: StateMachineImpl,
417{
418 fn map_runtime<StateMarkerType, F>(
426 state: State<Self, FromRuntime, StateMarkerType>,
427 f: F,
428 ) -> State<Self, ToRuntime, StateMarkerType>
429 where
430 F: FnOnce(FromRuntime) -> ToRuntime;
431}
432
433pub struct State<Storage, T, S>
467where
468 T: StateMachineImpl,
469 Storage: StateStorage,
470{
471 pub(crate) inner: Storage::Inner<T, S>,
472 pub(crate) marker: StateMarker<Storage, T, S>,
473}
474
475pub struct ConcreteStated<T, S>
489where
490 S: crate::StateMarker<Kind = ConcreteStateKind>,
491{
492 value: T,
493 state: PhantomData<fn() -> S>,
494}
495
496impl<T, S> ConcreteStated<T, S>
497where
498 S: crate::StateMarker<Kind = ConcreteStateKind>,
499{
500 #[allow(unsafe_code)]
507 #[must_use]
508 pub unsafe fn new(value: T) -> Self {
509 Self {
510 value,
511 state: PhantomData,
512 }
513 }
514
515 #[must_use]
517 pub fn into_raw(self) -> T {
518 self.value
519 }
520}
521
522#[doc(hidden)]
523#[must_use]
524pub fn concrete_stated_new<T, S>(value: T, _token: T::TransitionToken) -> ConcreteStated<T, S>
525where
526 T: StateMachineImpl,
527 S: crate::ConcreteStateTrait,
528{
529 ConcreteStated {
530 value,
531 state: PhantomData,
532 }
533}
534
535#[allow(type_alias_bounds)]
555pub type SResult<Storage, T, OkState, ErrState>
556where
557 Storage: StateStorage,
558 T: StateMachineImpl,
559= Result<State<Storage, T, OkState>, State<Storage, T, ErrState>>;
560
561#[doc(hidden)]
563pub struct StateTransitionCall<Storage, T, From, To>
564where
565 T: StateMachineImpl,
566 Storage: StateStorage,
567{
568 state: State<Storage, T, From>,
569 #[cfg(feature = "tracing")]
570 callsite: &'static Location<'static>,
571 marker: TransitionMarker<Storage, T, From, To>,
572}
573
574#[doc(hidden)]
576pub struct EffectTransitionCall<Storage, T, From, To, Effect>
577where
578 T: StateMachineImpl,
579 Storage: StateStorage,
580{
581 state: State<Storage, T, From>,
582 callsite: TransitionCallsite,
583 marker: PhantomData<fn() -> (To, Effect)>,
584}
585
586#[doc(hidden)]
588pub struct PinnedEffectTransitionCall<Storage, T, From, To, Effect>
589where
590 T: StateMachineImpl,
591 Storage: StateStorage,
592{
593 state: State<Storage, T, From>,
594 callsite: TransitionCallsite,
595 marker: PhantomData<fn() -> (To, Effect)>,
596}
597
598#[doc(hidden)]
600pub struct ConcreteProofTransitionCall<Storage, T, From, To>
601where
602 T: StateMachineImpl,
603 Storage: StateStorage,
604{
605 state: State<Storage, T, From>,
606 callsite: TransitionCallsite,
607 marker: PhantomData<fn() -> To>,
608}
609
610#[doc(hidden)]
612pub struct KindProofTransitionCall<Storage, T, From, Marker, To, Kind>
613where
614 T: StateMachineImpl,
615 Storage: StateStorage,
616 From: crate::StateTrait,
617 Marker: crate::StateMarker,
618 To: crate::ConcreteStateTrait,
619 Kind: StateKind,
620{
621 state: State<Storage, T, From>,
622 callsite: TransitionCallsite,
623 marker: PhantomData<fn() -> (Marker, To, Kind)>,
624}
625
626#[doc(hidden)]
628pub struct StateUnionProofTransitionCall<Storage, T, From, Marker, To>
629where
630 T: StateMachineImpl,
631 Storage: StateStorage,
632 From: crate::StateTrait,
633 Marker: StateUnionDiscriminant,
634 To: crate::ConcreteStateTrait,
635{
636 state: State<Storage, T, From>,
637 callsite: TransitionCallsite,
638 marker: PhantomData<fn() -> (Marker, To)>,
639}
640
641#[doc(hidden)]
643pub struct PinnedStateUnionProofTransitionCall<Storage, T, From, Marker, To>
644where
645 T: StateMachineImpl,
646 Storage: StateStorage,
647 From: crate::StateTrait,
648 Marker: StateUnionDiscriminant,
649 To: crate::ConcreteStateTrait,
650{
651 state: State<Storage, T, From>,
652 callsite: TransitionCallsite,
653 marker: PhantomData<fn() -> (Marker, To)>,
654}
655
656#[doc(hidden)]
658pub struct DiscriminatedTransitionCall<Storage, T, Marker, To>
659where
660 T: StateMachineImpl,
661 Storage: StateStorage,
662 Marker: StateUnionDiscriminant,
663{
664 state: DiscriminatedState<Storage, T, Marker>,
665 callsite: TransitionCallsite,
666 marker: PhantomData<fn() -> To>,
667}
668
669#[doc(hidden)]
671pub struct PinnedDiscriminatedTransitionCall<Storage, T, Marker, To>
672where
673 T: StateMachineImpl,
674 Storage: StateStorage,
675 Marker: StateUnionDiscriminant,
676{
677 state: DiscriminatedState<Storage, T, Marker>,
678 callsite: TransitionCallsite,
679 marker: PhantomData<fn() -> To>,
680}
681
682#[cfg(feature = "tracing")]
683#[doc(hidden)]
684pub type TransitionCallsite = &'static Location<'static>;
685
686#[cfg(not(feature = "tracing"))]
687#[doc(hidden)]
688pub type TransitionCallsite = ();
689
690#[doc(hidden)]
691#[track_caller]
692pub fn transition_callsite() -> TransitionCallsite {
693 #[cfg(feature = "tracing")]
694 {
695 Location::caller()
696 }
697 #[cfg(not(feature = "tracing"))]
698 {}
699}
700
701impl<Storage, T, From, To> StateTransitionCall<Storage, T, From, To>
702where
703 T: StateMachineImpl,
704 Storage: StateStorage,
705{
706 #[doc(hidden)]
707 pub fn call<Args>(self, args: Args) -> State<Storage, T, To>
708 where
709 T::Standin: Transition<From, To>,
710 <T::Standin as Transition<From, To>>::F: crate::TransitionSignature<Args>,
711 Storage: MayTransition,
712 From: crate::StateTrait,
713 To: crate::ConcreteStateTrait,
714 {
715 Storage::complete_transition(self.state, args, {
716 #[cfg(feature = "tracing")]
717 {
718 self.callsite
719 }
720 #[cfg(not(feature = "tracing"))]
721 {}
722 })
723 }
724}
725
726impl<Storage, T, From, To, Effect> EffectTransitionCall<Storage, T, From, To, Effect>
727where
728 T: StateMachineImpl,
729 Storage: StateStorage,
730{
731 pub fn call<Args>(mut self, args: Args) -> State<Storage, T, To>
732 where
733 Storage: SMut,
734 T::Standin: Transition<From, To>,
735 <T::Standin as Transition<From, To>>::F: crate::TransitionSignature<Args>,
736 From: crate::StateTrait,
737 To: crate::ConcreteStateTrait,
738 Effect: TransitionEffect<T, From, To, Args>,
739 {
740 Effect::apply(Storage::s_mut(&mut self.state.inner), args);
741 Storage::complete_transition_after_effect(self.state, self.callsite)
742 }
743}
744
745impl<Storage, T, From, To, Effect> PinnedEffectTransitionCall<Storage, T, From, To, Effect>
746where
747 T: StateMachineImpl,
748 Storage: StateStorage,
749{
750 pub fn call<Args>(mut self, args: Args) -> State<Storage, T, To>
751 where
752 Storage: SPinMut,
753 T::Standin: Transition<From, To>,
754 <T::Standin as Transition<From, To>>::F: crate::TransitionSignature<Args>,
755 From: crate::StateTrait,
756 To: crate::ConcreteStateTrait,
757 Effect: PinnedTransitionEffect<T, From, To, Args>,
758 {
759 Effect::apply(Storage::s_pin_mut(&mut self.state.inner), args);
760 Storage::complete_transition_after_effect(self.state, self.callsite)
761 }
762}
763
764impl<Storage, T, From, To> ConcreteProofTransitionCall<Storage, T, From, To>
765where
766 T: StateMachineImpl,
767 Storage: StateStorage,
768{
769 pub fn call<Args>(mut self, args: Args) -> State<Storage, T, To>
770 where
771 T: TransitionEffectSelector<From, To>,
772 Storage: SMut,
773 T::Standin: Transition<From, To>,
774 <T::Standin as Transition<From, To>>::F: crate::TransitionSignature<Args>,
775 From: crate::StateTrait,
776 To: crate::ConcreteStateTrait,
777 <T as TransitionEffectSelector<From, To>>::Effect: TransitionEffect<T, From, To, Args>,
778 {
779 <T as TransitionEffectSelector<From, To>>::Effect::apply(
780 Storage::s_mut(&mut self.state.inner),
781 args,
782 );
783 Storage::complete_transition_after_effect(self.state, self.callsite)
784 }
785}
786
787impl<Storage, T, From, Marker, To>
788 KindProofTransitionCall<Storage, T, From, Marker, To, ConcreteStateKind>
789where
790 T: StateMachineImpl,
791 Storage: StateStorage,
792 From: crate::StateTrait,
793 To: crate::ConcreteStateTrait,
794 Marker: crate::StateMarker,
795{
796 pub fn call<Args>(mut self, args: Args) -> State<Storage, T, To>
797 where
798 T: TransitionEffectSelector<From, To>,
799 Storage: SMut,
800 T::Standin: Transition<From, To>,
801 <T::Standin as Transition<From, To>>::F: crate::TransitionSignature<Args>,
802 From: crate::ConcreteStateTrait,
803 To: crate::ConcreteStateTrait,
804 <T as TransitionEffectSelector<From, To>>::Effect: TransitionEffect<T, From, To, Args>,
805 {
806 <T as TransitionEffectSelector<From, To>>::Effect::apply(
807 Storage::s_mut(&mut self.state.inner),
808 args,
809 );
810 Storage::complete_transition_after_effect(self.state, self.callsite)
811 }
812}
813
814impl<Storage, T, From, Marker, To>
815 KindProofTransitionCall<Storage, T, From, Marker, To, UnionStateKind>
816where
817 T: StateMachineImpl,
818 Storage: StateStorage,
819 From: crate::StateTrait,
820 To: crate::ConcreteStateTrait,
821 Marker: StateUnionDiscriminant,
822{
823 pub fn call<Args>(self, args: Args) -> State<Storage, T, To>
824 where
825 Storage: SMut,
826 From: crate::StateTrait + crate::In<Marker>,
827 Marker: StateUnionDiscriminatedTransition<T, To, Args>,
828 To: crate::ConcreteStateTrait,
829 {
830 let state = <From as crate::In<Marker>>::into_discriminated(self.state);
831 Marker::transition(state, args, self.callsite)
832 }
833}
834
835impl<Storage, T, From, Marker, To> StateUnionProofTransitionCall<Storage, T, From, Marker, To>
836where
837 T: StateMachineImpl,
838 Storage: StateStorage,
839 From: crate::StateTrait,
840 To: crate::ConcreteStateTrait,
841 Marker: StateUnionDiscriminant,
842{
843 pub fn call<Args>(mut self, args: Args) -> State<Storage, T, To>
844 where
845 Storage: SMut,
846 From: StateUnionErased<Marker>,
847 Marker: StateUnionSharedTransitionEffect<T, To, Args>,
848 To: crate::ConcreteStateTrait,
849 {
850 Marker::apply(Storage::s_mut(&mut self.state.inner), args);
851 Storage::complete_transition_after_effect(self.state, self.callsite)
852 }
853}
854
855impl<Storage, T, From, Marker, To> PinnedStateUnionProofTransitionCall<Storage, T, From, Marker, To>
856where
857 T: StateMachineImpl,
858 Storage: StateStorage,
859 From: crate::StateTrait,
860 To: crate::ConcreteStateTrait,
861 Marker: StateUnionDiscriminant,
862{
863 pub fn call<Args>(mut self, args: Args) -> State<Storage, T, To>
864 where
865 Storage: SPinMut,
866 From: StateUnionErased<Marker>,
867 Marker: crate::StateUnionSharedPinnedTransitionEffect<T, To, Args>,
868 To: crate::ConcreteStateTrait,
869 {
870 Marker::apply_pinned(Storage::s_pin_mut(&mut self.state.inner), args);
871 Storage::complete_transition_after_effect(self.state, self.callsite)
872 }
873}
874
875impl<Storage, T, Marker, To> DiscriminatedTransitionCall<Storage, T, Marker, To>
876where
877 T: StateMachineImpl,
878 Storage: StateStorage,
879 Marker: StateUnionDiscriminant,
880{
881 pub fn call<Args>(self, args: Args) -> State<Storage, T, To>
882 where
883 Storage: SMut,
884 Marker: StateUnionDiscriminatedTransition<T, To, Args>,
885 To: crate::ConcreteStateTrait,
886 {
887 Marker::transition(self.state, args, self.callsite)
888 }
889}
890
891impl<Storage, T, Marker, To> PinnedDiscriminatedTransitionCall<Storage, T, Marker, To>
892where
893 T: StateMachineImpl,
894 Storage: StateStorage,
895 Marker: StateUnionDiscriminant,
896{
897 pub fn call<Args>(self, args: Args) -> State<Storage, T, To>
898 where
899 Storage: SPinMut,
900 Marker: crate::StateUnionDiscriminatedPinnedTransition<T, To, Args>,
901 To: crate::ConcreteStateTrait,
902 {
903 Marker::pinned_transition(self.state, args, self.callsite)
904 }
905}
906
907#[must_use]
909#[track_caller]
910pub fn transition_state<Storage, T, S, Next>(
911 state: State<Storage, T, S>,
912 _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
913) -> StateTransitionCall<Storage, T, S, Next>
914where
915 T: StateMachineImpl,
916 Storage: StateStorage,
917 T::Standin: Transition<S, Next>,
918 S: crate::StateTrait,
919 Next: crate::ConcreteStateTrait,
920{
921 StateTransitionCall {
922 state,
923 #[cfg(feature = "tracing")]
924 callsite: Location::caller(),
925 marker: PhantomData,
926 }
927}
928
929#[doc(hidden)]
931#[must_use]
932#[track_caller]
933pub fn transition_state_with_effect<Storage, T, S, Next, Effect>(
934 state: State<Storage, T, S>,
935 _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
936) -> EffectTransitionCall<Storage, T, S, Next, Effect>
937where
938 T: StateMachineImpl,
939 Storage: StateStorage,
940 T::Standin: Transition<S, Next>,
941 S: crate::StateTrait,
942 Next: crate::ConcreteStateTrait,
943{
944 EffectTransitionCall {
945 state,
946 callsite: transition_callsite(),
947 marker: PhantomData,
948 }
949}
950
951#[doc(hidden)]
953#[must_use]
954#[track_caller]
955pub fn transition_state_with_pinned_effect<Storage, T, S, Next, Effect>(
956 state: State<Storage, T, S>,
957 _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
958) -> PinnedEffectTransitionCall<Storage, T, S, Next, Effect>
959where
960 T: StateMachineImpl,
961 Storage: StateStorage,
962 T::Standin: Transition<S, Next>,
963 S: crate::StateTrait,
964 Next: crate::ConcreteStateTrait,
965{
966 PinnedEffectTransitionCall {
967 state,
968 callsite: transition_callsite(),
969 marker: PhantomData,
970 }
971}
972
973#[doc(hidden)]
975#[must_use]
976#[track_caller]
977pub fn transition_state_with_static_union_pinned_proof<Storage, T, S, Marker, Next>(
978 state: State<Storage, T, S>,
979 _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
980) -> PinnedStateUnionProofTransitionCall<Storage, T, S, Marker, Next>
981where
982 T: StateMachineImpl,
983 Storage: StateStorage,
984 S: crate::StateTrait + StateUnionErased<Marker>,
985 Marker: StateUnionDiscriminant + crate::StateUnionSharedPinnedEffect<T, Next>,
986 Next: crate::ConcreteStateTrait,
987{
988 PinnedStateUnionProofTransitionCall {
989 state,
990 callsite: transition_callsite(),
991 marker: PhantomData,
992 }
993}
994
995#[doc(hidden)]
997#[must_use]
998#[track_caller]
999pub fn transition_state_with_union_proof<Storage, T, S, Marker, Next>(
1000 proven: StateUnionProvenState<Storage, T, S, Marker, Next>,
1001 _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
1002) -> StateUnionProofTransitionCall<Storage, T, S, Marker, Next>
1003where
1004 T: StateMachineImpl,
1005 Storage: StateStorage,
1006 S: StateUnionErased<Marker>,
1007 Marker: StateUnionSharedEffect<T, Next>,
1008 Next: crate::ConcreteStateTrait,
1009{
1010 StateUnionProofTransitionCall {
1011 state: proven.state,
1012 callsite: transition_callsite(),
1013 marker: PhantomData,
1014 }
1015}
1016
1017#[doc(hidden)]
1019#[must_use]
1020#[track_caller]
1021pub fn transition_state_with_concrete_proof<Storage, T, S, Marker, Next>(
1022 proven: StateConcreteProvenState<Storage, T, S, Marker, Next>,
1023 token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
1024) -> EffectTransitionCall<Storage, T, S, Next, <T as TransitionEffectSelector<S, Next>>::Effect>
1025where
1026 T: StateMachineImpl + TransitionEffectSelector<S, Next>,
1027 Storage: StateStorage,
1028 T::Standin: Transition<S, Next>,
1029 Marker: StateUnionDiscriminant,
1030 S: crate::StateTrait,
1031 Next: crate::ConcreteStateTrait,
1032{
1033 transition_state_with_effect(proven.state, token)
1034}
1035
1036#[doc(hidden)]
1038#[must_use]
1039#[track_caller]
1040pub fn transition_state_with_concrete_transition_proof<Storage, T, S, Marker, Next>(
1041 proven: StateWithProof<Storage, T, S, StateConcreteTransitionProof<T, S, Marker, Next>>,
1042 token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
1043) -> EffectTransitionCall<Storage, T, S, Next, <T as TransitionEffectSelector<S, Next>>::Effect>
1044where
1045 T: StateMachineImpl + TransitionEffectSelector<S, Next>,
1046 Storage: StateStorage,
1047 T::Standin: Transition<S, Next>,
1048 Marker: StateUnionDiscriminant,
1049 S: crate::StateTrait,
1050 Next: crate::ConcreteStateTrait,
1051{
1052 let StateWithProof {
1053 state,
1054 proof: _proof,
1055 } = proven;
1056 transition_state_with_effect(state, token)
1057}
1058
1059#[doc(hidden)]
1061#[must_use]
1062#[track_caller]
1063pub fn transition_state_with_union_transition_proof<Storage, T, S, Marker, Next>(
1064 proven: StateWithProof<Storage, T, S, StateUnionTransitionProof<T, S, Marker, Next>>,
1065 _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
1066) -> StateUnionProofTransitionCall<Storage, T, S, Marker, Next>
1067where
1068 T: StateMachineImpl,
1069 Storage: StateStorage,
1070 S: StateUnionErased<Marker>,
1071 Marker: StateUnionSharedEffect<T, Next>,
1072 Next: crate::ConcreteStateTrait,
1073{
1074 let StateWithProof {
1075 state,
1076 proof: _proof,
1077 } = proven;
1078 StateUnionProofTransitionCall {
1079 state,
1080 callsite: transition_callsite(),
1081 marker: PhantomData,
1082 }
1083}
1084
1085#[doc(hidden)]
1087#[must_use]
1088#[track_caller]
1089pub fn transition_state_with_static_union_proof<Storage, T, S, Marker, Next>(
1090 state: State<Storage, T, S>,
1091 _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
1092) -> StateUnionProofTransitionCall<Storage, T, S, Marker, Next>
1093where
1094 T: StateMachineImpl,
1095 Storage: StateStorage,
1096 S: crate::StateTrait + StateUnionErased<Marker> + crate::UnionTransitionProof<T, Marker, Next>,
1097 Marker: StateUnionDiscriminant + StateUnionSharedEffect<T, Next>,
1098 Next: crate::ConcreteStateTrait,
1099{
1100 StateUnionProofTransitionCall {
1101 state,
1102 callsite: transition_callsite(),
1103 marker: PhantomData,
1104 }
1105}
1106
1107#[doc(hidden)]
1109#[must_use]
1110#[track_caller]
1111pub fn transition_state_with_erased_transition_proof<Storage, T, S, Marker, Next, Proof>(
1112 proven: StateWithProof<Storage, T, S, Proof>,
1113 _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
1114) -> StateUnionProofTransitionCall<Storage, T, S, Marker, Next>
1115where
1116 T: StateMachineImpl,
1117 Storage: StateStorage,
1118 S: crate::StateTrait,
1119 Marker: StateUnionDiscriminant,
1120 Next: crate::ConcreteStateTrait,
1121{
1122 let StateWithProof {
1123 state,
1124 proof: _proof,
1125 } = proven;
1126 StateUnionProofTransitionCall {
1127 state,
1128 callsite: transition_callsite(),
1129 marker: PhantomData,
1130 }
1131}
1132
1133#[doc(hidden)]
1135#[must_use]
1136#[track_caller]
1137pub fn transition_state_with_concrete_kind_proof<Storage, T, S, Marker, Next, Kind>(
1138 proven: StateWithProof<
1139 Storage,
1140 T,
1141 S,
1142 crate::TransitionProof<Storage, T, S, Marker, Next, Kind>,
1143 >,
1144 _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
1145) -> ConcreteProofTransitionCall<Storage, T, S, Next>
1146where
1147 T: StateMachineImpl,
1148 Storage: StateStorage,
1149 S: crate::StateTrait,
1150 Marker: crate::StateMarker,
1151 Next: crate::ConcreteStateTrait,
1152 Kind: StateKind,
1153{
1154 let StateWithProof {
1155 state,
1156 proof: _proof,
1157 } = proven;
1158 ConcreteProofTransitionCall {
1159 state,
1160 callsite: transition_callsite(),
1161 marker: PhantomData,
1162 }
1163}
1164
1165#[doc(hidden)]
1167#[must_use]
1168#[track_caller]
1169pub fn transition_state_with_kind_proof<Storage, T, S, Marker, Next, Kind>(
1170 proven: StateWithProof<
1171 Storage,
1172 T,
1173 S,
1174 crate::TransitionProof<Storage, T, S, Marker, Next, Kind>,
1175 >,
1176 _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
1177) -> KindProofTransitionCall<Storage, T, S, Marker, Next, Kind>
1178where
1179 T: StateMachineImpl,
1180 Storage: StateStorage,
1181 S: crate::StateTrait,
1182 Marker: crate::StateMarker,
1183 Next: crate::ConcreteStateTrait,
1184 Kind: StateKind,
1185{
1186 let StateWithProof {
1187 state,
1188 proof: _proof,
1189 } = proven;
1190 KindProofTransitionCall {
1191 state,
1192 callsite: transition_callsite(),
1193 marker: PhantomData,
1194 }
1195}
1196
1197#[doc(hidden)]
1198pub fn transition_concrete_after_effect<Storage, T, From, To, Args, Effect>(
1199 mut state: State<Storage, T, From>,
1200 args: Args,
1201 callsite: TransitionCallsite,
1202) -> State<Storage, T, To>
1203where
1204 T: StateMachineImpl,
1205 Storage: SMut,
1206 From: crate::StateTrait,
1207 To: crate::ConcreteStateTrait,
1208 Effect: TransitionEffect<T, From, To, Args>,
1209{
1210 Effect::apply(Storage::s_mut(&mut state.inner), args);
1211 Storage::complete_transition_after_effect(state, callsite)
1212}
1213
1214#[doc(hidden)]
1215pub fn transition_concrete_after_pinned_effect<Storage, T, From, To, Args, Effect>(
1216 mut state: State<Storage, T, From>,
1217 args: Args,
1218 callsite: TransitionCallsite,
1219) -> State<Storage, T, To>
1220where
1221 T: StateMachineImpl,
1222 Storage: SPinMut,
1223 From: crate::StateTrait,
1224 To: crate::ConcreteStateTrait,
1225 Effect: PinnedTransitionEffect<T, From, To, Args>,
1226{
1227 Effect::apply(Storage::s_pin_mut(&mut state.inner), args);
1228 Storage::complete_transition_after_effect(state, callsite)
1229}
1230
1231#[doc(hidden)]
1233#[must_use]
1234#[track_caller]
1235pub fn transition_discriminated_state<Storage, T, Marker, Next>(
1236 state: DiscriminatedState<Storage, T, Marker>,
1237 _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
1238) -> DiscriminatedTransitionCall<Storage, T, Marker, Next>
1239where
1240 T: StateMachineImpl,
1241 Storage: StateStorage,
1242 Marker: StateUnionDiscriminant,
1243 Next: crate::StateTrait,
1244{
1245 DiscriminatedTransitionCall {
1246 state,
1247 callsite: transition_callsite(),
1248 marker: PhantomData,
1249 }
1250}
1251
1252#[doc(hidden)]
1254#[must_use]
1255#[track_caller]
1256pub fn transition_discriminated_state_pinned<Storage, T, Marker, Next>(
1257 state: DiscriminatedState<Storage, T, Marker>,
1258 _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
1259) -> PinnedDiscriminatedTransitionCall<Storage, T, Marker, Next>
1260where
1261 T: StateMachineImpl,
1262 Storage: StateStorage,
1263 Marker: StateUnionDiscriminant,
1264 Next: crate::StateTrait,
1265{
1266 PinnedDiscriminatedTransitionCall {
1267 state,
1268 callsite: transition_callsite(),
1269 marker: PhantomData,
1270 }
1271}
1272
1273#[doc(hidden)]
1275#[must_use]
1276pub fn proven_state<To, Storage, T, S>(
1277 state: State<Storage, T, S>,
1278) -> StateUnionProvenState<Storage, T, S, <To as StateUnionProofTarget<T, S>>::Marker, To>
1279where
1280 T: StateMachineImpl,
1281 Storage: StateStorage,
1282 S: StateUnionErased<<To as StateUnionProofTarget<T, S>>::Marker>,
1283 To: StateUnionProofTarget<T, S>,
1284{
1285 StateUnionProvenState {
1286 state,
1287 marker: PhantomData,
1288 }
1289}
1290
1291#[doc(hidden)]
1293#[must_use]
1294pub fn proven_union_state<Marker, To, Storage, T, S>(
1295 state: State<Storage, T, S>,
1296) -> StateUnionProvenState<Storage, T, S, Marker, To>
1297where
1298 T: StateMachineImpl,
1299 Storage: StateStorage,
1300 S: StateUnionErased<Marker>,
1301 Marker: StateUnionSharedEffect<T, To>,
1302 To: crate::ConcreteStateTrait,
1303{
1304 StateUnionProvenState {
1305 state,
1306 marker: PhantomData,
1307 }
1308}
1309
1310#[must_use]
1317pub fn pin_ref<Storage, T, S>(state: &State<Storage, T, S>) -> Pin<&T>
1318where
1319 T: StateMachineImpl,
1320 Storage: SPinRef,
1321{
1322 Storage::s_pin_ref(&state.inner)
1323}
1324
1325pub fn pin_mut<Storage, T, S>(state: &mut State<Storage, T, S>) -> Pin<&mut T>
1332where
1333 T: StateMachineImpl,
1334 Storage: SPinMut,
1335{
1336 Storage::s_pin_mut(&mut state.inner)
1337}
1338
1339impl<Storage, T, S> State<Storage, T, S>
1340where
1341 T: StateMachineImpl,
1342 Storage: StateStorage,
1343{
1344 pub(crate) fn from_inner(inner: Storage::Inner<T, S>) -> Self {
1345 Self {
1346 inner,
1347 marker: PhantomData,
1348 }
1349 }
1350
1351 #[doc(hidden)]
1353 #[must_use]
1354 pub fn with<Marker, To, Kind>(
1355 self,
1356 proof: crate::TransitionProof<Storage, T, S, Marker, To, Kind>,
1357 ) -> StateWithProof<Storage, T, S, crate::TransitionProof<Storage, T, S, Marker, To, Kind>>
1358 where
1359 S: crate::StateTrait,
1360 Marker: crate::StateMarker,
1361 To: crate::ConcreteStateTrait,
1362 Kind: crate::StateKind,
1363 {
1364 StateWithProof { state: self, proof }
1365 }
1366}
1367
1368impl<T, S> State<StorageStateOwned, T, S>
1369where
1370 T: StateMachineImpl,
1371{
1372 #[must_use]
1393 pub fn new(value: T) -> Self
1394 where
1395 T::Standin: Initial<S>,
1396 {
1397 State {
1398 inner: <StorageStateOwned as StateStorageNew>::new(value),
1399 marker: PhantomData,
1400 }
1401 }
1402
1403 #[must_use]
1409 pub fn from_concrete(value: ConcreteStated<T, S>) -> Self
1410 where
1411 S: crate::ConcreteStateTrait,
1412 {
1413 State {
1414 inner: crate::StateOwned {
1415 value: value.into_raw(),
1416 state: PhantomData,
1417 #[cfg(feature = "tracing")]
1418 trace: Vec::new(),
1419 },
1420 marker: PhantomData,
1421 }
1422 }
1423
1424 #[must_use]
1430 pub fn into_concrete(state: Self) -> ConcreteStated<T, S>
1431 where
1432 S: crate::ConcreteStateTrait,
1433 {
1434 ConcreteStated {
1435 value: state.inner.value,
1436 state: PhantomData,
1437 }
1438 }
1439}
1440
1441impl<T, S> State<StorageStateOwnedBox, T, S>
1442where
1443 T: StateMachineImpl,
1444{
1445 #[must_use]
1455 pub fn new(state: State<StorageStateOwned, T, S>) -> Self {
1456 State {
1457 inner: crate::StateOwned {
1458 value: Box::new(state.inner.value),
1459 state: PhantomData,
1460 #[cfg(feature = "tracing")]
1461 trace: state.inner.trace,
1462 },
1463 marker: PhantomData,
1464 }
1465 }
1466
1467 #[must_use]
1478 pub fn unbox(state: Self) -> State<StorageStateOwned, T, S> {
1479 State {
1480 inner: crate::StateOwned {
1481 value: *state.inner.value,
1482 state: PhantomData,
1483 #[cfg(feature = "tracing")]
1484 trace: state.inner.trace,
1485 },
1486 marker: PhantomData,
1487 }
1488 }
1489}
1490
1491impl<T, S> State<StorageStateOwnedPinBox, T, S>
1492where
1493 T: StateMachineImpl,
1494{
1495 #[must_use]
1505 pub fn new(state: State<StorageStateOwnedBox, T, S>) -> Self {
1506 State {
1507 inner: crate::StateOwned {
1508 value: Box::into_pin(state.inner.value),
1509 state: PhantomData,
1510 #[cfg(feature = "tracing")]
1511 trace: state.inner.trace,
1512 },
1513 marker: PhantomData,
1514 }
1515 }
1516
1517 #[must_use]
1556 pub fn into_boxed(state: Self) -> State<StorageStateOwnedBox, T, S>
1557 where
1558 T: Unpin,
1559 {
1560 State {
1561 inner: crate::StateOwned {
1562 value: Pin::into_inner(state.inner.value),
1563 state: PhantomData,
1564 #[cfg(feature = "tracing")]
1565 trace: state.inner.trace,
1566 },
1567 marker: PhantomData,
1568 }
1569 }
1570}
1571
1572#[cfg(feature = "unique-rc-arc")]
1573impl<T, S> State<StorageStateOwnedUniqueRc, T, S>
1574where
1575 T: StateMachineImpl,
1576{
1577 #[must_use]
1582 pub fn new(state: State<StorageStateOwned, T, S>) -> Self {
1583 State {
1584 inner: crate::StateOwned {
1585 value: UniqueRc::new(state.inner.value),
1586 state: PhantomData,
1587 #[cfg(feature = "tracing")]
1588 trace: state.inner.trace,
1589 },
1590 marker: PhantomData,
1591 }
1592 }
1593}
1594
1595#[cfg(feature = "unique-rc-arc")]
1596impl<T, S> State<StorageStateOwnedUniqueArc, T, S>
1597where
1598 T: StateMachineImpl,
1599{
1600 #[must_use]
1605 pub fn new(state: State<StorageStateOwned, T, S>) -> Self {
1606 State {
1607 inner: crate::StateOwned {
1608 value: UniqueArc::new(state.inner.value),
1609 state: PhantomData,
1610 #[cfg(feature = "tracing")]
1611 trace: state.inner.trace,
1612 },
1613 marker: PhantomData,
1614 }
1615 }
1616}
1617
1618impl<Storage, T, S> Deref for State<Storage, T, S>
1619where
1620 T: StateMachineImpl,
1621 Storage: SRef,
1622{
1623 type Target = T;
1624
1625 fn deref(&self) -> &Self::Target {
1626 Storage::s_ref(&self.inner)
1627 }
1628}
1629
1630impl<Storage, T, S> DerefMut for State<Storage, T, S>
1631where
1632 T: StateMachineImpl,
1633 Storage: SMut,
1634{
1635 fn deref_mut(&mut self) -> &mut Self::Target {
1636 Storage::s_mut(&mut self.inner)
1637 }
1638}