MagicStateMachines 0.1.0

Ergonomic typestate wrappers for compiler-enforced state machines with separable contracts
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
mod owned;

use crate::{
    ConcreteStateKind, DiscriminatedState, Initial, StateConcreteProvenState,
    StateConcreteTransitionProof, StateKind, StateMachineImpl, StateUnionDiscriminant,
    StateUnionDiscriminatedTransition, StateUnionErased, StateUnionProofTarget,
    StateUnionProvenState, StateUnionSharedEffect, StateUnionSharedTransitionEffect,
    StateUnionTransitionProof, StateWithProof, Transition, UnionStateKind, state_trait,
};
use core::marker::PhantomData;
use core::ops::{Deref, DerefMut};
#[cfg(feature = "tracing")]
use core::panic::Location;
use core::pin::Pin;
#[cfg(feature = "unique-rc-arc")]
use std::rc::UniqueRc;
#[cfg(feature = "unique-rc-arc")]
use std::sync::UniqueArc;

pub use owned::{SOwned, StorageStateOwned, StorageStateOwnedBox, StorageStateOwnedPinBox};
#[cfg(feature = "unique-rc-arc")]
pub use owned::{StorageStateOwnedUniqueArc, StorageStateOwnedUniqueRc};

/// Owned state whose runtime value is stored in `Box<T>`.
///
/// This is only a type alias for `State<StorageStateOwnedBox, T, S>`. Create it
/// from an already-owned state with [`SBox::new`](State<StorageStateOwnedBox, T, S>::new)
/// so the current state is preserved while changing containers:
///
/// ```ignore
/// let owned: State<SOwned, Connection, Disconnected> = State::new(connection);
/// let boxed: SBox<Connection, Disconnected> = SBox::new(owned);
/// ```
pub type SBox<T, S> = State<StorageStateOwnedBox, T, S>;
/// Owned state whose runtime value is stored in `Pin<Box<T>>`.
///
/// This is the pinned counterpart to [`SBox`]. Pinning consumes a boxed state
/// rather than raw `T`, again preserving the already-proven current state.
///
/// Pinned storage is deliberately not just "boxed plus `SMut`". For
/// `T: !Unpin`, the backend can expose [`Pin<&T>`](core::pin::Pin) and
/// `Pin<&mut T>` through [`SPinRef`] and [`SPinMut`], but it does not expose
/// `&mut T`. That means ordinary transitions requiring `S: SMut` stay
/// unavailable, while pinned transition methods can still be written:
///
/// ```ignore
/// impl Connection {
///     fn connect<S>(self: State<S, Self, Disconnected>) -> State<S, Self, Connected>
///     where
///         S: magicstatemachines::SPinMut,
///     {
///         magicstatemachines::transition!(pin self)
///     }
/// }
/// ```
pub type SPinBox<T, S> = State<StorageStateOwnedPinBox, T, S>;

fn retag_owned<T, From, To>(inner: crate::StateOwned<T, From>) -> crate::StateOwned<T, To> {
    crate::StateOwned {
        value: inner.value,
        state: PhantomData,
        #[cfg(feature = "tracing")]
        trace: inner.trace,
    }
}

type StateMarker<Storage, T, S> = PhantomData<fn() -> (Storage, T, S)>;
type TransitionMarker<Storage, T, From, To> = PhantomData<fn() -> (Storage, T, From, To)>;

/// Selects the implementation-side effect for a declared transition.
#[doc(hidden)]
pub trait TransitionEffectSelector<From, To>: StateMachineImpl {
    type Effect;
}

/// Selects the pinned implementation-side effect for a declared transition.
#[doc(hidden)]
pub trait PinnedTransitionEffectSelector<From, To>: StateMachineImpl {
    type Effect;
}

/// Applies implementation-side transition effects before the state is retagged.
#[doc(hidden)]
pub trait TransitionEffect<T, From, To, Args>
where
    T: StateMachineImpl,
{
    fn apply(value: &mut T, args: Args);
}

/// Applies implementation-side transition effects through pinned access.
///
/// This is the pinned counterpart to [`TransitionEffect`]. It is used by
/// `pinned transition` entries in [`StateMachineImpl!`](macro@crate::StateMachineImpl)
/// and by [`transition!(pin state, ...)`](macro@crate::transition). The effect
/// receives `Pin<&mut T>`, so a `!Unpin` runtime can update interior fields or
/// call pinned methods without ever exposing `&mut T`.
#[doc(hidden)]
pub trait PinnedTransitionEffect<T, From, To, Args>
where
    T: StateMachineImpl,
{
    fn apply(value: Pin<&mut T>, args: Args);
}

/// Selects where a storage backend's authoritative state marker is inferred.
#[doc(hidden)]
pub trait InferenceKind {
    type Inference: StateInference;
}

/// State marker inference carried by discriminated storage.
#[doc(hidden)]
pub trait StateInference {
    fn new<Storage, T, S>(inner: &Storage::Inner<T, S>) -> Self
    where
        Storage: StateStorage,
        T: StateMachineImpl,
        S: crate::ConcreteStateTrait;

    fn from_erased(state: state_trait::ErasedState) -> Self;

    fn state<Storage, T, S>(&self, inner: &Storage::Inner<T, S>) -> state_trait::ErasedState
    where
        Storage: StateStorage,
        T: StateMachineImpl,
        S: crate::StateTrait;
}

/// Inference stored outside the wrapped backend, used by type-only storage.
#[doc(hidden)]
pub struct OuterInference;

/// Inference delegated to the wrapped backend, used by runtime-state storage.
#[doc(hidden)]
pub struct InnerInference;

/// ZST inference value for [`InnerInference`].
#[doc(hidden)]
#[derive(Clone, Copy)]
pub struct InnerStateInference;

impl InferenceKind for OuterInference {
    type Inference = state_trait::ErasedState;
}

impl InferenceKind for InnerInference {
    type Inference = InnerStateInference;
}

