o3/buffer/segments/source/
mod.rs1use std::num::NonZero;
2
3use crate::{
4 alloc,
5 buffer::{self, resident, segments, storage},
6 mem::Rejected,
7};
8
9mod chain;
10mod detach;
11mod hold;
12mod normalize;
13mod pool;
14mod ranges;
15mod view;
16
17pub mod raw {
18 pub use super::{chain::pop_front, hold::raw::release};
19}
20
21pub use normalize::Normalize;
22pub use chain::Prefix;
23pub use detach::{Detach, DetachError};
24pub use crate::buffer::bytes::window::{Copies, CopyError};
25pub use ranges::{Header, Iter, Owned};
26pub use view::{Contiguous, Cursor, Segments};
27pub(in crate::buffer) use ranges::Allocation;
28
29pub type Pool<'d> = pool::Pool<Part<'d>, NonZero<u32>>;
30pub type ContiguousPool<'d> = pool::Pool<storage::NonEmpty<resident::Shared<'d>>>;
31pub type Prepared<'d> = pool::Partition<
32 storage::NonEmpty<resident::Shared<'d>>,
33 (),
34 ContiguousPool<'d>,
35>;
36pub type Scoped<'a, 'd> = pool::Partition<
37 storage::NonEmpty<resident::Shared<'d>>,
38 (),
39 &'a pool::Header<storage::NonEmpty<resident::Shared<'d>>>,
40>;
41
42#[derive(Debug)]
43pub struct Source<'d> {
44 state: State<'d>,
45}
46
47#[derive(Debug)]
48pub struct Sequence<'d> {
49 state: SequenceState<'d>,
50}
51
52#[derive(Debug)]
53enum SequenceState<'d> {
54 Empty,
55 Contiguous(storage::NonEmpty<resident::Shared<'d>>),
56 Prepared(Prepared<'d>),
57}
58
59impl<'d> From<Sequence<'d>> for Source<'d> {
60 fn from(source: Sequence<'d>) -> Self {
61 Self {
62 state: match source.state {
63 SequenceState::Empty => State::Empty,
64 SequenceState::Contiguous(owner) => State::Contiguous(owner),
65 SequenceState::Prepared(source) => State::Prepared(source),
66 },
67 }
68 }
69}
70
71#[derive(Debug)]
72enum State<'d> {
73 Empty,
74 Contiguous(storage::NonEmpty<resident::Shared<'d>>),
75 Ranges(ranges::Owned<'d>),
76 Prepared(Prepared<'d>),
77 Node(chain::Chain<'d, false>),
78 Mixed(chain::Chain<'d>),
79}
80
81#[derive(Debug)]
82pub enum Part<'d> {
83 Contiguous(storage::NonEmpty<resident::Shared<'d>>),
84 Ranges(ranges::Owned<'d>),
85}
86
87impl<'d> From<storage::NonEmpty<resident::Shared<'d>>> for Part<'d> {
88 fn from(owner: storage::NonEmpty<resident::Shared<'d>>) -> Self {
89 Self::Contiguous(owner)
90 }
91}
92
93impl<'d> Part<'d> {
94 fn try_clone(&self) -> Result<Self, crate::mem::rc::Error> {
95 Ok(match self {
96 Self::Contiguous(owner) => Self::Contiguous(owner.try_clone()?),
97 Self::Ranges(owner) => Self::Ranges(owner.try_clone()?),
98 })
99 }
100
101 fn count(&self) -> NonZero<u32> {
102 match self {
103 Self::Contiguous(_) => NonZero::<u32>::MIN,
104 Self::Ranges(owner) => owner.count(),
105 }
106 }
107
108 fn len(&self) -> usize {
109 match self {
110 Self::Contiguous(owner) => owner.len().get(),
111 Self::Ranges(owner) => owner.len(),
112 }
113 }
114
115 fn resident(&self) -> usize {
116 match self {
117 Self::Contiguous(owner) => buffer::Residency::resident(owner),
118 Self::Ranges(owner) => owner.resident(),
119 }
120 }
121
122 fn first(&self) -> &[u8] {
123 match self {
124 Self::Contiguous(owner) => owner.as_ref(),
125 Self::Ranges(owner) => owner.first(),
126 }
127 }
128}
129
130impl<'d> Source<'d> {
131 pub const EMPTY: Self = Self { state: State::Empty };
132
133 pub const fn new() -> Self {
134 Self::EMPTY
135 }
136
137 fn totals(&self) -> Option<chain::Totals<NonZero<u32>>> {
138 let (count, len, resident) = match &self.state {
139 State::Empty => return None,
140 State::Contiguous(owner) => (
141 NonZero::<u32>::MIN,
142 owner.len().get(),
143 buffer::Residency::resident(owner),
144 ),
145 State::Ranges(owner) => (owner.count(), owner.len(), owner.resident()),
146 State::Prepared(source) => {
147 let totals = source.totals()?;
148 return Some(chain::Totals {
149 nodes: totals.nodes,
150 logical: totals.nodes,
151 len: totals.len,
152 resident: totals.resident,
153 });
154 }
155 State::Node(chain) => return Some(chain.totals()),
156 State::Mixed(chain) => return Some(chain.totals()),
157 };
158 Some(chain::Totals { logical: count, nodes: NonZero::<u32>::MIN, len, resident })
159 }
160
161 pub fn len(&self) -> usize {
162 match &self.state {
163 State::Empty => 0,
164 State::Contiguous(owner) => owner.len().get(),
165 State::Ranges(owner) => owner.len(),
166 State::Prepared(source) => source.len(),
167 State::Node(chain) => chain.totals().len,
168 State::Mixed(chain) => chain.totals().len,
169 }
170 }
171
172 pub fn is_empty(&self) -> bool {
173 matches!(self.state, State::Empty)
174 }
175
176 pub fn count(&self) -> segments::Count {
177 let count = match &self.state {
178 State::Empty => return segments::Count::EMPTY,
179 State::Contiguous(_) => return segments::Count::ONE,
180 State::Ranges(owner) => owner.count(),
181 State::Prepared(source) => return source.count(),
182 State::Node(chain) => chain.totals().logical,
183 State::Mixed(chain) => chain.totals().logical,
184 };
185 segments::Count::new(count.get())
186 }
187
188 pub fn part_count(&self) -> segments::Count {
189 match &self.state {
190 State::Empty => segments::Count::EMPTY,
191 State::Mixed(chain) => segments::Count::new(chain.totals().nodes.get()),
192 State::Prepared(source) => source.count(),
193 State::Contiguous(_) | State::Ranges(_) | State::Node(_) => segments::Count::ONE,
194 }
195 }
196
197 pub fn front(&self) -> Option<&[u8]> {
198 Some(match &self.state {
199 State::Empty => return None,
200 State::Contiguous(owner) => owner.as_ref(),
201 State::Ranges(owner) => owner.first(),
202 State::Prepared(source) => return source.front().map(AsRef::as_ref),
203 State::Node(chain) => chain.front().first(),
204 State::Mixed(chain) => chain.front().first(),
205 })
206 }
207
208 pub fn pop_part<const KEEP_SOURCE: bool>(&mut self) -> Option<Part<'d>> {
209 chain::Chain::pop_part::<KEEP_SOURCE>(self)
210 }
211
212 pub fn contiguous(&self) -> Option<&[u8]> {
213 match &self.state {
214 State::Empty => Some(&[]),
215 State::Contiguous(owner) => Some(owner.as_ref()),
216 State::Ranges(owner) => owner.contiguous(),
217 State::Prepared(source) => source.contiguous(),
218 State::Node(chain) => match chain.front() {
219 Part::Contiguous(owner) => Some(owner.as_ref()),
220 Part::Ranges(owner) => owner.contiguous(),
221 },
222 State::Mixed(_) => None,
223 }
224 }
225
226 pub fn try_append(
227 &mut self,
228 source: Self,
229 ) -> Result<(), Rejected<Self, segments::assembly::Error>> {
230 chain::Chain::try_extend(self, source)
231 }
232
233 pub fn try_join(&mut self, source: &mut Self) -> Result<(), segments::Error> {
234 chain::Chain::try_join(self, source)
235 }
236
237 pub fn try_take<const KEEP_SOURCE: bool>(&mut self, amount: usize) -> Result<Self, segments::Error> {
238 chain::Chain::try_take::<KEEP_SOURCE>(self, amount)
239 }
240
241 pub fn prefix<const N: usize>(&mut self, amount: usize) -> Result<Prefix<'_, 'd, N>, buffer::write::consume::Error> {
242 Prefix::new(self, amount)
243 }
244
245 pub fn try_consume<const KEEP_SOURCE: bool>(&mut self, amount: usize) -> Result<(), buffer::write::consume::Error> {
246 Prefix::<0>::new(self, amount)?.discard::<KEEP_SOURCE>();
247 Ok(())
248 }
249
250 pub fn try_clone(&self) -> Result<Self, buffer::hold::ranges::retain::Error> {
251 use buffer::hold::ranges::retain::Error;
252 Ok(Self {
253 state: match &self.state {
254 State::Empty => State::Empty,
255 State::Contiguous(owner) => {
256 State::Contiguous(owner.try_clone().map_err(Error::Retain)?)
257 }
258 State::Ranges(owner) => {
259 State::Ranges(owner.try_clone().map_err(Error::Retain)?)
260 }
261 State::Prepared(source) => State::Prepared(source.try_clone()?),
262 State::Node(chain) => State::Node(chain.try_clone()?),
263 State::Mixed(chain) => State::Mixed(chain.try_clone()?),
264 },
265 })
266 }
267
268 pub(in crate::buffer) unsafe fn from_ranges<N: buffer::raw::Length>(
272 mut source: resident::Shared<'d>,
273 ranges: &[(N, N)],
274 count: segments::Count,
275 len: usize,
276 ) -> Result<Self, Rejected<resident::Shared<'d>, alloc::Error>> {
277 match count.kind() {
278 segments::Kind::Empty => Ok(Self::EMPTY),
279 segments::Kind::One => {
280 let &(start, end) = unsafe { &*ranges.as_ptr() };
282 unsafe { source.restrict(start.into_usize()..end.into_usize()) };
283 Ok(Self {
284 state: State::Contiguous(unsafe { storage::NonEmpty::assume(source) }),
286 })
287 }
288 segments::Kind::Many(count) => {
289 let owner = unsafe { ranges::Owned::try_new(source, ranges, count.nonzero(), len) }?;
290 Ok(Self { state: State::Ranges(owner) })
291 }
292 }
293 }
294
295 pub(in crate::buffer) fn allocate_ranges(
296 count: NonZero<u32>,
297 ) -> Result<Allocation<'d>, alloc::Error> {
298 ranges::Owned::allocate(count)
299 }
300
301 pub(in crate::buffer) unsafe fn from_allocated_ranges<N: buffer::raw::Length>(
304 allocation: Allocation<'d>,
305 source: resident::Shared<'d>,
306 ranges: &[(N, N)],
307 count: NonZero<u32>,
308 len: usize,
309 ) -> Self {
310 Self {
311 state: State::Ranges(unsafe {
312 ranges::Owned::initialize(allocation, source, ranges, count, len)
313 }),
314 }
315 }
316}
317
318impl Default for Source<'_> {
319 fn default() -> Self {
320 Self::EMPTY
321 }
322}
323
324impl<'d> From<resident::Shared<'d>> for Source<'d> {
325 fn from(owner: resident::Shared<'d>) -> Self {
326 match storage::NonEmpty::try_new(owner) {
327 Ok(owner) => owner.into(),
328 Err(_) => Self::EMPTY,
329 }
330 }
331}
332
333impl<'d> From<storage::NonEmpty<resident::Shared<'d>>> for Source<'d> {
334 fn from(owner: storage::NonEmpty<resident::Shared<'d>>) -> Self {
335 Self { state: State::Contiguous(owner) }
336 }
337}
338
339impl<'d> From<Part<'d>> for Source<'d> {
340 fn from(part: Part<'d>) -> Self {
341 Self {
342 state: match part {
343 Part::Contiguous(owner) => State::Contiguous(owner),
344 Part::Ranges(owner) => State::Ranges(owner),
345 },
346 }
347 }
348}
349
350impl<'d> From<Prepared<'d>> for Source<'d> {
351 fn from(source: Prepared<'d>) -> Self {
352 if source.is_empty() {
353 Self::EMPTY
354 } else {
355 Self { state: State::Prepared(source) }
356 }
357 }
358}
359
360impl buffer::Residency for Source<'_> {
361 fn resident(&self) -> usize {
362 self.totals().map_or(0, |totals| totals.resident)
363 }
364}