Skip to main content

dope_core/io/transfer/
progress.rs

1use std::{mem, num::NonZero};
2
3use o3::{base::num::bounded, buffer};
4
5use crate::io::transfer;
6
7/// Contiguous transfer input whose progress is bound to this exact slice.
8pub struct Bytes<'data> {
9    bytes: &'data [u8],
10    progress: State,
11}
12
13/// Prefix selected from one [`Bytes`] cursor and not yet committed.
14#[must_use = "the selected bytes must either be committed or left untouched"]
15pub struct Slice<'a, 'data> {
16    bytes: &'data [u8],
17    step: Step<'a>,
18}
19
20impl<'data> Bytes<'data> {
21    #[must_use]
22    pub fn new(view: transfer::View<'data>) -> Self {
23        let (bytes, len) = view.into_parts();
24        Self {
25            bytes,
26            progress: State::new(len),
27        }
28    }
29
30    #[must_use]
31    pub const fn is_empty(&self) -> bool {
32        self.progress.is_empty()
33    }
34
35    pub fn take(&mut self, limit: transfer::Len) -> Slice<'_, 'data> {
36        let start = self.bytes.len() - self.progress.left().into_raw();
37        let step = self.progress.reserve(limit);
38        let end = start + step.amount().into_raw();
39        // SAFETY: `Bytes::new` binds the state limit to this exact slice.
40        // `State::reserve` selects no more than its uncommitted remainder, so
41        // `start..end` remains inside `bytes` and cannot overflow.
42        let bytes = unsafe { self.bytes.get_unchecked(start..end) };
43        Slice { bytes, step }
44    }
45
46    #[must_use]
47    pub const fn into_len(self) -> transfer::Len {
48        self.progress.len()
49    }
50}
51
52impl Slice<'_, '_> {
53    #[must_use]
54    pub const fn as_slice(&self) -> &[u8] {
55        self.bytes
56    }
57
58    #[must_use]
59    pub const fn amount(&self) -> transfer::Len {
60        self.step.amount()
61    }
62
63    pub fn commit(self) {
64        self.step.commit();
65    }
66}
67
68const _: () =
69    assert!(mem::size_of::<Bytes<'_>>() == mem::size_of::<&[u8]>() + mem::size_of::<State>());
70
71/// Exact amount rejected by a bounded progress transition.
72#[derive(Clone, Copy, Eq, PartialEq)]
73pub struct Exceeded {
74    requested: transfer::Len,
75    available: transfer::Len,
76}
77
78impl Exceeded {
79    const fn new(req: transfer::Len, avail: transfer::Len) -> Self {
80        Self {
81            requested: req,
82            available: avail,
83        }
84    }
85
86    pub const fn requested(self) -> transfer::Len {
87        self.requested
88    }
89
90    pub const fn available(self) -> transfer::Len {
91        self.available
92    }
93}
94
95/// Monotonic committed prefix of one bounded transfer.
96pub struct State {
97    limit: transfer::Len,
98    remaining: transfer::Len,
99}
100
101impl State {
102    #[must_use]
103    pub const fn new(limit: transfer::Len) -> Self {
104        Self {
105            limit,
106            remaining: limit,
107        }
108    }
109
110    /// Counts the nonempty prefix before `suffix` as consumed, when one exists.
111    #[must_use]
112    pub const fn from_tail(limit: transfer::Len, suffix: transfer::Len) -> Option<Self> {
113        if limit.is_greater(suffix) {
114            Some(Self {
115                limit,
116                remaining: suffix,
117            })
118        } else {
119            None
120        }
121    }
122
123    #[must_use]
124    pub const fn is_empty(&self) -> bool {
125        self.remaining.is_zero()
126    }
127
128    /// Returns the uncommitted suffix without discarding its backend bound.
129    #[must_use]
130    pub const fn left(&self) -> transfer::Len {
131        self.remaining
132    }
133
134    #[must_use]
135    pub const fn len(&self) -> transfer::Len {
136        let consumed = self.limit.into_raw() - self.remaining.into_raw();
137        // SAFETY: construction bounds remaining by limit and every step only reduces it.
138        transfer::Len::from_bound(unsafe { bounded::raw::U32::from_proven(consumed as u32) })
139    }
140
141    pub fn reserve(&mut self, limit: transfer::Len) -> Step<'_> {
142        let amount = limit.min(self.remaining);
143        Step {
144            progress: self,
145            amount,
146        }
147    }
148
149    /// Advances an exact scatter cursor within this transfer's remaining budget.
150    pub fn scatter<'a>(
151        &mut self,
152        cursor: &mut impl buffer::raw::Scatter<'a>,
153    ) -> Option<buffer::raw::Chunk<'a, { transfer::MAX_BYTES }>> {
154        let cap = NonZero::new(cursor.remaining().min(self.remaining.into_raw()))?;
155        // SAFETY: cap fits this exact cursor and the remaining backend-bounded budget.
156        let chunk = unsafe { cursor.advance::<{ transfer::MAX_BYTES }>(cap) };
157        let count = transfer::Len::from_count(chunk.len().into_count());
158        self.remaining = self.remaining.abs_diff(count);
159        Some(chunk)
160    }
161
162    /// Prepares an exact extended state when the whole amount fits.
163    pub fn try_extend(&self, amount: transfer::Len) -> Result<Self, Exceeded> {
164        if amount.is_greater(self.remaining) {
165            return Err(Exceeded::new(amount, self.remaining));
166        }
167        Ok(Self {
168            limit: self.limit,
169            remaining: self.remaining.abs_diff(amount),
170        })
171    }
172}
173
174/// Prefix selected from one [`State`] and not yet committed.
175pub struct Step<'a> {
176    progress: &'a mut State,
177    amount: transfer::Len,
178}
179
180impl Step<'_> {
181    /// Returns the selected prefix without discarding its backend bound.
182    #[must_use]
183    pub const fn amount(&self) -> transfer::Len {
184        self.amount
185    }
186
187    pub fn commit(self) {
188        self.progress.remaining = self.progress.remaining.abs_diff(self.amount);
189    }
190}
191
192const _: () = {
193    assert!(mem::size_of::<State>() == 2 * mem::size_of::<u32>());
194    let state = mem::size_of::<&mut State>() + mem::size_of::<u32>();
195    let align = mem::align_of::<Step<'_>>();
196    assert!(mem::size_of::<Step<'_>>() == state.div_ceil(align) * align);
197};