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
use super::{SharedStateError, SharedStorage, SharedValue, WrongStateError};
use crate::{
    InnerInference, SMut, SRef, State, StateMachineImpl, StateStorage, StateTrait,
    StateUnionRuntime, StateUnionState, Transition, TransitionCallsite,
    state_trait::{self, ErasedState},
};
use core::marker::PhantomData;
use core::ops::{Deref, DerefMut};

/// Immutable shared-state read guard used as [`State`] storage.
///
/// This is the inner value for [`StorageStateRef`]. Public shared immutable
/// borrows return `State<StorageStateRef<...>, T, S>`, not this type directly.
/// The storage implements [`SRef`] but deliberately does not implement
/// [`SMut`], so read-only arbitrary-self methods can run while generated
/// transition calls cannot complete.
///
/// ```ignore
/// let connected = shared.borrow::<Connected>()?;
/// assert_eq!(connected.endpoint(), "localhost:8080"); // `self: &State<impl SRef, ...>`
///
/// // Union borrows work too; the runtime marker is checked against the union.
/// let online = shared.borrow::<Online>()?;
/// ```
pub struct StateRef<G, T, S> {
    guard: G,
    marker: PhantomData<fn() -> (T, S)>,
}

impl<G, T, S> StateRef<G, T, S>
where
    G: Deref<Target = SharedValue<T>>,
    S: SharedBorrowState,
{
    pub(super) fn from_guard<StorageError>(
        guard: G,
    ) -> Result<Self, SharedStateError<StorageError>> {
        S::ensure_state(&guard.state)?;
        Ok(Self {
            guard,
            marker: PhantomData,
        })
    }
}

impl<G, T, S> Deref for StateRef<G, T, S>
where
    G: Deref<Target = SharedValue<T>>,
{
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.guard.value
    }
}

/// Generic [`State`] backend for an immutable shared-state guard.
///
/// This storage can expose `&T` through [`SRef`], but it cannot expose `&mut T`
/// and therefore does not implement [`SMut`]. A `State<StorageStateRef<...>>`
/// is useful for read-only methods such as
/// `fn endpoint(self: &State<impl SRef, Self, impl InOnline>) -> &str`, but it
/// cannot be transitioned by the generated [`transition!`](macro@crate::transition)
/// macro.
pub struct StorageStateRef<'a, Backend>(PhantomData<&'a Backend>);

impl<'a, Backend> StateStorage for StorageStateRef<'a, Backend>
where
    Backend: SharedStorage + 'a,
{
    type Inference = InnerInference;

    type Inner<T, S>
        = StateRef<Backend::ReadGuard<'a, T>, T, S>
    where
        T: StateMachineImpl;
    type Machine<T>
        = T
    where
        T: StateMachineImpl;

    fn retag<T, From, To>(inner: Self::Inner<T, From>) -> Self::Inner<T, To>
    where
        T: StateMachineImpl,
    {
        StateRef {
            guard: inner.guard,
            marker: PhantomData,
        }
    }

    fn complete_transition<T, From, To, Args>(
        _state: State<Self, T, From>,
        _args: Args,
        _callsite: TransitionCallsite,
    ) -> State<Self, T, To>
    where
        T: StateMachineImpl,
        From: StateTrait,
        To: crate::ConcreteStateTrait,
        T::Standin: Transition<From, To>,
        <T::Standin as Transition<From, To>>::F: crate::TransitionSignature<Args>,
    {
        panic!("immutable shared-state views cannot transition")
    }

    fn complete_transition_after_effect<T, From, To>(
        _state: State<Self, T, From>,
        _callsite: TransitionCallsite,
    ) -> State<Self, T, To>
    where
        T: StateMachineImpl,
        From: StateTrait,
        To: crate::ConcreteStateTrait,
    {
        panic!("immutable shared-state views cannot transition")
    }

    fn inferred_state<T, S>(inner: &Self::Inner<T, S>) -> ErasedState
    where
        T: StateMachineImpl,
        S: StateTrait,
    {
        state_trait::clone_erased(&inner.guard.state)
    }
}

