use std::{cell::Cell, marker::PhantomData};
use crate::depot::queue::resident::{Key, raw::View};
pub trait Target {
type Parked;
fn detached_grant<T>(_view: View<'_, T>, _index: usize) -> Option<Self::Parked> {
None
}
fn retire(&self, key: Key);
}
#[repr(transparent)]
pub struct Lease<'target, T: Target + ?Sized> {
active: Option<(&'target T, Key)>,
target: PhantomData<Cell<&'target T>>,
}
impl<'target, T: Target + ?Sized> Lease<'target, T> {
pub const fn new() -> Self {
Self {
active: None,
target: PhantomData,
}
}
pub(super) fn active(&mut self) -> Option<(&'target T, &mut Key)> {
let (target, key) = self.active.as_mut()?;
Some((*target, key))
}
pub(super) fn set(&mut self, target: &'target T, key: Key) {
self.active = Some((target, key));
}
pub(super) fn detach(&mut self) {
self.active = None;
}
pub(super) fn is_bound(&self) -> bool {
self.active.is_some()
}
}
impl<T: Target + ?Sized> Default for Lease<'_, T> {
fn default() -> Self {
Self::new()
}
}
impl<T: Target + ?Sized> Drop for Lease<'_, T> {
fn drop(&mut self) {
if let Some((target, key)) = self.active {
target.retire(key);
}
}
}