use std::{borrow, ops, pin};
use crate::base::marker::ThreadBound;
pub mod raw;
#[derive(Debug)]
#[repr(transparent)]
pub struct Occupied<'a, T> {
value: &'a mut T,
bound: ThreadBound,
}
impl<'a, T> Occupied<'a, T> {
fn new(value: &'a mut T, bound: ThreadBound) -> Self {
Self { value, bound }
}
pub fn get_mut(&mut self) -> &mut T {
self.value
}
pub fn into_inner(self) -> T {
self.take().0
}
pub fn take(self) -> (T, raw::Vacant<'a, T>) {
raw::Vacant::take(self)
}
pub fn into_uninit(self) -> raw::Vacant<'a, T> {
raw::Vacant::release(self)
}
pub fn with_pin<R>(mut self, run: impl for<'p> FnOnce(pin::Pin<&'p mut T>) -> R) -> R {
run(raw::pinned(&mut self))
}
}
impl<'a, T, U> Occupied<'a, (T, U)> {
pub fn split(self) -> (Occupied<'a, T>, Occupied<'a, U>) {
let bound = self.bound;
let (first, second) = raw::detach(self);
(Occupied::new(first, bound), Occupied::new(second, bound))
}
}
impl<T> borrow::Borrow<T> for Occupied<'_, T> {
fn borrow(&self) -> &T {
self.value
}
}
impl<T> borrow::BorrowMut<T> for Occupied<'_, T> {
fn borrow_mut(&mut self) -> &mut T {
self.value
}
}
impl<T> ops::Deref for Occupied<'_, T> {
type Target = T;
fn deref(&self) -> &T {
self.value
}
}
impl<T> Drop for Occupied<'_, T> {
fn drop(&mut self) {
raw::clear(self);
}
}
const _: () = {
assert!(size_of::<Occupied<'_, ()>>() == size_of::<usize>());
assert!(size_of::<Option<Occupied<'_, ()>>>() == size_of::<usize>());
};