qubit_state_machine/standard/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 { source, event, target }
39 }
40
41 /// Returns the source state of this transition.
42 ///
43 /// # Returns
44 /// The state that must be current before this transition can be applied.
45 pub const fn source(&self) -> S {
46 self.source
47 }
48
49 /// Returns the event that triggers this transition.
50 ///
51 /// # Returns
52 /// The event associated with this transition.
53 pub const fn event(&self) -> E {
54 self.event
55 }
56
57 /// Returns the target state of this transition.
58 ///
59 /// # Returns
60 /// The state after this transition succeeds.
61 pub const fn target(&self) -> S {
62 self.target
63 }
64}