impl StateInference for state_trait::ErasedState {
    fn new<Storage, T, S>(_inner: &Storage::Inner<T, S>) -> Self
    where
        Storage: StateStorage,
        T: StateMachineImpl,
        S: crate::ConcreteStateTrait,
    {
        state_trait::erased_state::<S>()
    }

    fn from_erased(state: state_trait::ErasedState) -> Self {
        state
    }

    fn state<Storage, T, S>(&self, _inner: &Storage::Inner<T, S>) -> state_trait::ErasedState
    where
        Storage: StateStorage,
        T: StateMachineImpl,
        S: crate::StateTrait,
    {
        state_trait::clone_erased(self)
    }
}

impl StateInference for InnerStateInference {
    fn new<Storage, T, S>(_inner: &Storage::Inner<T, S>) -> Self
    where
        Storage: StateStorage,
        T: StateMachineImpl,
        S: crate::ConcreteStateTrait,
    {
        Self
    }

    fn from_erased(_state: state_trait::ErasedState) -> Self {
        Self
    }

    fn state<Storage, T, S>(&self, inner: &Storage::Inner<T, S>) -> state_trait::ErasedState
    where
        Storage: StateStorage,
        T: StateMachineImpl,
        S: crate::StateTrait,
    {
        Storage::inferred_state(inner)
    }
}

/// Storage backend used by [`State`].
///
/// `State<Storage, T, S>` is only a typed view; the actual representation is
/// selected by this trait. The same implementation methods can therefore work
/// for directly owned values, boxed values, pinned values, shared guard views,
/// and discriminated union views as long as they ask for the right capability
/// bound.
///
/// Backend authors provide an `Inner<T, S>` generic associated type. The
/// library retags that `Inner` after a successful transition, so the backend
/// controls where data is stored while the state-machine contract still
/// controls which retags are legal.
///
/// Most users do not implement this trait. They choose one of the built-in
/// storage aliases and write bounds on methods:
///
/// ```ignore
/// use magicstatemachines::{SRef, SMut, State, transition};
/// use test_def::{InOnline, Online};
/// use test_def::states::{Authenticated, Connected};
///
/// impl Connection {
///     fn endpoint<S>(self: &State<S, Self, impl InOnline>) -> &str
///     where
///         S: SRef,
///     {
///         &self.endpoint
///     }
///
///     fn authenticate<S>(
///         self: State<S, Self, Connected>,
///         user: String,
///     ) -> State<S, Self, Authenticated>
///     where
///         S: SMut,
///     {
///         transition!(self, user)
///     }
/// }
/// ```
pub trait StateStorage: Sized {
    /// Selects how [`SDiscriminated`](crate::SDiscriminated) recovers the current state marker.
    type Inference: InferenceKind = OuterInference;

    /// Concrete state representation used by this storage backend.
    type Inner<T, S>
    where
        T: StateMachineImpl;

    /// Type that carries the state-machine implementation contract.
    type Machine<T>: StateMachineImpl<Standin = T::Standin, Impl = T::Impl, TransitionToken = T::TransitionToken>
    where
        T: StateMachineImpl;

    #[doc(hidden)]
    fn retag<T, From, To>(inner: Self::Inner<T, From>) -> Self::Inner<T, To>
    where
        T: StateMachineImpl;

    /// Retags a state after checking that `Args` matches the declared transition signature.
    ///
    /// Storage backends normally delegate this to their inner representation.
    /// Implementation crates should call transitions through [`transition!`](macro@crate::transition)
    /// rather than invoking this method directly.
    fn complete_transition<T, From, To, Args>(
        state: State<Self, T, From>,
        args: Args,
        callsite: TransitionCallsite,
    ) -> State<Self, T, To>
    where
        T: StateMachineImpl,
        From: crate::StateTrait,
        To: crate::ConcreteStateTrait,
        T::Standin: Transition<From, To>,
        <T::Standin as Transition<From, To>>::F: crate::TransitionSignature<Args>;

    #[doc(hidden)]
    fn complete_transition_after_effect<T, From, To>(
        state: State<Self, T, From>,
        callsite: TransitionCallsite,
    ) -> State<Self, T, To>
    where
        T: StateMachineImpl,
        From: crate::StateTrait,
        To: crate::ConcreteStateTrait;

    #[doc(hidden)]
    fn inferred_state<T, State>(inner: &Self::Inner<T, State>) -> state_trait::ErasedState
    where
        T: StateMachineImpl,
        State: crate::StateTrait,
    {
        let _ = inner;
        state_trait::static_erased_state::<State>()
    }
}

#[doc(hidden)]
pub fn complete_transition_after_effect<Storage, T, From, To>(
    state: State<Storage, T, From>,
    callsite: TransitionCallsite,
) -> State<Storage, T, To>
where
    Storage: StateStorage,
    T: StateMachineImpl,
    From: crate::StateTrait,
    To: crate::ConcreteStateTrait,
{
    Storage::complete_transition_after_effect(state, callsite)
}

/// Storage backend that can create initial owned state.
///
/// This is the capability behind [`State::new`]. The `Initial` bound prevents
/// raw runtime data from being introduced in a non-initial state.
pub trait StateStorageNew: StateStorage {
    /// Creates a backend-specific inner value in an allowed initial state.
    fn new<T, State>(value: T) -> Self::Inner<T, State>
    where
        T: StateMachineImpl,
        <Self::Machine<T> as StateMachineImpl>::Standin: Initial<State>;
}

/// Storage backend that can expose a runtime reference.
///
/// Use `S: SRef` for methods that only read the runtime implementation. This
/// bound covers owned states, boxed/pinned states, and read or write guards
/// from shared storage.
pub trait SRef: StateStorage {
    /// Borrows the runtime implementation from this storage backend.
    fn s_ref<T, State>(inner: &Self::Inner<T, State>) -> &T
    where
        T: StateMachineImpl;
}

/// Storage backend that can expose a mutable runtime reference.
///
/// Use `S: SMut` for methods that mutate runtime data or call
/// [`transition!`](macro@crate::transition). Transition effect bodies always
/// receive `&mut T`, so a transition method normally needs this bound even if
/// the body is empty.
pub trait SMut: SRef {
    /// Mutably borrows the runtime implementation from this storage backend.
    fn s_mut<T, State>(inner: &mut Self::Inner<T, State>) -> &mut T
    where
        T: StateMachineImpl;
}

