use crate::ArenaProvider;
use std::sync::Arc;
pub struct Pool<'p> {
pool: PoolRef<'p>,
workspace: ArenaProvider,
}
enum PoolRef<'p> {
Borrowed(&'p threadteam::Pool),
Shared(Arc<threadteam::Pool>),
}
impl<'p> Pool<'p> {
pub fn borrow(pool: &'p threadteam::Pool) -> Self {
Self::with(PoolRef::Borrowed(pool))
}
fn with(pool: PoolRef<'p>) -> Self {
Self {
pool,
workspace: ArenaProvider::new(),
}
}
pub fn workspace(&self) -> &ArenaProvider {
&self.workspace
}
pub fn trim_workspace(&self) {
use crate::WorkspaceProvider;
self.workspace.trim();
}
pub fn size(&self) -> usize {
self.inner().width().get()
}
pub(crate) fn inner(&self) -> &threadteam::Pool {
match &self.pool {
PoolRef::Borrowed(p) => p,
PoolRef::Shared(p) => p,
}
}
}
impl Pool<'static> {
pub fn owned(pool: threadteam::Pool) -> Self {
Self::shared(Arc::new(pool))
}
pub fn shared(pool: Arc<threadteam::Pool>) -> Self {
Self::with(PoolRef::Shared(pool))
}
}
impl std::fmt::Debug for Pool<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Pool").field("size", &self.size()).finish()
}
}