use core::fmt::Debug;
use core::fmt::Formatter;
use core::fmt::Result;
use core::mem::MaybeUninit;
use crate::index::Detached;
use crate::params::DefaultParams;
use crate::params::Params;
use crate::params::ParamsExt;
use crate::reclaim::Collector;
use crate::table::Table;
pub use crate::table::WeakKeys;
#[repr(transparent)]
pub struct PTab<T, P = DefaultParams>
where
P: Params + ?Sized,
{
inner: Table<T, P>,
}
impl<T, P> PTab<T, P>
where
P: Params + ?Sized,
{
#[inline]
pub fn new() -> Self {
Self {
inner: Table::new(),
}
}
#[inline]
pub const fn capacity(&self) -> usize {
self.inner.cap()
}
#[inline]
pub fn len(&self) -> usize {
self.inner.len() as usize
}
#[inline]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
#[inline]
pub fn insert(&self, value: T) -> Option<Detached>
where
T: 'static,
{
self.inner.insert(value)
}
#[inline]
pub fn write<F>(&self, init: F) -> Option<Detached>
where
T: 'static,
F: FnOnce(&mut MaybeUninit<T>, Detached),
{
self.inner.write(init)
}
#[inline]
pub fn remove(&self, index: Detached) -> bool
where
P::Collector: Collector, {
self.inner.remove(index)
}
#[inline]
pub fn exists(&self, index: Detached) -> bool {
self.inner.exists(index, &P::guard())
}
#[inline]
pub fn with<F, R>(&self, index: Detached, f: F) -> Option<R>
where
F: Fn(&T) -> R,
{
self.inner.with(index, &P::guard(), f)
}
#[inline]
pub fn read(&self, index: Detached) -> Option<T>
where
T: Copy,
{
self.inner.read(index, &P::guard())
}
#[inline]
pub fn weak_keys(&self) -> WeakKeys<'_, T, P> {
self.inner.weak_keys(P::guard())
}
}
impl<T, P> Debug for PTab<T, P>
where
T: Debug,
P: Params + ?Sized,
{
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
f.debug_struct("PTab")
.field("values", &self.inner)
.field("params", &P::debug())
.finish()
}
}
impl<T, P> Default for PTab<T, P>
where
P: Params + ?Sized,
{
#[inline]
fn default() -> Self {
Self::new()
}
}