/// Storage backend that can expose a pinned runtime reference.
///
/// This capability is separate from [`SRef`] and [`SMut`] because pinning is
/// stronger than ordinary borrowing. A backend should implement this only when
/// it can prove the runtime value will not be moved for the lifetime of the
/// storage object. [`SPinBox`](crate::SPinBox) does; ordinary owned storage
/// intentionally does not.
///
/// Use this bound for read-only methods that must call APIs taking
/// `Pin<&Self>`:
///
/// ```ignore
/// fn stable_address<S>(self: &State<S, Self, Ready>) -> usize
/// where
///     S: magicstatemachines::SPinRef,
/// {
///     magicstatemachines::pin_ref(self).stable_address()
/// }
/// ```
pub trait SPinRef: SRef {
    /// Borrows the runtime implementation as a pinned shared reference.
    fn s_pin_ref<T, State>(inner: &Self::Inner<T, State>) -> Pin<&T>
    where
        T: StateMachineImpl;
}

/// Storage backend that can expose a pinned mutable runtime reference.
///
/// This is the capability required by pinned transition effects. It does not
/// imply [`SMut`]: a backend may safely provide `Pin<&mut T>` for `!Unpin`
/// values while still refusing to provide `&mut T`.
pub trait SPinMut: SPinRef {
    /// Mutably borrows the runtime implementation as a pinned reference.
    fn s_pin_mut<T, State>(inner: &mut Self::Inner<T, State>) -> Pin<&mut T>
    where
        T: StateMachineImpl;
}

/// Storage backend whose state token can be consumed by value.
///
/// Use this when an API must move the backend representation itself rather
/// than just read or mutate the runtime value. Most transition methods only
/// need [`SMut`].
pub trait SMove: StateStorage {}

/// A state token parameterized by storage, runtime implementation, and state.
///
/// The type parameters carry the whole proof:
///
/// - `Storage` selects how the runtime value is held, for example
///   [`SOwned`](crate::SOwned), [`StorageStateOwnedBox`], or a mutable guard
///   from shared storage.
/// - `T` is the runtime implementation type, such as `Connection`.
/// - `S` is the current state marker, such as `Disconnected` or
///   `impl InOnline`.
///
/// `State` is intentionally a deref-like wrapper with very few inherent
/// methods. State-machine behavior should be implemented on `T` with
/// arbitrary self types, not added as generic convenience methods on `State`.
/// That keeps the transition capability private to the implementation module.
///
/// ```ignore
/// impl Connection {
///     fn connect<S>(self: State<S, Self, Disconnected>) -> State<S, Self, Connected>
///     where
///         S: SMut,
///     {
///         magicstatemachines::transition!(self)
///     }
///
///     fn endpoint<S>(self: &State<S, Self, impl InOnline>) -> &str
///     where
///         S: SRef,
///     {
///         &self.endpoint
///     }
/// }
/// ```
pub struct State<Storage, T, S>
where
    T: StateMachineImpl,
    Storage: StateStorage,
{
    pub(crate) inner: Storage::Inner<T, S>,
    pub(crate) marker: StateMarker<Storage, T, S>,
}

/// A result whose success and error values are states of the same machine.
///
/// This is useful for fallible state-machine methods where both branches
/// return ownership of the same runtime value in different states:
///
/// ```ignore
/// fn authenticate_if<S>(
///     self: State<S, Connection, Connected>,
///     user: Option<String>,
/// ) -> SResult<S, Connection, Authenticated, Connected>
/// where
///     S: SMut,
/// {
///     match user {
///         Some(user) => Ok(magicstatemachines::transition!(self, user)),
///         None => Err(self),
///     }
/// }
/// ```
#[allow(type_alias_bounds)]
pub type SResult<Storage, T, OkState, ErrState>
where
    Storage: StateStorage,
    T: StateMachineImpl,
= Result<State<Storage, T, OkState>, State<Storage, T, ErrState>>;

/// A callable transition for generic [`State`] storage.
#[doc(hidden)]
pub struct StateTransitionCall<Storage, T, From, To>
where
    T: StateMachineImpl,
    Storage: StateStorage,
{
    state: State<Storage, T, From>,
    #[cfg(feature = "tracing")]
    callsite: &'static Location<'static>,
    marker: TransitionMarker<Storage, T, From, To>,
}

/// A callable transition that first routes through implementation-owned effects.
#[doc(hidden)]
pub struct EffectTransitionCall<Storage, T, From, To, Effect>
where
    T: StateMachineImpl,
    Storage: StateStorage,
{
    state: State<Storage, T, From>,
    callsite: TransitionCallsite,
    marker: PhantomData<fn() -> (To, Effect)>,
}

/// A callable transition that runs a pinned implementation-side effect first.
#[doc(hidden)]
pub struct PinnedEffectTransitionCall<Storage, T, From, To, Effect>
where
    T: StateMachineImpl,
    Storage: StateStorage,
{
    state: State<Storage, T, From>,
    callsite: TransitionCallsite,
    marker: PhantomData<fn() -> (To, Effect)>,
}

/// A callable concrete transition selected by [`StateKind`].
#[doc(hidden)]
pub struct ConcreteProofTransitionCall<Storage, T, From, To>
where
    T: StateMachineImpl,
    Storage: StateStorage,
{
    state: State<Storage, T, From>,
    callsite: TransitionCallsite,
    marker: PhantomData<fn() -> To>,
}

/// A callable transition selected by the receiver state's marker kind.
#[doc(hidden)]
pub struct KindProofTransitionCall<Storage, T, From, Marker, To, Kind>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    From: crate::StateTrait,
    Marker: crate::StateMarker,
    To: crate::ConcreteStateTrait,
    Kind: StateKind,
{
    state: State<Storage, T, From>,
    callsite: TransitionCallsite,
    marker: PhantomData<fn() -> (Marker, To, Kind)>,
}