impl<'a, Backend> SRef for StorageStateRef<'a, Backend>
where
    Backend: SharedStorage + 'a,
{
    fn s_ref<T, S>(inner: &Self::Inner<T, S>) -> &T
    where
        T: StateMachineImpl,
    {
        inner
    }
}

/// Mutable typed view into shared state.
///
/// Transitions performed through this guard update an internal pending state.
/// When the guard is dropped, that pending state is committed back to the
/// shared container.
///
/// The returned guard is used through the generic [`State`] alias
/// [`SMutView`](crate::SMutView), so implementation methods can keep the same
/// `State<S, Self, Current>` receiver shape they use for owned values:
///
/// ```ignore
/// {
///     let connected = shared.borrow_mut::<Connected>()?;
///     let authenticated = magicstatemachines::transition!(
///         connected,
///         "alice".to_owned(),
///     );
///     drop(authenticated); // shared state is now `Authenticated`
/// }
/// ```
///
/// The commit happens when the final guard state is dropped. Constructing a
/// transition call object is not a commit by itself.
pub struct StateMut<G, T, S>
where
    G: DerefMut<Target = SharedValue<T>>,
{
    guard: Option<G>,
    pending: ErasedState,
    marker: PhantomData<fn() -> (T, S)>,
}

/// Generic [`State`] backend for a mutable shared-state guard.
pub struct StorageStateMut<'a, Backend>(PhantomData<&'a Backend>);

impl<'a, Backend> StateStorage for StorageStateMut<'a, Backend>
where
    Backend: SharedStorage + 'a,
{
    type Inference = InnerInference;

    type Inner<T, S>
        = StateMut<Backend::WriteGuard<'a, T>, T, S>
    where
        T: StateMachineImpl;
    type Machine<T>
        = T
    where
        T: StateMachineImpl;

    fn retag<T, From, To>(mut inner: Self::Inner<T, From>) -> Self::Inner<T, To>
    where
        T: StateMachineImpl,
    {
        StateMut {
            guard: inner.guard.take(),
            pending: state_trait::clone_erased(&inner.pending),
            marker: PhantomData,
        }
    }

    fn complete_transition<T, From, To, Args>(
        mut state: State<Self, T, From>,
        _args: Args,
        _callsite: TransitionCallsite,
    ) -> State<Self, T, To>
    where
        T: StateMachineImpl,
        From: StateTrait,
        To: crate::ConcreteStateTrait,
        T::Standin: Transition<From, To>,
        <T::Standin as Transition<From, To>>::F: crate::TransitionSignature<Args>,
    {
        State {
            inner: StateMut {
                guard: state.inner.guard.take(),
                pending: state_trait::erased_state::<To>(),
                marker: PhantomData,
            },
            marker: PhantomData,
        }
    }

    fn complete_transition_after_effect<T, From, To>(
        mut state: State<Self, T, From>,
        _callsite: TransitionCallsite,
    ) -> State<Self, T, To>
    where
        T: StateMachineImpl,
        From: StateTrait,
        To: crate::ConcreteStateTrait,
    {
        State {
            inner: StateMut {
                guard: state.inner.guard.take(),
                pending: state_trait::erased_state::<To>(),
                marker: PhantomData,
            },
            marker: PhantomData,
        }
    }

    fn inferred_state<T, S>(inner: &Self::Inner<T, S>) -> ErasedState
    where
        T: StateMachineImpl,
        S: StateTrait,
    {
        state_trait::clone_erased(&inner.pending)
    }
}

impl<'a, Backend> SRef for StorageStateMut<'a, Backend>
where
    Backend: SharedStorage + 'a,
{
    fn s_ref<T, S>(inner: &Self::Inner<T, S>) -> &T
    where
        T: StateMachineImpl,
    {
        inner
    }
}

impl<'a, Backend> SMut for StorageStateMut<'a, Backend>
where
    Backend: SharedStorage + 'a,
{
    fn s_mut<T, S>(inner: &mut Self::Inner<T, S>) -> &mut T
    where
        T: StateMachineImpl,
    {
        inner
    }
}

