newton-machine 0.2.0

Newtonian state machines: typed Harel configurations, an Elm update loop, and inertial history as a sidecar.
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
//! Commands as data. The host executes them; `update` never performs I/O.
//!
//! GoF Command without objects or closures: an ordered bag of atoms `C`.
//! Concatenation is [`Cmd::and`]. Up to [`INLINE_CAP`] atoms live on the
//! stack so `perform` (exit + enter) never needs a heap. More than that
//! spills to `Vec` when `alloc` is on, and **panics** when it is not —
//! silent drop would violate the conservation law.
//!
// rustbrain: [[docs/concepts/command-as-data]]
// rustbrain: [[docs/adr/0004-commands-as-data]]
// rustbrain: [[docs/adr/0018-cmd-inline-then-heap]]
// rustbrain: [[docs/goals/effects-never-leave-the-host]]

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

/// Stack capacity before [`Cmd::and`] needs a heap (`alloc`) or panics.
///
/// Two is the common `perform` case (exit cmd + enter cmd). Four covers a
/// nested enter path without `Vec`.
pub const INLINE_CAP: usize = 4;

/// `Cmd::and` needed more atoms than [`INLINE_CAP`] and `alloc` is off.
///
/// Programmer error at the call site (too many effects in one step on a
/// no-heap build). [`Cmd::and`] panics with this message. [`Cmd::try_and`]
/// returns it so serde / bulk builders can fail without aborting.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CmdOverflow {
    /// Stack cap ([`INLINE_CAP`]).
    pub cap: usize,
    /// Atoms that would have been in the result.
    pub attempted: usize,
}

impl core::fmt::Display for CmdOverflow {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(
            f,
            "Cmd::and needs {} atoms; stack cap is {} (enable feature `alloc` for a heap batch, or emit fewer effects per step)",
            self.attempted, self.cap
        )
    }
}

#[cfg(feature = "std")]
impl std::error::Error for CmdOverflow {}

#[derive(Clone)]
enum Inner<C> {
    Inline {
        slots: [Option<C>; INLINE_CAP],
        len: u8,
    },
    #[cfg(feature = "alloc")]
    Heap(Vec<C>),
}

/// A description of work the host should perform after a run-to-completion step.
///
/// `C` is the author's command vocabulary (`Submit`, `Persist`, `Alert`, …).
/// Nothing in this type can open a socket. Representation (stack vs heap) is
/// private: compare and iterate atoms, do not match variants.
#[must_use]
#[derive(Clone)]
pub struct Cmd<C> {
    inner: Inner<C>,
}

impl<C> Cmd<C> {
    /// No effect.
    #[inline]
    pub const fn none() -> Self {
        Self {
            inner: Inner::Inline {
                slots: [None, None, None, None],
                len: 0,
            },
        }
    }

    /// One command.
    #[inline]
    pub const fn single(cmd: C) -> Self {
        Self {
            inner: Inner::Inline {
                slots: [Some(cmd), None, None, None],
                len: 1,
            },
        }
    }

    /// True when this command describes no work.
    #[inline]
    pub fn is_none(&self) -> bool {
        self.is_empty()
    }

    /// How many atoms will the host execute.
    pub fn len(&self) -> usize {
        match &self.inner {
            Inner::Inline { len, .. } => *len as usize,
            #[cfg(feature = "alloc")]
            Inner::Heap(v) => v.len(),
        }
    }

    /// True when [`Cmd::len`] is 0.
    #[inline]
    pub fn is_empty(&self) -> bool {
        match &self.inner {
            Inner::Inline { len, .. } => *len == 0,
            #[cfg(feature = "alloc")]
            Inner::Heap(v) => v.is_empty(),
        }
    }