/// A callable transition proven through a generated state union.
#[doc(hidden)]
pub struct StateUnionProofTransitionCall<Storage, T, From, Marker, To>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    From: crate::StateTrait,
    Marker: StateUnionDiscriminant,
    To: crate::ConcreteStateTrait,
{
    state: State<Storage, T, From>,
    callsite: TransitionCallsite,
    marker: PhantomData<fn() -> (Marker, To)>,
}

/// A callable pinned transition proven through a generated state union.
#[doc(hidden)]
pub struct PinnedStateUnionProofTransitionCall<Storage, T, From, Marker, To>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    From: crate::StateTrait,
    Marker: StateUnionDiscriminant,
    To: crate::ConcreteStateTrait,
{
    state: State<Storage, T, From>,
    callsite: TransitionCallsite,
    marker: PhantomData<fn() -> (Marker, To)>,
}

/// A callable discriminated-union transition that returns to the original storage after transition.
#[doc(hidden)]
pub struct DiscriminatedTransitionCall<Storage, T, Marker, To>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    Marker: StateUnionDiscriminant,
{
    state: DiscriminatedState<Storage, T, Marker>,
    callsite: TransitionCallsite,
    marker: PhantomData<fn() -> To>,
}

/// A callable pinned discriminated-union transition.
#[doc(hidden)]
pub struct PinnedDiscriminatedTransitionCall<Storage, T, Marker, To>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    Marker: StateUnionDiscriminant,
{
    state: DiscriminatedState<Storage, T, Marker>,
    callsite: TransitionCallsite,
    marker: PhantomData<fn() -> To>,
}

#[cfg(feature = "tracing")]
#[doc(hidden)]
pub type TransitionCallsite = &'static Location<'static>;

#[cfg(not(feature = "tracing"))]
#[doc(hidden)]
pub type TransitionCallsite = ();

#[doc(hidden)]
#[track_caller]
pub fn transition_callsite() -> TransitionCallsite {
    #[cfg(feature = "tracing")]
    {
        Location::caller()
    }
    #[cfg(not(feature = "tracing"))]
    {}
}

impl<Storage, T, From, To> StateTransitionCall<Storage, T, From, To>
where
    T: StateMachineImpl,
    Storage: StateStorage,
{
    #[doc(hidden)]
    pub fn call<Args>(self, args: Args) -> State<Storage, T, To>
    where
        T::Standin: Transition<From, To>,
        <T::Standin as Transition<From, To>>::F: crate::TransitionSignature<Args>,
        From: crate::StateTrait,
        To: crate::ConcreteStateTrait,
    {
        Storage::complete_transition(self.state, args, {
            #[cfg(feature = "tracing")]
            {
                self.callsite
            }
            #[cfg(not(feature = "tracing"))]
            {}
        })
    }
}

impl<Storage, T, From, To, Effect> EffectTransitionCall<Storage, T, From, To, Effect>
where
    T: StateMachineImpl,
    Storage: StateStorage,
{
    pub fn call<Args>(mut self, args: Args) -> State<Storage, T, To>
    where
        Storage: SMut,
        T::Standin: Transition<From, To>,
        <T::Standin as Transition<From, To>>::F: crate::TransitionSignature<Args>,
        From: crate::StateTrait,
        To: crate::ConcreteStateTrait,
        Effect: TransitionEffect<T, From, To, Args>,
    {
        Effect::apply(Storage::s_mut(&mut self.state.inner), args);
        Storage::complete_transition_after_effect(self.state, self.callsite)
    }
}

impl<Storage, T, From, To, Effect> PinnedEffectTransitionCall<Storage, T, From, To, Effect>
where
    T: StateMachineImpl,
    Storage: StateStorage,
{
    pub fn call<Args>(mut self, args: Args) -> State<Storage, T, To>
    where
        Storage: SPinMut,
        T::Standin: Transition<From, To>,
        <T::Standin as Transition<From, To>>::F: crate::TransitionSignature<Args>,
        From: crate::StateTrait,
        To: crate::ConcreteStateTrait,
        Effect: PinnedTransitionEffect<T, From, To, Args>,
    {
        Effect::apply(Storage::s_pin_mut(&mut self.state.inner), args);
        Storage::complete_transition_after_effect(self.state, self.callsite)
    }
}

impl<Storage, T, From, To> ConcreteProofTransitionCall<Storage, T, From, To>
where
    T: StateMachineImpl,
    Storage: StateStorage,
{
    pub fn call<Args>(mut self, args: Args) -> State<Storage, T, To>
    where
        T: TransitionEffectSelector<From, To>,
        Storage: SMut,
        T::Standin: Transition<From, To>,
        <T::Standin as Transition<From, To>>::F: crate::TransitionSignature<Args>,
        From: crate::StateTrait,
        To: crate::ConcreteStateTrait,
        <T as TransitionEffectSelector<From, To>>::Effect: TransitionEffect<T, From, To, Args>,
    {
        <T as TransitionEffectSelector<From, To>>::Effect::apply(
            Storage::s_mut(&mut self.state.inner),
            args,
        );
        Storage::complete_transition_after_effect(self.state, self.callsite)
    }
}

impl<Storage, T, From, Marker, To>
    KindProofTransitionCall<Storage, T, From, Marker, To, ConcreteStateKind>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    From: crate::StateTrait,
    To: crate::ConcreteStateTrait,
    Marker: crate::StateMarker,
{
    pub fn call<Args>(mut self, args: Args) -> State<Storage, T, To>
    where
        T: TransitionEffectSelector<From, To>,
        Storage: SMut,
        T::Standin: Transition<From, To>,
        <T::Standin as Transition<From, To>>::F: crate::TransitionSignature<Args>,
        From: crate::ConcreteStateTrait,
        To: crate::ConcreteStateTrait,
        <T as TransitionEffectSelector<From, To>>::Effect: TransitionEffect<T, From, To, Args>,
    {
        <T as TransitionEffectSelector<From, To>>::Effect::apply(
            Storage::s_mut(&mut self.state.inner),
            args,
        );
        Storage::complete_transition_after_effect(self.state, self.callsite)
    }
}