impl<G, T, S> StateMut<G, T, S>
where
    G: DerefMut<Target = SharedValue<T>>,
    S: SharedBorrowState,
{
    pub(super) fn from_guard<StorageError>(
        guard: G,
    ) -> Result<Self, SharedStateError<StorageError>> {
        S::ensure_state(&guard.state)?;
        let pending = S::initial_pending(&guard.state);
        Ok(Self {
            guard: Some(guard),
            pending,
            marker: PhantomData,
        })
    }
}

/// Creates a callable transition for a mutable shared-state guard.
///
/// This is the guarded-state counterpart to [`crate::transition()`].
#[must_use]
pub fn transition_mut<G, T, S, Next>(
    state: StateMut<G, T, S>,
    _token: T::TransitionToken,
) -> StateMutTransitionCall<G, T, S, Next>
where
    G: DerefMut<Target = SharedValue<T>>,
    T: StateMachineImpl,
    T::Standin: Transition<S, Next>,
{
    StateMutTransitionCall {
        state,
        to: PhantomData,
    }
}

impl<G, T, S> Deref for StateMut<G, T, S>
where
    G: DerefMut<Target = SharedValue<T>>,
{
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.guard.as_ref().expect("guard is present").value
    }
}

impl<G, T, S> DerefMut for StateMut<G, T, S>
where
    G: DerefMut<Target = SharedValue<T>>,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.guard.as_mut().expect("guard is present").value
    }
}

impl<G, T, S> Drop for StateMut<G, T, S>
where
    G: DerefMut<Target = SharedValue<T>>,
{
    fn drop(&mut self) {
        if let Some(guard) = self.guard.as_mut() {
            guard.state = state_trait::clone_erased(&self.pending);
        }
    }
}

/// Low-level transition call object for [`StateMut`].
#[doc(hidden)]
pub struct StateMutTransitionCall<G, T, From, To>
where
    G: DerefMut<Target = SharedValue<T>>,
{
    state: StateMut<G, T, From>,
    to: PhantomData<fn() -> To>,
}

impl<G, T, From, To> StateMutTransitionCall<G, T, From, To>
where
    G: DerefMut<Target = SharedValue<T>>,
{
    pub fn call<Args>(mut self, _args: Args) -> StateMut<G, T, To>
    where
        T: StateMachineImpl,
        T::Standin: Transition<From, To>,
        <T::Standin as Transition<From, To>>::F: crate::TransitionSignature<Args>,
        To: crate::ConcreteStateTrait,
    {
        StateMut {
            guard: self.state.guard.take(),
            pending: state_trait::erased_state::<To>(),
            marker: PhantomData,
        }
    }
}

/// State marker that can validate a shared-storage runtime marker.
#[doc(hidden)]
pub trait SharedBorrowState: StateTrait {
    fn ensure_state(actual: &ErasedState) -> Result<(), WrongStateError>;
    fn initial_pending(actual: &ErasedState) -> ErasedState;
}

pub auto trait ExactSharedBorrowState {}

impl<Marker> !ExactSharedBorrowState for StateUnionState<Marker> {}

impl<S> SharedBorrowState for S
where
    S: crate::ConcreteStateTrait + ExactSharedBorrowState,
{
    fn ensure_state(actual: &ErasedState) -> Result<(), WrongStateError> {
        if state_trait::is_state::<S>(actual) {
            Ok(())
        } else {
            Err(WrongStateError {
                expected: core::any::type_name::<S>(),
                actual: actual.type_name(),
            })
        }
    }

    fn initial_pending(_actual: &ErasedState) -> ErasedState {
        state_trait::erased_state::<S>()
    }
}

impl<Marker> SharedBorrowState for StateUnionState<Marker>
where
    Marker: StateUnionRuntime + 'static,
    StateUnionState<Marker>: StateTrait,
{
    fn ensure_state(actual: &ErasedState) -> Result<(), WrongStateError> {
        if Marker::contains(&**actual) {
            Ok(())
        } else {
            Err(WrongStateError {
                expected: Marker::expected_type_name(),
                actual: actual.type_name(),
            })
        }
    }

    fn initial_pending(actual: &ErasedState) -> ErasedState {
        state_trait::clone_erased(actual)
    }
}