    /// Iterate atoms in document order.
    pub fn iter(&self) -> Iter<'_, C> {
        match &self.inner {
            Inner::Inline { slots, len } => Iter {
                kind: IterKind::Inline(slots[..*len as usize].iter()),
            },
            #[cfg(feature = "alloc")]
            Inner::Heap(v) => Iter {
                kind: IterKind::Heap(v.iter()),
            },
        }
    }

    /// Concatenate in document order. [`Cmd::none`] is the identity.
    ///
    /// Panics if the result would exceed [`INLINE_CAP`] and the `alloc`
    /// feature is off. That is a chart/host bug, not a dropped intent.
    pub fn and(self, other: Self) -> Self {
        match self.try_and(other) {
            Ok(cmd) => cmd,
            Err(e) => panic!("{e}"),
        }
    }

    /// Fallible [`Cmd::and`]. Does not panic.
    pub fn try_and(self, other: Self) -> Result<Self, CmdOverflow> {
        if self.is_empty() {
            return Ok(other);
        }
        if other.is_empty() {
            return Ok(self);
        }
        let attempted = self.len() + other.len();
        if attempted <= INLINE_CAP {
            let mut slots: [Option<C>; INLINE_CAP] = [None, None, None, None];
            let mut i = 0usize;
            for a in self {
                slots[i] = Some(a);
                i += 1;
            }
            for a in other {
                slots[i] = Some(a);
                i += 1;
            }
            return Ok(Self {
                inner: Inner::Inline {
                    slots,
                    len: i as u8,
                },
            });
        }
        #[cfg(feature = "alloc")]
        {
            let mut v = Vec::with_capacity(attempted);
            v.extend(self);
            v.extend(other);
            Ok(Self {
                inner: Inner::Heap(v),
            })
        }
        #[cfg(not(feature = "alloc"))]
        {
            Err(CmdOverflow {
                cap: INLINE_CAP,
                attempted,
            })
        }
    }
}

impl<C> Default for Cmd<C> {
    fn default() -> Self {
        Self::none()
    }
}

impl<C> From<C> for Cmd<C> {
    fn from(cmd: C) -> Self {
        Self::single(cmd)
    }
}

impl<C: PartialEq> PartialEq for Cmd<C> {
    fn eq(&self, other: &Self) -> bool {
        self.len() == other.len() && self.iter().eq(other.iter())
    }
}

impl<C: Eq> Eq for Cmd<C> {}

impl<C: core::fmt::Debug> core::fmt::Debug for Cmd<C> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_list().entries(self.iter()).finish()
    }
}

enum IterKind<'a, C> {
    Inline(core::slice::Iter<'a, Option<C>>),
    #[cfg(feature = "alloc")]
    Heap(core::slice::Iter<'a, C>),
}

/// Borrowed iterator over [`Cmd`] atoms.
pub struct Iter<'a, C> {
    kind: IterKind<'a, C>,
}

impl<'a, C> Iterator for Iter<'a, C> {
    type Item = &'a C;

    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.kind {
            IterKind::Inline(it) => it.next().and_then(|s| s.as_ref()),
            #[cfg(feature = "alloc")]
            IterKind::Heap(it) => it.next(),
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let n = self.len();
        (n, Some(n))
    }
}

impl<C> ExactSizeIterator for Iter<'_, C> {
    fn len(&self) -> usize {
        match &self.kind {
            IterKind::Inline(it) => it.len(),
            #[cfg(feature = "alloc")]
            IterKind::Heap(it) => it.len(),
        }
    }
}

enum IntoKind<C> {
    Inline {
        slots: [Option<C>; INLINE_CAP],
        i: u8,
        len: u8,
    },
    #[cfg(feature = "alloc")]
    Heap(alloc::vec::IntoIter<C>),
}

/// Owned iterator over [`Cmd`] atoms.
pub struct IntoIter<C> {
    kind: IntoKind<C>,
}

