1use std::{
2 marker, mem,
3 num::NonZero,
4 ops,
5 slice::{from_raw_parts, from_raw_parts_mut},
6};
7
8use crate::{
9 base::num,
10 buffer,
11 buffer::{
12 pool::{self, owners, state},
13 resident, segments,
14 storage::provided,
15 },
16 mem::rc,
17};
18mod output;
19pub use output::Target;
20mod slices;
21pub use slices::Slices;
22mod store;
23pub use store::Store;
24mod segment;
25pub(in crate::buffer) use segment::Seal;
26pub use segment::Segment;
27mod scatter;
28pub use scatter::Scatter;
29pub(in crate::buffer) use slices::{Exclusive, Prefix, SliceExt};
30
31pub unsafe trait Consume: AsRef<[u8]> {
35 fn consume<const MAX: u32>(prefix: Consumed<'_, Self, MAX>);
36}
37
38unsafe impl Consume for &mut [u8] {
40 fn consume<const MAX: u32>(prefix: Consumed<'_, Self, MAX>) {
41 let (target, end) = prefix.into_parts();
42 let bytes = mem::take(target);
43 *target = unsafe { bytes.get_unchecked_mut(end.count().get() as usize..) };
45 }
46}
47
48unsafe impl<S: ?Sized> Consume for buffer::View<'_, S> {
51 fn consume<const MAX: u32>(prefix: Consumed<'_, Self, MAX>) {
52 let (view, end) = prefix.into_parts();
53 view.proof = unsafe { view.proof.get_unchecked(end.count().get() as usize..) };
55 }
56}
57
58pub unsafe trait Retain: Sized {
62 fn try_retain(&self) -> Result<Self, rc::Error>;
63}
64
65unsafe impl<T: crate::mem::raw::Target> Retain for crate::mem::raw::Owner<T> {
67 fn try_retain(&self) -> Result<Self, rc::Error> {
68 self.try_clone()
69 }
70}
71
72unsafe impl<T: Retain> Retain for Option<T> {
74 fn try_retain(&self) -> Result<Self, rc::Error> {
75 self.as_ref().map(T::try_retain).transpose()
76 }
77}
78
79pub unsafe fn utf8<O: Source>(owner: O) -> buffer::storage::Str<O> {
83 unsafe { buffer::storage::Str::new(owner) }
85}
86
87pub unsafe fn nonempty<O: Source>(owner: O) -> buffer::storage::NonEmpty<O> {
90 unsafe { buffer::storage::NonEmpty::assume(owner) }
92}
93
94pub unsafe trait Convert<T, const BRANCH: u8 = 0> {
98 fn into(self) -> T;
99}
100
101unsafe impl<T> Convert<T> for T {
103 fn into(self) -> T {
104 self
105 }
106}
107
108unsafe impl<L, R> Convert<crate::base::Either<L, R>, 1> for L {
110 fn into(self) -> crate::base::Either<L, R> {
111 crate::base::Either::Left(self)
112 }
113}
114
115unsafe impl<L, R> Convert<crate::base::Either<L, R>, 2> for R {
117 fn into(self) -> crate::base::Either<L, R> {
118 crate::base::Either::Right(self)
119 }
120}
121
122pub unsafe trait Chunks {
127 type Cursor<'data>: Scatter<'data>
128 where
129 Self: 'data;
130
131 fn is_empty(&self) -> bool {
132 self.len() == 0
133 }
134
135 fn len(&self) -> usize;
136 fn chunks(&self) -> Self::Cursor<'_>;
137 fn cursor(&self, offset: usize) -> Option<Self::Cursor<'_>> {
138 Scatter::seek(self.chunks(), offset)
139 }
140
141 fn suffix<const N: usize>(&self) -> Option<[u8; N]> {
143 let mut cursor = self.chunks();
144 let skipped = buffer::raw::Scatter::remaining(&cursor).checked_sub(N)?;
145 segments::scatter::Take::new(&mut cursor, skipped).finish();
146 let mut output = [0; N];
147 let source = segments::scatter::Take::new(&mut cursor, usize::MAX);
148 unsafe {
151 buffer::raw::copy_slices(source.map(|chunk| chunk.bytes()), output.as_mut_ptr());
152 }
153 Some(output)
154 }
155}
156
157pub unsafe trait Input<T> {
161 fn bytes(&self) -> &[u8];
162 fn into_item(self) -> (T, impl for<'a> FnOnce(&'a mut T) -> &'a mut [u8]);
163}
164
165pub trait Fixed<T> {
167 fn resident(&self) -> NonZero<u32>;
168
169 unsafe fn project<'a, 'id>(&'a self, item: &'a mut T, limit: Limit<'id>)
173 -> Projection<'a, 'id>;
174
175 unsafe fn insert<'d>(
179 vacant: provided::Vacant<'d, T, (), &'d Self>,
180 item: T,
181 ) -> provided::Unique<'d, ()>
182 where
183 Self: Sized,
184 {
185 unsafe { vacant.insert_fixed(item) }
187 }
188}
189
190pub unsafe trait Arenas: Unpin {
194 type Roots<'d>: 'd
195 where
196 Self: 'd;
197
198 unsafe fn enter(&mut self) -> (Self::Roots<'_>, impl FnOnce() + '_);
201}
202
203unsafe impl<A: Arenas, B: Arenas> Arenas for (A, B) {
205 type Roots<'d>
206 = (A::Roots<'d>, B::Roots<'d>)
207 where
208 Self: 'd;
209
210 unsafe fn enter(&mut self) -> (Self::Roots<'_>, impl FnOnce() + '_) {
211 let (first, finish) = unsafe { self.0.enter() };
213 let (second, end) = unsafe { self.1.enter() };
214 ((first, second), move || {
215 end();
216 finish();
217 })
218 }
219}
220
221#[repr(transparent)]
223pub struct Projection<'a, 'id> {
224 bytes: &'a mut [u8],
225 operation: marker::PhantomData<fn(&'id mut ()) -> &'id mut ()>,
226}
227
228impl<'a> Projection<'a, '_> {
229 pub(in crate::buffer) const fn into_inner(self) -> &'a mut [u8] {
230 self.bytes
231 }
232}
233
234pub type Text<'d> = buffer::storage::Str<resident::Exact<'d>>;
236
237pub struct Output<'a, L = pool::Layout>
239where
240 owners::Plain<L>: crate::buffer::raw::Backing,
241{
242 target: &'a mut pool::Span<owners::Plain<L>, pool::Owned, state::Initialized>,
243}
244
245impl<L> pool::Span<owners::Plain<L>, pool::Owned, state::Initialized>
246where
247 owners::Plain<L>: crate::buffer::raw::Backing,
248{
249 pub fn output(&mut self) -> Output<'_, L> {
250 Output { target: self }
251 }
252}
253
254impl<'a, L> Output<'a, L>
255where
256 owners::Plain<L>: crate::buffer::raw::Backing,
257{
258 pub fn is_empty(&self) -> bool {
259 self.len() == 0
260 }
261
262 pub fn len(&self) -> usize {
263 self.target.capacity().get() as usize - self.target.len()
264 }
265
266 pub fn as_mut_slice(&mut self) -> &mut [u8] {
267 let (data, start) = buffer::raw::Prefix::prefix(self.target);
268 unsafe { range_mut(data.cast(), start as usize, self.len()) }
270 }
271
272 pub unsafe fn complete(self, len: NonZero<usize>) {
275 *Exclusive::len_mut(self.target) += len.get() as u32;
276 }
277}
278
279const _: () = assert!(size_of::<Output<'_>>() == size_of::<usize>());
280
281pub unsafe trait Source: AsRef<[u8]> {}
285
286pub unsafe fn view<S: Source + ?Sized>(
290 source: &S,
291 range: ops::Range<usize>,
292) -> buffer::View<'_, S> {
293 buffer::View {
294 source,
295 proof: unsafe { source.as_ref().get_unchecked(range) },
297 }
298}
299
300unsafe impl<T: buffer::raw::Consume> Source for T {}
302
303unsafe impl Source for &buffer::storage::Shared {}
305
306pub unsafe trait Mutable: Source + AsMut<[u8]> {}
310
311pub unsafe trait Split: Mutable {}
315
316pub unsafe trait Freeze: Source + Sized {
320 type Output: Source;
321
322 fn freeze(self) -> Self::Output;
323}
324
325pub unsafe trait Initializer {
329 fn len(&self) -> usize;
330
331 fn is_empty(&self) -> bool {
332 self.len() == 0
333 }
334
335 unsafe fn initialize(self, target: *mut mem::MaybeUninit<u8>);
338}
339
340pub use store::Backing;
341pub(in crate::buffer) use store::{Debit, Header, Heap, Policy, Rc};
342
343mod length;
344pub use length::Length;
345pub(in crate::buffer) use length::truncate;
346mod limit;
347pub use limit::Limit;
348mod selection;
349pub(in crate::buffer) use selection::Selection;
350pub use selection::{Consumed, Slice};
351
352#[repr(transparent)]
354pub struct Chunk<'data, const MAX: usize = { usize::MAX }> {
355 bytes: &'data [u8],
356}
357
358pub(in crate::buffer) unsafe fn range<'a>(
365 pointer: *const u8,
366 start: usize,
367 len: usize,
368) -> &'a [u8] {
369 unsafe { from_raw_parts(pointer.add(start), len) }
371}
372
373pub(in crate::buffer) unsafe fn range_mut<'a>(
380 pointer: *mut u8,
381 start: usize,
382 len: usize,
383) -> &'a mut [u8] {
384 unsafe { from_raw_parts_mut(pointer.add(start), len) }
386}
387
388impl<'data, const MAX: usize> Chunk<'data, MAX> {
389 pub unsafe fn new(bytes: &'data [u8]) -> Self {
394 Self { bytes }
395 }
396
397 pub const fn bytes(&self) -> &'data [u8] {
398 self.bytes
399 }
400
401 pub const fn len(&self) -> num::bounded::NonZeroUsize<1, MAX> {
402 unsafe { num::bounded::raw::nonzero_usize(self.bytes.len()) }
404 }
405}
406
407const _: () = {
408 assert!(mem::size_of::<Chunk<'static>>() == mem::size_of::<&[u8]>());
409 assert!(mem::size_of::<Option<Chunk<'static>>>() == mem::size_of::<&[u8]>());
410};
411
412pub unsafe fn copy_slices<'a>(sources: impl IntoIterator<Item = &'a [u8]>, target: *mut u8) {
422 let mut offset = 0;
423 for source in sources {
424 unsafe { source.copy_to(target.add(offset)) };
426 offset += source.len();
427 }
428}