Skip to main content

dope_core/driver/settings/demands/
mod.rs

1use o3::depot::capacity;
2
3use crate::{
4    driver::{route::table::conn, settings, settings::errors},
5    io::transfer,
6};
7
8pub mod slots;
9
10#[derive(Clone, Copy, Default, Eq, PartialEq)]
11#[repr(C)]
12pub struct Files {
13    accepted: Accept,
14    outbound: Outbound,
15}
16
17#[derive(Clone, Copy, Default, Eq, PartialEq)]
18#[repr(transparent)]
19pub struct Accept(u32);
20
21#[derive(Clone, Copy, Default, Eq, PartialEq)]
22#[repr(transparent)]
23pub struct Outbound(u32);
24
25impl Files {
26    pub const ZERO: Self = Self::new(Accept::ZERO, Outbound::ZERO);
27
28    pub const fn new(accepted: Accept, outbound: Outbound) -> Self {
29        Self { accepted, outbound }
30    }
31
32    pub const fn accept(accepted: Accept) -> Self {
33        Self::new(accepted, Outbound::ZERO)
34    }
35
36    pub const fn only_out(outbound: Outbound) -> Self {
37        Self::new(Accept::ZERO, outbound)
38    }
39
40    pub const fn accepted(self) -> Accept {
41        self.accepted
42    }
43
44    pub const fn outbound(self) -> Outbound {
45        self.outbound
46    }
47
48    pub(in crate::driver::settings) fn merge(self, next: Self) -> Result<Self, errors::Claim> {
49        let current = self.accepted.get();
50        let added = next.accepted.get();
51        let accepted = Accept(
52            current
53                .checked_add(added)
54                .ok_or(errors::Claim::AcceptedDescriptorsOverflow { current, added })?,
55        );
56        let current = self.outbound.get();
57        let added = next.outbound.get();
58        let outbound = Outbound(
59            current
60                .checked_add(added)
61                .ok_or(errors::Claim::OutboundDescriptorsOverflow { current, added })?,
62        );
63        Ok(Self::new(accepted, outbound))
64    }
65}
66
67impl Accept {
68    pub const ZERO: Self = Self(0);
69    pub(in crate::driver::settings) const fn get(self) -> u32 {
70        self.0
71    }
72}
73
74impl From<slots::Slots> for Accept {
75    fn from(slots: slots::Slots) -> Self {
76        Self(slots.raw())
77    }
78}
79
80impl From<conn::Capacity> for Accept {
81    fn from(capacity: conn::Capacity) -> Self {
82        Self(capacity.raw())
83    }
84}
85
86impl Outbound {
87    pub const ZERO: Self = Self(0);
88
89    pub const fn new(count: capacity::Count) -> Self {
90        Self(count.raw())
91    }
92
93    pub const fn fixed<const VALUE: u32>() -> Self {
94        Self(VALUE)
95    }
96    pub(in crate::driver::settings) const fn get(self) -> u32 {
97        self.0
98    }
99
100    pub const fn scale<const FACTOR: u32>(capacity: conn::Capacity) -> Self {
101        Self(capacity.scale::<FACTOR>().raw())
102    }
103}
104
105impl TryFrom<usize> for Accept {
106    type Error = Overflow;
107    fn try_from(value: usize) -> Result<Self, Self::Error> {
108        Ok(Self(capacity::Count::try_from(value)?.raw()))
109    }
110}
111
112impl TryFrom<usize> for Outbound {
113    type Error = Overflow;
114    fn try_from(value: usize) -> Result<Self, Self::Error> {
115        Ok(Self::new(capacity::Count::try_from(value)?))
116    }
117}
118
119/// Additive receive-pool demand. Entries compose by addition while buffer
120/// length composes by maximum because every slot uses the largest framing.
121#[derive(Clone, Copy, Eq, PartialEq)]
122#[repr(C)]
123pub struct Receive {
124    entries: Entries,
125    buffer_len: transfer::Len,
126}
127
128/// Additive receive-slot demand representable by the claim accumulator.
129#[derive(Clone, Copy, Default, Eq, PartialEq)]
130#[repr(transparent)]
131pub struct Entries(u32);
132
133#[derive(Clone, Copy, Debug, Eq, PartialEq)]
134#[repr(transparent)]
135pub struct Overflow {
136    actual: usize,
137}
138
139#[derive(Clone, Copy, Default, Eq, PartialEq)]
140#[repr(transparent)]
141pub struct Submit(u32);
142
143impl Receive {
144    pub const ZERO: Self = Self {
145        entries: Entries::ZERO,
146        buffer_len: transfer::Len::ZERO,
147    };
148
149    pub const fn new(entries: Entries, len: transfer::Len) -> Self {
150        Self {
151            entries,
152            buffer_len: len,
153        }
154    }
155
156    pub const fn entries(self) -> Entries {
157        self.entries
158    }
159
160    pub const fn buffer_len(self) -> transfer::Len {
161        self.buffer_len
162    }
163
164    pub(in crate::driver::settings) fn merge(self, next: Self) -> Result<Self, errors::Claim> {
165        let entries = self.entries.0.checked_add(next.entries.0).ok_or(
166            errors::Claim::ReceiveEntriesOverflow {
167                current: self.entries.0,
168                added: next.entries.0,
169            },
170        )?;
171        Ok(Self::new(
172            Entries(entries),
173            self.buffer_len.max(next.buffer_len),
174        ))
175    }
176}
177
178impl From<settings::Receive> for Receive {
179    fn from(layout: settings::Receive) -> Self {
180        Self::new(
181            Entries(u32::from(layout.entries())),
182            transfer::Len::from_nonzero(layout.buffer_len()),
183        )
184    }
185}
186
187impl Entries {
188    pub const ZERO: Self = Self(0);
189
190    pub(in crate::driver::settings) const fn get(self) -> u32 {
191        self.0
192    }
193}
194
195impl From<capacity::Count> for Entries {
196    fn from(count: capacity::Count) -> Self {
197        Self(count.raw())
198    }
199}
200
201impl TryFrom<usize> for Entries {
202    type Error = Overflow;
203
204    fn try_from(value: usize) -> Result<Self, Self::Error> {
205        Ok(Self(capacity::Count::try_from(value)?.raw()))
206    }
207}
208
209impl From<capacity::Error> for Overflow {
210    fn from(error: capacity::Error) -> Self {
211        Self::new(error.requested())
212    }
213}
214
215impl Overflow {
216    pub(in crate::driver::settings) const fn new(actual: usize) -> Self {
217        Self { actual }
218    }
219}
220
221impl Default for Receive {
222    fn default() -> Self {
223        Self::ZERO
224    }
225}
226
227impl Submit {
228    pub(in crate::driver::settings) const fn get(self) -> u32 {
229        self.0
230    }
231}
232
233impl From<u32> for Submit {
234    fn from(value: u32) -> Self {
235        Self(value)
236    }
237}
238
239const _: () = {
240    assert!(core::mem::size_of::<Files>() == 2 * core::mem::size_of::<u32>());
241    assert!(core::mem::size_of::<Accept>() == core::mem::size_of::<u32>());
242    assert!(core::mem::size_of::<Outbound>() == core::mem::size_of::<u32>());
243    assert!(core::mem::size_of::<Receive>() == 2 * core::mem::size_of::<u32>());
244    assert!(core::mem::size_of::<Entries>() == core::mem::size_of::<u32>());
245    assert!(core::mem::size_of::<Submit>() == core::mem::size_of::<u32>());
246    assert!(core::mem::size_of::<settings::Schedule>() == 2 * core::mem::size_of::<u32>());
247};