impl<Storage, T, From, Marker, To>
    KindProofTransitionCall<Storage, T, From, Marker, To, UnionStateKind>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    From: crate::StateTrait,
    To: crate::ConcreteStateTrait,
    Marker: StateUnionDiscriminant,
{
    pub fn call<Args>(self, args: Args) -> State<Storage, T, To>
    where
        Storage: SMut,
        From: crate::StateTrait + crate::In<Marker>,
        Marker: StateUnionDiscriminatedTransition<T, To, Args>,
        To: crate::ConcreteStateTrait,
    {
        let state = <From as crate::In<Marker>>::into_discriminated(self.state);
        Marker::transition(state, args, self.callsite)
    }
}

impl<Storage, T, From, Marker, To> StateUnionProofTransitionCall<Storage, T, From, Marker, To>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    From: crate::StateTrait,
    To: crate::ConcreteStateTrait,
    Marker: StateUnionDiscriminant,
{
    pub fn call<Args>(mut self, args: Args) -> State<Storage, T, To>
    where
        Storage: SMut,
        From: StateUnionErased<Marker>,
        Marker: StateUnionSharedTransitionEffect<T, To, Args>,
        To: crate::ConcreteStateTrait,
    {
        Marker::apply(Storage::s_mut(&mut self.state.inner), args);
        Storage::complete_transition_after_effect(self.state, self.callsite)
    }
}

impl<Storage, T, From, Marker, To> PinnedStateUnionProofTransitionCall<Storage, T, From, Marker, To>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    From: crate::StateTrait,
    To: crate::ConcreteStateTrait,
    Marker: StateUnionDiscriminant,
{
    pub fn call<Args>(mut self, args: Args) -> State<Storage, T, To>
    where
        Storage: SPinMut,
        From: StateUnionErased<Marker>,
        Marker: crate::StateUnionSharedPinnedTransitionEffect<T, To, Args>,
        To: crate::ConcreteStateTrait,
    {
        Marker::apply_pinned(Storage::s_pin_mut(&mut self.state.inner), args);
        Storage::complete_transition_after_effect(self.state, self.callsite)
    }
}

impl<Storage, T, Marker, To> DiscriminatedTransitionCall<Storage, T, Marker, To>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    Marker: StateUnionDiscriminant,
{
    pub fn call<Args>(self, args: Args) -> State<Storage, T, To>
    where
        Storage: SMut,
        Marker: StateUnionDiscriminatedTransition<T, To, Args>,
        To: crate::ConcreteStateTrait,
    {
        Marker::transition(self.state, args, self.callsite)
    }
}

impl<Storage, T, Marker, To> PinnedDiscriminatedTransitionCall<Storage, T, Marker, To>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    Marker: StateUnionDiscriminant,
{
    pub fn call<Args>(self, args: Args) -> State<Storage, T, To>
    where
        Storage: SPinMut,
        Marker: crate::StateUnionDiscriminatedPinnedTransition<T, To, Args>,
        To: crate::ConcreteStateTrait,
    {
        Marker::pinned_transition(self.state, args, self.callsite)
    }
}

/// Creates a callable transition for generic state storage.
#[must_use]
#[track_caller]
pub fn transition_state<Storage, T, S, Next>(
    state: State<Storage, T, S>,
    _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
) -> StateTransitionCall<Storage, T, S, Next>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    T::Standin: Transition<S, Next>,
    S: crate::StateTrait,
    Next: crate::ConcreteStateTrait,
{
    StateTransitionCall {
        state,
        #[cfg(feature = "tracing")]
        callsite: Location::caller(),
        marker: PhantomData,
    }
}

/// Creates a callable transition that runs implementation-side effects first.
#[doc(hidden)]
#[must_use]
#[track_caller]
pub fn transition_state_with_effect<Storage, T, S, Next, Effect>(
    state: State<Storage, T, S>,
    _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
) -> EffectTransitionCall<Storage, T, S, Next, Effect>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    T::Standin: Transition<S, Next>,
    S: crate::StateTrait,
    Next: crate::ConcreteStateTrait,
{
    EffectTransitionCall {
        state,
        callsite: transition_callsite(),
        marker: PhantomData,
    }
}

/// Creates a callable transition that runs pinned implementation-side effects first.
#[doc(hidden)]
#[must_use]
#[track_caller]
pub fn transition_state_with_pinned_effect<Storage, T, S, Next, Effect>(
    state: State<Storage, T, S>,
    _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
) -> PinnedEffectTransitionCall<Storage, T, S, Next, Effect>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    T::Standin: Transition<S, Next>,
    S: crate::StateTrait,
    Next: crate::ConcreteStateTrait,
{
    PinnedEffectTransitionCall {
        state,
        callsite: transition_callsite(),
        marker: PhantomData,
    }
}

/// Creates a callable pinned transition from a static union proof.
#[doc(hidden)]
#[must_use]
#[track_caller]
pub fn transition_state_with_static_union_pinned_proof<Storage, T, S, Marker, Next>(
    state: State<Storage, T, S>,
    _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
) -> PinnedStateUnionProofTransitionCall<Storage, T, S, Marker, Next>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    S: crate::StateTrait + StateUnionErased<Marker>,
    Marker: StateUnionDiscriminant + crate::StateUnionSharedPinnedEffect<T, Next>,
    Next: crate::ConcreteStateTrait,
{
    PinnedStateUnionProofTransitionCall {
        state,
        callsite: transition_callsite(),
        marker: PhantomData,
    }
}

/// Creates a callable transition from a union-membership proof.
#[doc(hidden)]
#[must_use]
#[track_caller]
pub fn transition_state_with_union_proof<Storage, T, S, Marker, Next>(
    proven: StateUnionProvenState<Storage, T, S, Marker, Next>,
    _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
) -> StateUnionProofTransitionCall<Storage, T, S, Marker, Next>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    S: StateUnionErased<Marker>,
    Marker: StateUnionSharedEffect<T, Next>,
    Next: crate::ConcreteStateTrait,
{
    StateUnionProofTransitionCall {
        state: proven.state,
        callsite: transition_callsite(),
        marker: PhantomData,
    }
}

