Skip to main content

qubit_state_machine/
transition.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! Immutable transition value.
10
11/// A directed transition in a finite state machine.
12///
13/// `S` is the state type and `E` is the event type. In normal use both are
14/// small enum-like values that implement `Copy`, `Eq`, and `Hash`.
15#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
16pub struct Transition<S, E> {
17    source: S,
18    event: E,
19    target: S,
20}
21
22impl<S, E> Transition<S, E>
23where
24    S: Copy,
25    E: Copy,
26{
27    /// Creates a transition from `source` to `target` triggered by `event`.
28    ///
29    /// # Parameters
30    /// - `source`: State before the event is applied.
31    /// - `event`: Event that triggers this transition.
32    /// - `target`: State after the transition succeeds.
33    ///
34    /// # Returns
35    /// A new immutable transition value.
36    pub const fn new(source: S, event: E, target: S) -> Self {
37        Self {
38            source,
39            event,
40            target,
41        }
42    }
43
44    /// Returns the source state of this transition.
45    ///
46    /// # Returns
47    /// The state that must be current before this transition can be applied.
48    pub const fn source(&self) -> S {
49        self.source
50    }
51
52    /// Returns the event that triggers this transition.
53    ///
54    /// # Returns
55    /// The event associated with this transition.
56    pub const fn event(&self) -> E {
57        self.event
58    }
59
60    /// Returns the target state of this transition.
61    ///
62    /// # Returns
63    /// The state after this transition succeeds.
64    pub const fn target(&self) -> S {
65        self.target
66    }
67}