Skip to main content

dope_sys/memory/buffer/
provided.rs

1use std::{num, ptr};
2
3use crate::{
4    io::{Close, Retry},
5    memory,
6    memory::{buffer, buffer::canary},
7};
8
9/// Selects native pending and completed provided-buffer ownership.
10pub trait Source {
11    const PROVIDED: u16;
12    const CANARY: u16;
13    type Entry: Copy;
14    type Index: buffer::Index;
15    type Context<L: memory::raw::Receive>: Context<Layout = L, Complete = Self::Complete<L::Extent>, Cleanup = Self::Cleanup<L>>;
16    type Canary<L: memory::raw::Canary>: canary::raw::Storage<
17            Layout = L,
18            Index = Self::Index,
19            State = Self::Cleanup<L>,
20            Error = <Self::Context<L> as Context>::Error,
21        >;
22    type Cleanup<L: memory::raw::Receive>: Cleanup<Context = Self::Context<L>, Producer = Self::Producer<L>>;
23    type Producer<L: memory::raw::Receive>: Producer<
24            Layout = L,
25            Context = Self::Context<L>,
26            Pending = Self::Pending<L::Extent>,
27            Index = Self::Index,
28            Entry = Self::Entry,
29        >;
30    type Pending<E: memory::raw::Extent>: buffer::raw::Token<Index = Self::Index, Length = E::Capacity>
31        + buffer::raw::Pending<Extent = E, Complete = Self::Complete<E>>;
32    type Complete<E: memory::raw::Extent>: buffer::raw::Token<Index = Self::Index, Length = E::Length>;
33}
34
35/// Payload ownership and exact completed-prefix projection for one receive ring.
36pub trait Context: Close + memory::Context + buffer::raw::Context<Self::Complete> {
37    type Error: Failure<Cleanup = Self::Cleanup>;
38    type Cleanup: Retry + memory::Failure;
39    type Complete: buffer::raw::Token;
40
41    fn new(layout: Self::Layout) -> Result<Self, Self::Error>;
42    fn as_ptr(&self) -> *const u8;
43}
44
45/// Native ring entries and deferred publication, paired with one payload owner.
46pub trait Producer: Close + buffer::raw::Select + Sized {
47    type Layout: memory::raw::Receive;
48    type Context: Context;
49    type Entry: Copy;
50    type Error: Open<Context = Self::Context, Cleanup = <Self::Context as Context>::Cleanup>;
51
52    fn new(context: &Self::Context) -> Result<Self, Self::Error>;
53    fn flush(&mut self);
54    fn layout(&self) -> Self::Layout;
55    fn registration(&self) -> (ptr::NonNull<Self::Entry>, num::NonZero<u16>);
56}
57
58/// The original mapping failure and any storage still requiring cleanup.
59pub trait Failure {
60    type Cleanup: Retry + memory::Failure;
61
62    fn into_parts(self) -> (memory::Error, Option<Self::Cleanup>);
63}
64
65/// A failed producer allocation that can retire its paired payload owner.
66pub trait Open: Failure {
67    type Context: Context;
68
69    fn close(self, context: Self::Context) -> <Self::Context as Context>::Error;
70}
71
72/// Exact native storage states retained until all mappings are released.
73pub trait Cleanup: Retry + memory::Failure {
74    type Context: Context;
75    type Producer: Producer<Context = Self::Context>;
76
77    fn entries(current: <Self::Producer as Close>::State, context: Self::Context) -> Self;
78    fn payload(current: <Self::Context as Close>::State) -> Self;
79    fn producer(current: <Self::Producer as Close>::State) -> Self;
80}