/// Creates a callable transition from a concrete-state proof.
#[doc(hidden)]
#[must_use]
#[track_caller]
pub fn transition_state_with_concrete_proof<Storage, T, S, Marker, Next>(
    proven: StateConcreteProvenState<Storage, T, S, Marker, Next>,
    token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
) -> EffectTransitionCall<Storage, T, S, Next, <T as TransitionEffectSelector<S, Next>>::Effect>
where
    T: StateMachineImpl + TransitionEffectSelector<S, Next>,
    Storage: StateStorage,
    T::Standin: Transition<S, Next>,
    Marker: StateUnionDiscriminant,
    S: crate::StateTrait,
    Next: crate::ConcreteStateTrait,
{
    transition_state_with_effect(proven.state, token)
}

/// Creates a callable transition from an unresolved concrete-state proof.
#[doc(hidden)]
#[must_use]
#[track_caller]
pub fn transition_state_with_concrete_transition_proof<Storage, T, S, Marker, Next>(
    proven: StateWithProof<Storage, T, S, StateConcreteTransitionProof<T, S, Marker, Next>>,
    token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
) -> EffectTransitionCall<Storage, T, S, Next, <T as TransitionEffectSelector<S, Next>>::Effect>
where
    T: StateMachineImpl + TransitionEffectSelector<S, Next>,
    Storage: StateStorage,
    T::Standin: Transition<S, Next>,
    Marker: StateUnionDiscriminant,
    S: crate::StateTrait,
    Next: crate::ConcreteStateTrait,
{
    let StateWithProof {
        state,
        proof: _proof,
    } = proven;
    transition_state_with_effect(state, token)
}

/// Creates a callable transition from an unresolved union-membership proof.
#[doc(hidden)]
#[must_use]
#[track_caller]
pub fn transition_state_with_union_transition_proof<Storage, T, S, Marker, Next>(
    proven: StateWithProof<Storage, T, S, StateUnionTransitionProof<T, S, Marker, Next>>,
    _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
) -> StateUnionProofTransitionCall<Storage, T, S, Marker, Next>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    S: StateUnionErased<Marker>,
    Marker: StateUnionSharedEffect<T, Next>,
    Next: crate::ConcreteStateTrait,
{
    let StateWithProof {
        state,
        proof: _proof,
    } = proven;
    StateUnionProofTransitionCall {
        state,
        callsite: transition_callsite(),
        marker: PhantomData,
    }
}

/// Creates a callable static union transition from a shared-body proof.
#[doc(hidden)]
#[must_use]
#[track_caller]
pub fn transition_state_with_static_union_proof<Storage, T, S, Marker, Next>(
    state: State<Storage, T, S>,
    _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
) -> StateUnionProofTransitionCall<Storage, T, S, Marker, Next>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    S: crate::StateTrait + StateUnionErased<Marker> + crate::UnionTransitionProof<T, Marker, Next>,
    Marker: StateUnionDiscriminant + StateUnionSharedEffect<T, Next>,
    Next: crate::ConcreteStateTrait,
{
    StateUnionProofTransitionCall {
        state,
        callsite: transition_callsite(),
        marker: PhantomData,
    }
}

/// Creates a callable erased transition from a kind-selected proof.
#[doc(hidden)]
#[must_use]
#[track_caller]
pub fn transition_state_with_erased_transition_proof<Storage, T, S, Marker, Next, Proof>(
    proven: StateWithProof<Storage, T, S, Proof>,
    _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
) -> StateUnionProofTransitionCall<Storage, T, S, Marker, Next>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    S: crate::StateTrait,
    Marker: StateUnionDiscriminant,
    Next: crate::ConcreteStateTrait,
{
    let StateWithProof {
        state,
        proof: _proof,
    } = proven;
    StateUnionProofTransitionCall {
        state,
        callsite: transition_callsite(),
        marker: PhantomData,
    }
}

/// Creates a concrete callable transition from a kind-selected proof.
#[doc(hidden)]
#[must_use]
#[track_caller]
pub fn transition_state_with_concrete_kind_proof<Storage, T, S, Marker, Next, Kind>(
    proven: StateWithProof<
        Storage,
        T,
        S,
        crate::TransitionProof<Storage, T, S, Marker, Next, Kind>,
    >,
    _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
) -> ConcreteProofTransitionCall<Storage, T, S, Next>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    S: crate::StateTrait,
    Marker: crate::StateMarker,
    Next: crate::ConcreteStateTrait,
    Kind: StateKind,
{
    let StateWithProof {
        state,
        proof: _proof,
    } = proven;
    ConcreteProofTransitionCall {
        state,
        callsite: transition_callsite(),
        marker: PhantomData,
    }
}

/// Creates a callable transition from a kind-selected proof.
#[doc(hidden)]
#[must_use]
#[track_caller]
pub fn transition_state_with_kind_proof<Storage, T, S, Marker, Next, Kind>(
    proven: StateWithProof<
        Storage,
        T,
        S,
        crate::TransitionProof<Storage, T, S, Marker, Next, Kind>,
    >,
    _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
) -> KindProofTransitionCall<Storage, T, S, Marker, Next, Kind>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    S: crate::StateTrait,
    Marker: crate::StateMarker,
    Next: crate::ConcreteStateTrait,
    Kind: StateKind,
{
    let StateWithProof {
        state,
        proof: _proof,
    } = proven;
    KindProofTransitionCall {
        state,
        callsite: transition_callsite(),
        marker: PhantomData,
    }
}

#[doc(hidden)]
pub fn transition_concrete_after_effect<Storage, T, From, To, Args, Effect>(
    mut state: State<Storage, T, From>,
    args: Args,
    callsite: TransitionCallsite,
) -> State<Storage, T, To>
where
    T: StateMachineImpl,
    Storage: SMut,
    From: crate::StateTrait,
    To: crate::ConcreteStateTrait,
    Effect: TransitionEffect<T, From, To, Args>,
{
    Effect::apply(Storage::s_mut(&mut state.inner), args);
    Storage::complete_transition_after_effect(state, callsite)
}

