Skip to main content

qubit_state_machine/
transition.rs

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