Skip to main content

bornera_core/write/
state.rs

1//! Immutable admission, slice, progress, and discard observations.
2
3use calandria::RetainedBytes;
4
5use crate::{ConnectionEpoch, Delivery, EffectId, FrameMeasure, OperationId};
6
7use super::WriteQueue;
8
9/// Whether one progress report crossed the conservative delivery boundary.
10#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11pub(crate) enum WriteBoundary {
12    /// The operation had already crossed or no byte progressed.
13    Unchanged,
14    /// The first positive byte progress occurred in this report.
15    Crossed,
16}
17
18/// Borrowed bytes from only the FIFO queue front.
19#[derive(Clone, Copy, Debug, Eq, PartialEq)]
20pub struct WriteSlice<'a> {
21    /// Owning epoch.
22    pub epoch: ConnectionEpoch,
23    /// Owning operation.
24    pub operation: OperationId,
25    /// Write effect required by the corresponding progress report.
26    pub effect: EffectId,
27    /// Next contiguous bytes, capped by the caller's requested size.
28    pub bytes: &'a [u8],
29}
30
31/// Result of applying exact progress to the FIFO front.
32#[derive(Clone, Debug, Eq, PartialEq)]
33pub(crate) enum WriteProgress<F> {
34    /// The same frame remains at the FIFO front.
35    Pending {
36        /// Owning operation.
37        operation: OperationId,
38        /// Whether this report crossed the delivery boundary.
39        boundary: WriteBoundary,
40    },
41    /// The complete frame left write ownership.
42    Complete {
43        /// Owning operation.
44        operation: OperationId,
45        /// Owning effect.
46        effect: EffectId,
47        /// Original complete frame.
48        frame: F,
49        /// Commit-time wire and retained-memory measurement.
50        measure: FrameMeasure,
51        /// Whether this report crossed the delivery boundary.
52        boundary: WriteBoundary,
53        /// Current conservative delivery certainty.
54        delivery: Delivery,
55    },
56}
57
58/// One frame removed before normal write completion.
59#[derive(Clone, Debug, Eq, PartialEq)]
60#[non_exhaustive]
61pub struct DiscardedWrite<F> {
62    /// Owning operation.
63    pub operation: OperationId,
64    /// Owning effect.
65    pub effect: EffectId,
66    /// Original complete frame.
67    pub frame: F,
68    /// Wire and retained-memory measurement captured at commit.
69    pub measure: FrameMeasure,
70    /// Exact bytes that had progressed before removal.
71    pub written: usize,
72    /// Conservative delivery certainty at removal.
73    pub delivery: Delivery,
74}
75
76/// Bounded frames removed together when an epoch closes.
77#[derive(Clone, Debug, Eq, PartialEq)]
78#[non_exhaustive]
79pub struct DiscardedWrites<F> {
80    pub(crate) writes: Vec<DiscardedWrite<F>>,
81    pub(crate) retained_bytes: RetainedBytes,
82}
83
84impl<F> DiscardedWrites<F> {
85    /// Returns discarded writes in original wire order.
86    pub fn writes(&self) -> &[DiscardedWrite<F>] {
87        &self.writes
88    }
89
90    /// Returns variable memory released by the discarded frames.
91    pub const fn retained_bytes(&self) -> RetainedBytes {
92        self.retained_bytes
93    }
94
95    /// Recovers discarded frames in original wire order.
96    pub fn into_writes(self) -> Vec<DiscardedWrite<F>> {
97        self.writes
98    }
99}
100
101impl<F> WriteQueue<F> {
102    pub(crate) fn front_is_empty(&self) -> bool {
103        self.frames
104            .front()
105            .is_some_and(|frame| frame.measure.wire_bytes() == 0)
106    }
107}