#[doc(hidden)]
pub fn transition_concrete_after_pinned_effect<Storage, T, From, To, Args, Effect>(
    mut state: State<Storage, T, From>,
    args: Args,
    callsite: TransitionCallsite,
) -> State<Storage, T, To>
where
    T: StateMachineImpl,
    Storage: SPinMut,
    From: crate::StateTrait,
    To: crate::ConcreteStateTrait,
    Effect: PinnedTransitionEffect<T, From, To, Args>,
{
    Effect::apply(Storage::s_pin_mut(&mut state.inner), args);
    Storage::complete_transition_after_effect(state, callsite)
}

/// Creates a callable discriminated-union transition that runs the exact concrete effect.
#[doc(hidden)]
#[must_use]
#[track_caller]
pub fn transition_discriminated_state<Storage, T, Marker, Next>(
    state: DiscriminatedState<Storage, T, Marker>,
    _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
) -> DiscriminatedTransitionCall<Storage, T, Marker, Next>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    Marker: StateUnionDiscriminant,
    Next: crate::StateTrait,
{
    DiscriminatedTransitionCall {
        state,
        callsite: transition_callsite(),
        marker: PhantomData,
    }
}

/// Creates a callable pinned discriminated-union transition that runs the exact concrete effect.
#[doc(hidden)]
#[must_use]
#[track_caller]
pub fn transition_discriminated_state_pinned<Storage, T, Marker, Next>(
    state: DiscriminatedState<Storage, T, Marker>,
    _token: <Storage::Machine<T> as StateMachineImpl>::TransitionToken,
) -> PinnedDiscriminatedTransitionCall<Storage, T, Marker, Next>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    Marker: StateUnionDiscriminant,
    Next: crate::StateTrait,
{
    PinnedDiscriminatedTransitionCall {
        state,
        callsite: transition_callsite(),
        marker: PhantomData,
    }
}

/// Binds a generated union-transition proof selected by the target state.
#[doc(hidden)]
#[must_use]
pub fn proven_state<To, Storage, T, S>(
    state: State<Storage, T, S>,
) -> StateUnionProvenState<Storage, T, S, <To as StateUnionProofTarget<T, S>>::Marker, To>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    S: StateUnionErased<<To as StateUnionProofTarget<T, S>>::Marker>,
    To: StateUnionProofTarget<T, S>,
{
    StateUnionProvenState {
        state,
        marker: PhantomData,
    }
}

/// Binds a generated union-transition proof selected by a union marker.
#[doc(hidden)]
#[must_use]
pub fn proven_union_state<Marker, To, Storage, T, S>(
    state: State<Storage, T, S>,
) -> StateUnionProvenState<Storage, T, S, Marker, To>
where
    T: StateMachineImpl,
    Storage: StateStorage,
    S: StateUnionErased<Marker>,
    Marker: StateUnionSharedEffect<T, To>,
    To: crate::ConcreteStateTrait,
{
    StateUnionProvenState {
        state,
        marker: PhantomData,
    }
}

/// Borrows a state's runtime value through its storage's pin guarantee.
///
/// This is a free function rather than an inherent `State` method so generic
/// state-machine APIs stay focused on receiver types and transitions. Use it
/// when a method accepts `&State<S, T, Current>` and needs to call an API on
/// `T` that requires `Pin<&T>`.
#[must_use]
pub fn pin_ref<Storage, T, S>(state: &State<Storage, T, S>) -> Pin<&T>
where
    T: StateMachineImpl,
    Storage: SPinRef,
{
    Storage::s_pin_ref(&state.inner)
}

/// Mutably borrows a state's runtime value through its storage's pin guarantee.
///
/// Most pinned transitions should use `pinned transition` plus
/// [`transition!(pin state, ...)`](macro@crate::transition) instead of calling
/// this directly. It is provided for implementation methods that need to call
/// additional pinned APIs before deciding whether to transition.
pub fn pin_mut<Storage, T, S>(state: &mut State<Storage, T, S>) -> Pin<&mut T>
where
    T: StateMachineImpl,
    Storage: SPinMut,
{
    Storage::s_pin_mut(&mut state.inner)
}

impl<Storage, T, S> State<Storage, T, S>
where
    T: StateMachineImpl,
    Storage: StateStorage,
{
    pub(crate) fn from_inner(inner: Storage::Inner<T, S>) -> Self {
        Self {
            inner,
            marker: PhantomData,
        }
    }

    /// Binds a generated union-transition proof to this state.
    #[doc(hidden)]
    #[must_use]
    pub fn with<Marker, To, Kind>(
        self,
        proof: crate::TransitionProof<Storage, T, S, Marker, To, Kind>,
    ) -> StateWithProof<Storage, T, S, crate::TransitionProof<Storage, T, S, Marker, To, Kind>>
    where
        S: crate::StateTrait,
        Marker: crate::StateMarker,
        To: crate::ConcreteStateTrait,
        Kind: crate::StateKind,
    {
        StateWithProof { state: self, proof }
    }
}

impl<T, S> State<StorageStateOwned, T, S>
where
    T: StateMachineImpl,
{
    /// Wraps an implementation in an initial directly owned state.
    ///
    /// This is the normal entry point for a state machine. It only compiles
    /// when the definition crate declared `S` as an initial state for
    /// `T::Standin`.
    ///
    /// ```ignore
    /// use magicstatemachines::{SOwned, State};
    /// use test_def::states::Disconnected;
    ///
    /// let disconnected: State<SOwned, Connection, Disconnected> =
    ///     State::new(Connection::new("localhost:8080"));
    /// ```
    ///
    /// To move the state into another owned container later, create that
    /// container from the returned state token instead of constructing raw
    /// `T` again.
    #[must_use]
    pub fn new(value: T) -> Self
    where
        T::Standin: Initial<S>,
    {
        State {
            inner: <StorageStateOwned as StateStorageNew>::new(value),
            marker: PhantomData,
        }
    }
}

