Skip to main content

tpt_torus_core/
flow.rs

1use crate::operation::Operation;
2
3/// A submission to the Virtual Torus — the user-space equivalent of an io_uring SQE.
4///
5/// `Flow` wraps an I/O [`Operation`] along with user-provided data (`user_data`)
6/// that is returned verbatim in the corresponding [`Result`](crate::Result) when
7/// the operation completes. Applications submit `Flow`s and later reap the
8/// matching [`Result`](crate::Result)s, correlating them via `user_data`.
9///
10/// # Example
11///
12/// ```
13/// use tpt_torus_core::flow::Flow;
14/// use tpt_torus_core::operation::Operation;
15///
16/// let flow = Flow::new(Operation::Read {
17///     fd: 0,
18///     buf: std::ptr::null_mut(),
19///     len: 0,
20///     offset: 0,
21/// });
22/// assert_eq!(flow.user_data(), 0);
23/// ```
24pub struct Flow {
25    /// The I/O operation this flow submits to the backend.
26    pub(crate) op: Operation,
27    /// Opaque caller data echoed back on completion (via [`Result::user_data`](crate::Result::user_data)).
28    pub(crate) user_data: u64,
29}
30
31impl Flow {
32    /// Create a new `Flow` with the given operation and zero user data.
33    pub fn new(op: Operation) -> Self {
34        Self { op, user_data: 0 }
35    }
36
37    /// Create a new `Flow` with user-provided data returned on completion.
38    ///
39    /// The `user_data` value is opaque to the framework; it is stored with the
40    /// flow and handed back unchanged in the corresponding
41    /// [`Result`](crate::Result) so callers can correlate submissions with
42    /// completions (e.g. as a request id or a pointer to a context struct).
43    pub fn with_user_data(op: Operation, user_data: u64) -> Self {
44        Self { op, user_data }
45    }
46
47    /// Attach arbitrary user data to this flow.
48    ///
49    /// The value is returned unchanged in the corresponding [`Result`](crate::Result).
50    ///
51    /// Returns `&mut Self` for method chaining.
52    pub fn set_user_data(&mut self, data: u64) -> &mut Self {
53        self.user_data = data;
54        self
55    }
56
57    /// Get the user data associated with this flow.
58    pub fn user_data(&self) -> u64 {
59        self.user_data
60    }
61
62    /// Access the inner operation.
63    pub fn operation(&self) -> &Operation {
64        &self.op
65    }
66
67    /// Consume the flow, returning the inner operation.
68    pub fn into_operation(self) -> Operation {
69        self.op
70    }
71}