safer_ring/operation/states.rs
1//! Operation state types and state machine definitions.
2//!
3//! This module defines the state types used in the Operation state machine
4//! and the sealed OperationState trait that ensures type safety.
5
6/// Marker trait for operation states.
7///
8/// This trait is sealed to prevent external implementations and ensure
9/// that only valid states can be used with operations.
10pub trait OperationState: private::Sealed {}
11
12/// Operation is being built and can be configured.
13///
14/// In this state, the operation can have its parameters modified but cannot
15/// be submitted to the kernel. This prevents incomplete operations from being
16/// submitted accidentally.
17#[derive(Debug, Clone, Copy)]
18pub struct Building;
19
20/// Operation has been submitted and is in flight.
21///
22/// In this state, the operation cannot be modified but can be polled for
23/// completion. The operation ID is used to track the operation in the kernel.
24#[derive(Debug, Clone, Copy)]
25pub struct Submitted {
26 /// Operation ID for tracking in the completion queue
27 /// This ID is assigned by the ring when the operation is submitted
28 pub(crate) id: u64,
29}
30
31/// Operation has completed with a result.
32///
33/// In this state, the operation result can be extracted along with the
34/// buffer ownership. This is the terminal state for operations.
35#[derive(Debug)]
36pub struct Completed<T> {
37 /// The result of the operation
38 pub(crate) result: T,
39}
40
41// Implement the sealed trait for all valid states
42impl OperationState for Building {}
43impl OperationState for Submitted {}
44impl<T> OperationState for Completed<T> {}
45
46/// Private module to seal the OperationState trait.
47///
48/// This prevents external crates from implementing OperationState,
49/// ensuring type safety of the state machine.
50mod private {
51 pub trait Sealed {}
52 impl Sealed for super::Building {}
53 impl Sealed for super::Submitted {}
54 impl<T> Sealed for super::Completed<T> {}
55}