impl<T, S> State<StorageStateOwnedBox, T, S>
where
    T: StateMachineImpl,
{
    /// Moves a directly owned state into `Box` storage without changing its state.
    ///
    /// This mirrors `Box::new`, but it takes `State<SOwned, T, S>` instead of
    /// raw `T` so the current state is preserved:
    ///
    /// ```ignore
    /// let owned: State<SOwned, Connection, Connected> = connection.connect();
    /// let boxed: SBox<Connection, Connected> = SBox::new(owned);
    /// ```
    #[must_use]
    pub fn new(state: State<StorageStateOwned, T, S>) -> Self {
        State {
            inner: crate::StateOwned {
                value: Box::new(state.inner.value),
                state: PhantomData,
                #[cfg(feature = "tracing")]
                trace: state.inner.trace,
            },
            marker: PhantomData,
        }
    }

    /// Moves this boxed state back into direct owned storage.
    ///
    /// This consumes the box and returns `State<SOwned, T, S>`. The state
    /// marker is unchanged, so methods available before boxing remain
    /// available after unboxing.
    ///
    /// ```ignore
    /// let boxed: SBox<Connection, Connected> = SBox::new(owned);
    /// let owned_again: State<SOwned, Connection, Connected> = SBox::unbox(boxed);
    /// ```
    #[must_use]
    pub fn unbox(state: Self) -> State<StorageStateOwned, T, S> {
        State {
            inner: crate::StateOwned {
                value: *state.inner.value,
                state: PhantomData,
                #[cfg(feature = "tracing")]
                trace: state.inner.trace,
            },
            marker: PhantomData,
        }
    }
}

impl<T, S> State<StorageStateOwnedPinBox, T, S>
where
    T: StateMachineImpl,
{
    /// Pins an already boxed state in place without changing its state.
    ///
    /// Pinning must happen to the boxed allocation. For that reason this
    /// constructor consumes [`SBox`] rather than `SOwned`:
    ///
    /// ```ignore
    /// let boxed: SBox<Connection, Connected> = SBox::new(owned);
    /// let pinned: SPinBox<Connection, Connected> = SPinBox::new(boxed);
    /// ```
    #[must_use]
    pub fn new(state: State<StorageStateOwnedBox, T, S>) -> Self {
        State {
            inner: crate::StateOwned {
                value: Box::into_pin(state.inner.value),
                state: PhantomData,
                #[cfg(feature = "tracing")]
                trace: state.inner.trace,
            },
            marker: PhantomData,
        }
    }

    /// Converts pinned box storage back to box storage when the runtime is `Unpin`.
    ///
    /// This is only available for `T: Unpin`; otherwise the pinning guarantee
    /// must be preserved.
    ///
    /// ```compile_fail
    /// use core::marker::PhantomPinned;
    /// use magicstatemachines::{
    ///     Initial, SBox, SOwned, SPinBox, State, StateMachineImpl, States,
    /// };
    ///
    /// struct Machine;
    /// struct Runtime {
    ///     _pin: PhantomPinned,
    /// }
    /// struct Token;
    ///
    /// States! {
    ///     Ready;
    /// }
    ///
    /// impl Initial<Ready> for Machine {}
    ///
    /// impl StateMachineImpl for Runtime {
    ///     type Standin = Machine;
    ///     type Impl = Self;
    ///     type TransitionToken = Token;
    /// }
    ///
    /// let ready = State::<SOwned, _, Ready>::new(Runtime {
    ///     _pin: PhantomPinned,
    /// });
    /// let pinned: SPinBox<Runtime, Ready> = SPinBox::new(SBox::new(ready));
    ///
    /// // `Runtime` is `!Unpin`, so the pinned allocation cannot be turned
    /// // back into an ordinary box.
    /// let _boxed = SPinBox::into_boxed(pinned);
    /// ```
    #[must_use]
    pub fn into_boxed(state: Self) -> State<StorageStateOwnedBox, T, S>
    where
        T: Unpin,
    {
        State {
            inner: crate::StateOwned {
                value: Pin::into_inner(state.inner.value),
                state: PhantomData,
                #[cfg(feature = "tracing")]
                trace: state.inner.trace,
            },
            marker: PhantomData,
        }
    }
}

#[cfg(feature = "unique-rc-arc")]
impl<T, S> State<StorageStateOwnedUniqueRc, T, S>
where
    T: StateMachineImpl,
{
    /// Moves a directly owned state into `UniqueRc` storage without changing its state.
    ///
    /// This API is available only with the `unique-rc-arc` feature because
    /// `UniqueRc` itself is nightly-only.
    #[must_use]
    pub fn new(state: State<StorageStateOwned, T, S>) -> Self {
        State {
            inner: crate::StateOwned {
                value: UniqueRc::new(state.inner.value),
                state: PhantomData,
                #[cfg(feature = "tracing")]
                trace: state.inner.trace,
            },
            marker: PhantomData,
        }
    }
}

#[cfg(feature = "unique-rc-arc")]
impl<T, S> State<StorageStateOwnedUniqueArc, T, S>
where
    T: StateMachineImpl,
{
    /// Moves a directly owned state into `UniqueArc` storage without changing its state.
    ///
    /// This API is available only with the `unique-rc-arc` feature because
    /// `UniqueArc` itself is nightly-only.
    #[must_use]
    pub fn new(state: State<StorageStateOwned, T, S>) -> Self {
        State {
            inner: crate::StateOwned {
                value: UniqueArc::new(state.inner.value),
                state: PhantomData,
                #[cfg(feature = "tracing")]
                trace: state.inner.trace,
            },
            marker: PhantomData,
        }
    }
}

impl<Storage, T, S> Deref for State<Storage, T, S>
where
    T: StateMachineImpl,
    Storage: SRef,
{
    type Target = T;

    fn deref(&self) -> &Self::Target {
        Storage::s_ref(&self.inner)
    }
}

impl<Storage, T, S> DerefMut for State<Storage, T, S>
where
    T: StateMachineImpl,
    Storage: SMut,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        Storage::s_mut(&mut self.inner)
    }
}