impl<C> Iterator for IntoIter<C> {
    type Item = C;

    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.kind {
            IntoKind::Inline { slots, i, len } => {
                if *i >= *len {
                    return None;
                }
                let item = slots[*i as usize].take();
                *i += 1;
                item
            }
            #[cfg(feature = "alloc")]
            IntoKind::Heap(it) => it.next(),
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let n = match &self.kind {
            IntoKind::Inline { i, len, .. } => (*len).saturating_sub(*i) as usize,
            #[cfg(feature = "alloc")]
            IntoKind::Heap(it) => it.len(),
        };
        (n, Some(n))
    }
}

impl<C> ExactSizeIterator for IntoIter<C> {}

impl<C> IntoIterator for Cmd<C> {
    type Item = C;
    type IntoIter = IntoIter<C>;

    fn into_iter(self) -> Self::IntoIter {
        match self.inner {
            Inner::Inline { slots, len } => IntoIter {
                kind: IntoKind::Inline { slots, i: 0, len },
            },
            #[cfg(feature = "alloc")]
            Inner::Heap(v) => IntoIter {
                kind: IntoKind::Heap(v.into_iter()),
            },
        }
    }
}

#[cfg(feature = "serde")]
mod serde_impl {
    use super::Cmd;
    use serde::de::{SeqAccess, Visitor};
    use serde::ser::SerializeSeq;
    use serde::{Deserialize, Deserializer, Serialize, Serializer};

    impl<C: Serialize> Serialize for Cmd<C> {
        fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
            let mut seq = serializer.serialize_seq(Some(self.len()))?;
            for a in self.iter() {
                seq.serialize_element(a)?;
            }
            seq.end()
        }
    }

    impl<'de, C: Deserialize<'de>> Deserialize<'de> for Cmd<C> {
        fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
            struct CmdVisitor<C>(core::marker::PhantomData<C>);

            impl<'de, C: Deserialize<'de>> Visitor<'de> for CmdVisitor<C> {
                type Value = Cmd<C>;

                fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                    f.write_str("a sequence of command atoms")
                }

                fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Cmd<C>, A::Error> {
                    let mut cmd = Cmd::none();
                    while let Some(atom) = seq.next_element()? {
                        cmd = cmd
                            .try_and(Cmd::single(atom))
                            .map_err(serde::de::Error::custom)?;
                    }
                    Ok(cmd)
                }
            }

            deserializer.deserialize_seq(CmdVisitor(core::marker::PhantomData))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{Cmd, INLINE_CAP};

    #[test]
    fn none_is_identity() {
        let a = Cmd::single(1u8);
        assert_eq!(Cmd::none().and(a.clone()), a);
        assert_eq!(a.clone().and(Cmd::none()), a);
    }

    #[test]
    fn and_fits_on_stack() {
        let cmd = Cmd::single(1).and(Cmd::single(2)).and(Cmd::single(3));
        assert_eq!(cmd.len(), 3);
        let mut it = cmd.iter();
        assert_eq!(it.next(), Some(&1));
        assert_eq!(it.next(), Some(&2));
        assert_eq!(it.next(), Some(&3));
        assert_eq!(it.next(), None);
    }

    #[test]
    fn four_fit() {
        let mut cmd = Cmd::none();
        for i in 0..INLINE_CAP {
            cmd = cmd.and(Cmd::single(i as u8));
        }
        assert_eq!(cmd.len(), INLINE_CAP);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn five_spills_to_heap() {
        let mut cmd = Cmd::none();
        for i in 0..=INLINE_CAP {
            cmd = cmd.and(Cmd::single(i as u8));
        }
        assert_eq!(cmd.len(), INLINE_CAP + 1);
        assert_eq!(cmd.iter().copied().count(), INLINE_CAP + 1);
    }

    #[cfg(not(feature = "alloc"))]
    #[test]
    #[should_panic(expected = "stack cap")]
    fn five_panics_without_heap() {
        let mut cmd = Cmd::none();
        for i in 0..=INLINE_CAP {
            cmd = cmd.and(Cmd::single(i as u8));
        }
    }
}