use std::mem;
use crate::depot::queue::resident;
pub type Registry<W, G, L> = resident::Lanes<W, G, usize, L>;
impl<W, G, L> resident::Lane<'_, Registry<W, G, L>> {
pub fn offer_one(self, grant: G) -> Offer<W, G> {
if self.owner.is_draining() {
return Offer::Draining(grant);
}
let Some(entry) = self.owner.pop::<true>(self.position) else {
return Offer::Empty(grant);
};
let value = unsafe { entry.take() };
entry.write_grant(grant);
Offer::Value(value)
}
pub fn is_empty(self) -> bool {
self.owner.is_empty(self.position)
}
}
pub type Registration<'target, W, G, L> = resident::Lease<'target, W, G, usize, L>;
impl<W, G, L> Registration<'_, W, G, L> {
pub fn cancel(&mut self) -> Cancel<G> {
let Some((registry, key)) = self.active.take() else {
return Cancel::Empty;
};
match registry.remove(key) {
Cancel::Wake => Cancel::Wake,
Cancel::Grant(entry) => Cancel::Grant(unsafe { entry.take_grant() }),
Cancel::Empty => Cancel::Empty,
}
}
pub fn take_grant(&mut self) -> Grant<G> {
let Some((registry, key)) = self.active else {
return Grant::Empty;
};
let Some(entry) = registry.claim(key) else {
return Grant::Pending;
};
self.active = None;
Grant::Ready(unsafe { entry.take_grant() })
}
}
const _: () = {
assert!(
mem::size_of::<Registration<'static, usize, usize, resident::Fixed<2>>>()
== 2 * mem::size_of::<usize>()
);
assert!(
mem::align_of::<Registration<'static, usize, usize, resident::Fixed<2>>>()
== mem::align_of::<usize>()
);
};
pub type Reject = resident::Reject<usize>;
#[derive(Eq, PartialEq)]
#[must_use = "a rejected affine grant remains caller-owned"]
pub enum Offer<W, G> {
Value(W),
Draining(G),
Empty(G),
}
#[derive(Eq, PartialEq)]
pub enum Grant<G> {
Empty,
Pending,
Ready(G),
}
#[must_use = "a delivered affine grant remains caller-owned"]
#[derive(Eq, PartialEq)]
pub enum Cancel<G> {
Empty,
Wake,
Grant(G),
}