Skip to main content

dope_sys/memory/buffer/
raw.rs

1use o3::buffer;
2
3use crate::memory;
4
5type Extent<P> = <P as Pending>::Extent;
6type Rejection<P> = (
7    P,
8    <Extent<P> as memory::raw::Extent>::Length,
9    <Extent<P> as memory::raw::Extent>::Capacity,
10);
11
12/// A unique completion transfers one selected slot from its original ring.
13pub trait Select {
14    type Index: Copy;
15    type Pending: Token<Index = Self::Index>
16        + Pending<
17            Complete: Token<
18                Index = Self::Index,
19                Length = <Extent<Self::Pending> as memory::raw::Extent>::Length,
20            >,
21        >;
22
23    /// # Safety
24    /// A unique CQE transfers any valid reported slot from this ring.
25    /// Keep its payload live; project and return only through its original context/ring.
26    unsafe fn select(&self, index: Self::Index) -> Result<Self::Pending, Self::Index>;
27}
28
29/// # Safety
30/// The index names this owner's unique live slot and cannot change or duplicate.
31/// Its length denotes a live prefix wholly contained within that same slot.
32pub unsafe trait Token {
33    type Index: Copy;
34    type Length: Copy;
35
36    fn index(&self) -> Self::Index;
37    fn length(&self) -> Self::Length;
38}
39
40/// Projects a completed prefix from its original fixed-residency payload allocation.
41pub trait Context<T: Token>: buffer::raw::Fixed<T> {
42    /// # Safety
43    /// The completed token belongs to this exact context and retains its selected slot.
44    /// Its initialized prefix remains immutable and unavailable to the producer until the borrow ends.
45    unsafe fn bytes<'a>(&'a self, buffer: &'a T) -> &'a [u8];
46}
47
48/// A selected slot whose completed prefix has not yet been admitted.
49pub trait Pending: Sized {
50    type Extent: memory::raw::Extent;
51    type Complete;
52
53    fn try_complete(
54        self,
55        len: <Self::Extent as memory::raw::Extent>::Length,
56    ) -> Result<Self::Complete, Rejection<Self>>;
57}