#![allow(clippy::unneeded_field_pattern)]
use crate::reflect::ReflectArray;
use crate::{
reflect::prelude::*,
visitor::{Visit, VisitResult, Visitor},
};
use arrayvec::ArrayVec;
use std::any::Any;
use std::{
fmt::{Debug, Display, Formatter},
future::Future,
hash::{Hash, Hasher},
iter::FromIterator,
marker::PhantomData,
ops::{Index, IndexMut},
};
const INVALID_GENERATION: u32 = 0;
pub trait PayloadContainer: Sized {
type Element: Sized;
fn new_empty() -> Self;
fn new(element: Self::Element) -> Self;
fn is_some(&self) -> bool;
fn as_ref(&self) -> Option<&Self::Element>;
fn as_mut(&mut self) -> Option<&mut Self::Element>;
fn replace(&mut self, element: Self::Element) -> Option<Self::Element>;
fn take(&mut self) -> Option<Self::Element>;
}
impl<T> PayloadContainer for Option<T> {
type Element = T;
fn new_empty() -> Self {
Self::None
}
fn new(element: Self::Element) -> Self {
Self::Some(element)
}
fn is_some(&self) -> bool {
Option::is_some(self)
}
fn as_ref(&self) -> Option<&Self::Element> {
Option::as_ref(self)
}
fn as_mut(&mut self) -> Option<&mut Self::Element> {
Option::as_mut(self)
}
fn replace(&mut self, element: Self::Element) -> Option<Self::Element> {
Option::replace(self, element)
}
fn take(&mut self) -> Option<Self::Element> {
Option::take(self)
}
}
#[derive(Debug)]
pub struct Pool<T, P = Option<T>>
where
T: Sized,
P: PayloadContainer<Element = T>,
{
records: Vec<PoolRecord<T, P>>,
free_stack: Vec<u32>,
}
impl<T: Reflect> Reflect for Pool<T> {
fn type_name(&self) -> &'static str {
std::any::type_name::<Self>()
}
fn fields_info(&self) -> Vec<FieldInfo> {
vec![]
}
fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
fn as_reflect(&self) -> &dyn Reflect {
self
}
fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
self
}
fn set(&mut self, value: Box<dyn Reflect>) -> Result<Box<dyn Reflect>, Box<dyn Reflect>> {
let this = std::mem::replace(self, value.take()?);
Ok(Box::new(this))
}
fn field(&self, _name: &str) -> Option<&dyn Reflect> {
None
}
fn field_mut(&mut self, _name: &str) -> Option<&mut dyn Reflect> {
None
}
fn as_array(&self) -> Option<&dyn ReflectArray> {
Some(self)
}
fn as_array_mut(&mut self) -> Option<&mut dyn ReflectArray> {
Some(self)
}
}
impl<T: Reflect> ReflectArray for Pool<T> {
fn reflect_index(&self, index: usize) -> Option<&dyn Reflect> {
self.at(index as u32).map(|p| p.as_reflect())
}
fn reflect_index_mut(&mut self, index: usize) -> Option<&mut dyn Reflect> {
self.at_mut(index as u32).map(|p| p.as_reflect_mut())
}
fn reflect_len(&self) -> usize {
self.get_capacity() as usize
}
}
impl<T: PartialEq> PartialEq for Pool<T> {
fn eq(&self, other: &Self) -> bool {
self.records == other.records
}
}
#[derive(Reflect)]
pub struct Handle<T> {
#[reflect(read_only, description = "Index of an object in a pool.")]
index: u32,
#[reflect(read_only, description = "Generation of an object in a pool.")]
generation: u32,
#[reflect(hidden)]
type_marker: PhantomData<T>,
}
unsafe impl<T> Send for Handle<T> {}
unsafe impl<T> Sync for Handle<T> {}
impl<T> Display for Handle<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.index, self.generation)
}
}
#[derive(Copy, Clone, Debug, Ord, PartialOrd, PartialEq, Eq, Hash, Reflect, Visit)]
pub struct ErasedHandle {
#[reflect(read_only)]
index: u32,
#[reflect(read_only)]
generation: u32,
}
impl Default for ErasedHandle {
fn default() -> Self {
Self::none()
}
}
impl<T> From<ErasedHandle> for Handle<T> {
fn from(erased_handle: ErasedHandle) -> Self {
Handle {
index: erased_handle.index,
generation: erased_handle.generation,
type_marker: PhantomData,
}
}
}
impl<T> From<Handle<T>> for ErasedHandle {
fn from(h: Handle<T>) -> Self {
Self {
index: h.index,
generation: h.generation,
}
}
}
impl ErasedHandle {
pub fn none() -> Self {
Self {
index: 0,
generation: INVALID_GENERATION,
}
}
pub fn new(index: u32, generation: u32) -> Self {
Self { index, generation }
}
#[inline(always)]
pub fn is_some(&self) -> bool {
self.generation != INVALID_GENERATION
}
#[inline(always)]
pub fn is_none(&self) -> bool {
!self.is_some()
}
#[inline(always)]
pub fn index(self) -> u32 {
self.index
}
#[inline(always)]
pub fn generation(self) -> u32 {
self.generation
}
}
impl<T> Visit for Handle<T> {
fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
let mut region = visitor.enter_region(name)?;
self.index.visit("Index", &mut region)?;
self.generation.visit("Generation", &mut region)?;
Ok(())
}
}
impl<T> Default for Handle<T> {
fn default() -> Self {
Self::NONE
}
}
impl<T> Debug for Handle<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "[Idx: {}; Gen: {}]", self.index, self.generation)
}
}
#[derive(Debug)]
struct PoolRecord<T, P = Option<T>>
where
T: Sized,
P: PayloadContainer<Element = T>,
{
generation: u32,
payload: P,
}
impl<T: PartialEq> PartialEq for PoolRecord<T> {
fn eq(&self, other: &Self) -> bool {
self.generation == other.generation && self.payload == other.payload
}
}
impl<T, P> Default for PoolRecord<T, P>
where
P: PayloadContainer<Element = T> + 'static,
{
fn default() -> Self {
Self {
generation: INVALID_GENERATION,
payload: P::new_empty(),
}
}
}
impl<T, P> Visit for PoolRecord<T, P>
where
T: Visit + 'static,
P: PayloadContainer<Element = T> + Visit,
{
fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
let mut region = visitor.enter_region(name)?;
self.generation.visit("Generation", &mut region)?;
self.payload.visit("Payload", &mut region)?;
Ok(())
}
}
impl<T> Clone for Handle<T> {
fn clone(&self) -> Handle<T> {
Handle {
index: self.index,
generation: self.generation,
type_marker: PhantomData,
}
}
}
impl<T> Copy for Handle<T> {}
impl<T> Eq for Handle<T> {}
impl<T> PartialEq for Handle<T> {
fn eq(&self, other: &Handle<T>) -> bool {
self.generation == other.generation && self.index == other.index
}
}
impl<T, P> Visit for Pool<T, P>
where
T: Visit + 'static,
P: PayloadContainer<Element = T> + Default + Visit + 'static,
{
fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
let mut region = visitor.enter_region(name)?;
self.records.visit("Records", &mut region)?;
self.free_stack.visit("FreeStack", &mut region)?;
Ok(())
}
}
impl<T> Hash for Handle<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.index.hash(state);
self.generation.hash(state);
}
}
impl<T> Handle<T> {
pub const NONE: Handle<T> = Handle {
index: 0,
generation: INVALID_GENERATION,
type_marker: PhantomData,
};
#[inline(always)]
pub fn is_none(self) -> bool {
self.index == 0 && self.generation == INVALID_GENERATION
}
#[inline(always)]
pub fn is_some(self) -> bool {
!self.is_none()
}
#[inline(always)]
pub fn index(self) -> u32 {
self.index
}
#[inline(always)]
pub fn generation(self) -> u32 {
self.generation
}
#[inline(always)]
pub fn new(index: u32, generation: u32) -> Self {
Handle {
index,
generation,
type_marker: PhantomData,
}
}
}
impl<T> Default for Pool<T>
where
T: 'static,
{
fn default() -> Self {
Self::new()
}
}
#[derive(Debug)]
pub struct Ticket<T> {
index: u32,
marker: PhantomData<T>,
}
impl<T: Clone> Clone for PoolRecord<T> {
fn clone(&self) -> Self {
Self {
generation: self.generation,
payload: self.payload.clone(),
}
}
}
impl<T: Clone> Clone for Pool<T> {
fn clone(&self) -> Self {
Self {
records: self.records.clone(),
free_stack: self.free_stack.clone(),
}
}
}
impl<T, P> Pool<T, P>
where
P: PayloadContainer<Element = T> + 'static,
{
#[inline]
pub fn new() -> Self {
Pool {
records: Vec::new(),
free_stack: Vec::new(),
}
}
#[inline]
pub fn with_capacity(capacity: u32) -> Self {
let capacity = usize::try_from(capacity).expect("capacity overflowed usize");
Pool {
records: Vec::with_capacity(capacity),
free_stack: Vec::new(),
}
}
fn records_len(&self) -> u32 {
u32::try_from(self.records.len()).expect("Number of records overflowed u32")
}
fn records_get(&self, index: u32) -> Option<&PoolRecord<T, P>> {
let index = usize::try_from(index).expect("Index overflowed usize");
self.records.get(index)
}
fn records_get_mut(&mut self, index: u32) -> Option<&mut PoolRecord<T, P>> {
let index = usize::try_from(index).expect("Index overflowed usize");
self.records.get_mut(index)
}
#[inline]
#[must_use]
pub fn spawn(&mut self, payload: T) -> Handle<T> {
self.spawn_with(|_| payload)
}
#[inline]
pub fn spawn_at(&mut self, index: u32, payload: T) -> Result<Handle<T>, T> {
self.spawn_at_internal(index, INVALID_GENERATION, payload)
}
pub fn spawn_at_handle(&mut self, handle: Handle<T>, payload: T) -> Result<Handle<T>, T> {
self.spawn_at_internal(handle.index, handle.generation, payload)
}
fn spawn_at_internal(
&mut self,
index: u32,
desired_generation: u32,
payload: T,
) -> Result<Handle<T>, T> {
let index_usize = usize::try_from(index).expect("index overflowed usize");
match self.records.get_mut(index_usize) {
Some(record) => match record.payload.as_ref() {
Some(_) => Err(payload),
None => {
let position = self
.free_stack
.iter()
.rposition(|i| *i == index)
.expect("free_stack must contain the index of the empty record (most likely attempting to spawn at a reserved index)!");
self.free_stack.remove(position);
let generation = if desired_generation == INVALID_GENERATION {
record.generation + 1
} else {
desired_generation
};
record.generation = generation;
record.payload = P::new(payload);
Ok(Handle::new(index, generation))
}
},
None => {
for i in self.records_len()..index {
self.records.push(PoolRecord {
generation: 1,
payload: P::new_empty(),
});
self.free_stack.push(i);
}
let generation = if desired_generation == INVALID_GENERATION {
1
} else {
desired_generation
};
self.records.push(PoolRecord {
generation,
payload: P::new(payload),
});
Ok(Handle::new(index, generation))
}
}
}
#[inline]
#[must_use]
pub fn spawn_with<F: FnOnce(Handle<T>) -> T>(&mut self, callback: F) -> Handle<T> {
if let Some(free_index) = self.free_stack.pop() {
let record = self
.records_get_mut(free_index)
.expect("free stack contained invalid index");
if record.payload.is_some() {
panic!(
"Attempt to spawn an object at pool record with payload! Record index is {}",
free_index
);
}
let generation = record.generation + 1;
let handle = Handle {
index: free_index,
generation,
type_marker: PhantomData,
};
let payload = callback(handle);
record.generation = generation;
record.payload.replace(payload);
handle
} else {
let generation = 1;
let handle = Handle {
index: self.records.len() as u32,
generation,
type_marker: PhantomData,
};
let payload = callback(handle);
let record = PoolRecord {
generation,
payload: P::new(payload),
};
self.records.push(record);
handle
}
}
#[inline]
pub async fn spawn_with_async<F, Fut>(&mut self, callback: F) -> Handle<T>
where
F: FnOnce(Handle<T>) -> Fut,
Fut: Future<Output = T>,
{
if let Some(free_index) = self.free_stack.pop() {
let record = self
.records_get_mut(free_index)
.expect("free stack contained invalid index");
if record.payload.is_some() {
panic!(
"Attempt to spawn an object at pool record with payload! Record index is {}",
free_index
);
}
let generation = record.generation + 1;
let handle = Handle {
index: free_index,
generation,
type_marker: PhantomData,
};
let payload = callback(handle).await;
record.generation = generation;
record.payload.replace(payload);
handle
} else {
let generation = 1;
let handle = Handle {
index: self.records.len() as u32,
generation,
type_marker: PhantomData,
};
let payload = callback(handle).await;
let record = PoolRecord {
generation,
payload: P::new(payload),
};
self.records.push(record);
handle
}
}
#[inline]
#[must_use]
pub fn borrow(&self, handle: Handle<T>) -> &T {
if let Some(record) = self.records_get(handle.index) {
if record.generation == handle.generation {
if let Some(payload) = record.payload.as_ref() {
payload
} else {
panic!("Attempt to borrow destroyed object at {:?} handle.", handle);
}
} else {
panic!(
"Attempt to use dangling handle {:?}. Record has generation {}!",
handle, record.generation
);
}
} else {
panic!(
"Attempt to borrow object using out-of-bounds handle {:?}! Record count is {}",
handle,
self.records.len()
);
}
}
#[inline]
#[must_use]
pub fn borrow_mut(&mut self, handle: Handle<T>) -> &mut T {
let record_count = self.records.len();
if let Some(record) = self.records_get_mut(handle.index) {
if record.generation == handle.generation {
if let Some(payload) = record.payload.as_mut() {
payload
} else {
panic!("Attempt to borrow destroyed object at {:?} handle.", handle);
}
} else {
panic!("Attempt to borrow object using dangling handle {:?}. Record has {} generation!", handle, record.generation);
}
} else {
panic!(
"Attempt to borrow object using out-of-bounds handle {:?}! Record count is {}",
handle, record_count
);
}
}
#[inline]
#[must_use]
pub fn try_borrow(&self, handle: Handle<T>) -> Option<&T> {
self.records_get(handle.index).and_then(|r| {
if r.generation == handle.generation {
r.payload.as_ref()
} else {
None
}
})
}
#[inline]
#[must_use]
pub fn try_borrow_mut(&mut self, handle: Handle<T>) -> Option<&mut T> {
self.records_get_mut(handle.index).and_then(|r| {
if r.generation == handle.generation {
r.payload.as_mut()
} else {
None
}
})
}
#[inline]
#[must_use = "Handle set must not be ignored"]
pub fn borrow_two_mut(&mut self, handles: (Handle<T>, Handle<T>)) -> (&mut T, &mut T) {
assert_ne!(handles.0.index, handles.1.index);
unsafe {
let this = self as *mut Self;
((*this).borrow_mut(handles.0), (*this).borrow_mut(handles.1))
}
}
#[inline]
#[must_use = "Handle set must not be ignored"]
pub fn borrow_three_mut(
&mut self,
handles: (Handle<T>, Handle<T>, Handle<T>),
) -> (&mut T, &mut T, &mut T) {
assert_ne!(handles.0.index, handles.1.index);
assert_ne!(handles.0.index, handles.2.index);
assert_ne!(handles.1.index, handles.2.index);
unsafe {
let this = self as *mut Self;
(
(*this).borrow_mut(handles.0),
(*this).borrow_mut(handles.1),
(*this).borrow_mut(handles.2),
)
}
}
#[inline]
#[must_use = "Handle set must not be ignored"]
pub fn borrow_four_mut(
&mut self,
handles: (Handle<T>, Handle<T>, Handle<T>, Handle<T>),
) -> (&mut T, &mut T, &mut T, &mut T) {
assert_ne!(handles.0.index, handles.1.index);
assert_ne!(handles.0.index, handles.2.index);
assert_ne!(handles.0.index, handles.3.index);
assert_ne!(handles.1.index, handles.2.index);
assert_ne!(handles.1.index, handles.3.index);
assert_ne!(handles.2.index, handles.3.index);
unsafe {
let this = self as *mut Self;
(
(*this).borrow_mut(handles.0),
(*this).borrow_mut(handles.1),
(*this).borrow_mut(handles.2),
(*this).borrow_mut(handles.3),
)
}
}
pub fn try_borrow_dependant_mut<F>(
&mut self,
handle: Handle<T>,
func: F,
) -> (Option<&mut T>, Option<&mut T>)
where
F: FnOnce(&T) -> Handle<T>,
{
let this = unsafe { &mut *(self as *mut Pool<T, P>) };
let first = self.try_borrow_mut(handle);
if let Some(first_object) = first.as_ref() {
let second_handle = func(first_object);
if second_handle != handle {
return (first, this.try_borrow_mut(second_handle));
}
}
(first, None)
}
#[inline]
pub fn free(&mut self, handle: Handle<T>) -> T {
let index = usize::try_from(handle.index).expect("index overflowed usize");
if let Some(record) = self.records.get_mut(index) {
if record.generation == handle.generation {
self.free_stack.push(handle.index);
if let Some(payload) = record.payload.take() {
payload
} else {
panic!("Attempt to double free object at handle {:?}!", handle);
}
} else {
panic!(
"Attempt to free object using dangling handle {:?}! Record generation is {}",
handle, record.generation
);
}
} else {
panic!("Attempt to free destroyed object using out-of-bounds handle {:?}! Record count is {}", handle, self.records.len());
}
}
#[inline]
pub fn try_free(&mut self, handle: Handle<T>) -> Option<T> {
let index = usize::try_from(handle.index).expect("index overflowed usize");
self.records.get_mut(index).and_then(|record| {
if record.generation == handle.generation {
if let Some(payload) = record.payload.take() {
self.free_stack.push(handle.index);
Some(payload)
} else {
None
}
} else {
None
}
})
}
#[inline]
pub fn take_reserve(&mut self, handle: Handle<T>) -> (Ticket<T>, T) {
if let Some(record) = self.records_get_mut(handle.index) {
if record.generation == handle.generation {
if let Some(payload) = record.payload.take() {
let ticket = Ticket {
index: handle.index,
marker: PhantomData,
};
(ticket, payload)
} else {
panic!(
"Attempt to take already taken object at handle {:?}!",
handle
);
}
} else {
panic!(
"Attempt to take object using dangling handle {:?}! Record generation is {}",
handle, record.generation
);
}
} else {
panic!("Attempt to take destroyed object using out-of-bounds handle {:?}! Record count is {}", handle, self.records.len());
}
}
#[inline]
pub fn try_take_reserve(&mut self, handle: Handle<T>) -> Option<(Ticket<T>, T)> {
if let Some(record) = self.records_get_mut(handle.index) {
if record.generation == handle.generation {
if let Some(payload) = record.payload.take() {
let ticket = Ticket {
index: handle.index,
marker: PhantomData,
};
Some((ticket, payload))
} else {
None
}
} else {
None
}
} else {
None
}
}
pub fn put_back(&mut self, ticket: Ticket<T>, value: T) -> Handle<T> {
let record = self
.records_get_mut(ticket.index)
.expect("Ticket index was invalid");
let old = record.payload.replace(value);
assert!(old.is_none());
Handle::new(ticket.index, record.generation)
}
pub fn forget_ticket(&mut self, ticket: Ticket<T>) {
self.free_stack.push(ticket.index);
}
#[inline]
#[must_use]
pub fn get_capacity(&self) -> u32 {
u32::try_from(self.records.len()).expect("records.len() overflowed u32")
}
#[inline]
pub fn clear(&mut self) {
self.records.clear();
self.free_stack.clear();
}
#[inline]
#[must_use]
pub fn at_mut(&mut self, n: u32) -> Option<&mut T> {
self.records_get_mut(n).and_then(|rec| rec.payload.as_mut())
}
#[inline]
#[must_use]
pub fn at(&self, n: u32) -> Option<&T> {
self.records_get(n).and_then(|rec| rec.payload.as_ref())
}
#[inline]
#[must_use]
pub fn handle_from_index(&self, n: u32) -> Handle<T> {
if let Some(record) = self.records_get(n) {
if record.generation != INVALID_GENERATION {
return Handle::new(n, record.generation);
}
}
Handle::NONE
}
#[inline]
#[must_use]
pub fn alive_count(&self) -> u32 {
let cnt = self.iter().count();
u32::try_from(cnt).expect("alive_count overflowed u32")
}
pub fn total_count(&self) -> u32 {
let free = u32::try_from(self.free_stack.len()).expect("free stack length overflowed u32");
self.records_len() - free
}
#[inline]
pub fn replace(&mut self, handle: Handle<T>, payload: T) -> Option<T> {
let index_usize = usize::try_from(handle.index).expect("index overflowed usize");
if let Some(record) = self.records.get_mut(index_usize) {
if record.generation == handle.generation {
self.free_stack.retain(|i| *i != handle.index);
record.payload.replace(payload)
} else {
panic!("Attempt to replace object in pool using dangling handle! Handle is {:?}, but pool record has {} generation", handle, record.generation);
}
} else {
None
}
}
#[inline]
pub fn is_valid_handle(&self, handle: Handle<T>) -> bool {
if let Some(record) = self.records_get(handle.index) {
record.payload.is_some() && record.generation == handle.generation
} else {
false
}
}
#[must_use]
pub fn iter(&self) -> PoolIterator<T, P> {
unsafe {
PoolIterator {
ptr: self.records.as_ptr(),
end: self.records.as_ptr().add(self.records.len()),
marker: PhantomData,
}
}
}
pub fn pair_iter(&self) -> PoolPairIterator<T, P> {
PoolPairIterator {
pool: self,
current: 0,
}
}
#[must_use]
pub fn iter_mut(&mut self) -> PoolIteratorMut<T, P> {
unsafe {
PoolIteratorMut {
ptr: self.records.as_mut_ptr(),
end: self.records.as_mut_ptr().add(self.records.len()),
marker: PhantomData,
}
}
}
pub fn pair_iter_mut(&mut self) -> PoolPairIteratorMut<T, P> {
unsafe {
PoolPairIteratorMut {
current: 0,
ptr: self.records.as_mut_ptr(),
end: self.records.as_mut_ptr().add(self.records.len()),
marker: PhantomData,
}
}
}
pub fn retain<F>(&mut self, mut pred: F)
where
F: FnMut(&T) -> bool,
{
for (i, record) in self.records.iter_mut().enumerate() {
if record.generation == INVALID_GENERATION {
continue;
}
let retain = if let Some(payload) = record.payload.as_ref() {
pred(payload)
} else {
continue;
};
if !retain {
self.free_stack.push(i as u32);
record.payload.take(); }
}
}
pub fn begin_multi_borrow<const N: usize>(&mut self) -> MultiBorrowContext<N, T, P> {
MultiBorrowContext::new(self)
}
pub fn drain(&mut self) -> impl Iterator<Item = T> + '_ {
self.free_stack.clear();
self.records.drain(..).filter_map(|mut r| r.payload.take())
}
fn end(&self) -> *const PoolRecord<T, P> {
unsafe { self.records.as_ptr().add(self.records.len()) }
}
fn begin(&self) -> *const PoolRecord<T, P> {
self.records.as_ptr()
}
pub fn handle_of(&self, ptr: &T) -> Handle<T> {
let begin = self.begin() as usize;
let end = self.end() as usize;
let val = ptr as *const T as usize;
if val >= begin && val < end {
let record_size = std::mem::size_of::<PoolRecord<T>>();
let record_location = (val - offset_of!(PoolRecord<T>, payload)) - begin;
if record_location % record_size == 0 {
let index = record_location / record_size;
let index = u32::try_from(index).expect("Index overflowed u32");
return self.handle_from_index(index);
}
}
Handle::NONE
}
}
impl<T> FromIterator<T> for Pool<T>
where
T: 'static,
{
fn from_iter<C: IntoIterator<Item = T>>(iter: C) -> Self {
let iter = iter.into_iter();
let (lower_bound, upper_bound) = iter.size_hint();
let lower_bound = u32::try_from(lower_bound).expect("lower_bound overflowed u32");
let upper_bound =
upper_bound.map(|b| u32::try_from(b).expect("upper_bound overflowed u32"));
let mut pool = Self::with_capacity(upper_bound.unwrap_or(lower_bound));
for v in iter {
let _ = pool.spawn(v);
}
pool
}
}
impl<T, P> Index<Handle<T>> for Pool<T, P>
where
T: 'static,
P: PayloadContainer<Element = T> + 'static,
{
type Output = T;
fn index(&self, index: Handle<T>) -> &Self::Output {
self.borrow(index)
}
}
impl<T, P> IndexMut<Handle<T>> for Pool<T, P>
where
T: 'static,
P: PayloadContainer<Element = T> + 'static,
{
fn index_mut(&mut self, index: Handle<T>) -> &mut Self::Output {
self.borrow_mut(index)
}
}
impl<'a, T, P> IntoIterator for &'a Pool<T, P>
where
P: PayloadContainer<Element = T> + 'static,
{
type Item = &'a T;
type IntoIter = PoolIterator<'a, T, P>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a, T, P> IntoIterator for &'a mut Pool<T, P>
where
P: PayloadContainer<Element = T> + 'static,
{
type Item = &'a mut T;
type IntoIter = PoolIteratorMut<'a, T, P>;
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
pub struct PoolIterator<'a, T, P>
where
P: PayloadContainer<Element = T>,
{
ptr: *const PoolRecord<T, P>,
end: *const PoolRecord<T, P>,
marker: PhantomData<&'a T>,
}
impl<'a, T, P> Iterator for PoolIterator<'a, T, P>
where
P: PayloadContainer<Element = T> + 'static,
{
type Item = &'a T;
fn next(&mut self) -> Option<Self::Item> {
unsafe {
while self.ptr != self.end {
let current = &*self.ptr;
if let Some(payload) = current.payload.as_ref() {
self.ptr = self.ptr.offset(1);
return Some(payload);
}
self.ptr = self.ptr.offset(1);
}
None
}
}
}
pub struct PoolPairIterator<'a, T, P: PayloadContainer<Element = T>> {
pool: &'a Pool<T, P>,
current: usize,
}
impl<'a, T, P> Iterator for PoolPairIterator<'a, T, P>
where
P: PayloadContainer<Element = T>,
{
type Item = (Handle<T>, &'a T);
fn next(&mut self) -> Option<Self::Item> {
loop {
match self.pool.records.get(self.current) {
Some(record) => {
if let Some(payload) = record.payload.as_ref() {
let handle = Handle::new(self.current as u32, record.generation);
self.current += 1;
return Some((handle, payload));
}
self.current += 1;
}
None => return None,
}
}
}
}
pub struct PoolIteratorMut<'a, T, P>
where
P: PayloadContainer<Element = T>,
{
ptr: *mut PoolRecord<T, P>,
end: *mut PoolRecord<T, P>,
marker: PhantomData<&'a mut T>,
}
impl<'a, T, P> Iterator for PoolIteratorMut<'a, T, P>
where
P: PayloadContainer<Element = T> + 'static,
{
type Item = &'a mut T;
fn next(&mut self) -> Option<Self::Item> {
unsafe {
while self.ptr != self.end {
let current = &mut *self.ptr;
if let Some(payload) = current.payload.as_mut() {
self.ptr = self.ptr.offset(1);
return Some(payload);
}
self.ptr = self.ptr.offset(1);
}
None
}
}
}
pub struct PoolPairIteratorMut<'a, T, P>
where
P: PayloadContainer<Element = T>,
{
ptr: *mut PoolRecord<T, P>,
end: *mut PoolRecord<T, P>,
marker: PhantomData<&'a mut T>,
current: usize,
}
impl<'a, T, P> Iterator for PoolPairIteratorMut<'a, T, P>
where
P: PayloadContainer<Element = T> + 'static,
{
type Item = (Handle<T>, &'a mut T);
fn next(&mut self) -> Option<Self::Item> {
unsafe {
while self.ptr != self.end {
let current = &mut *self.ptr;
if let Some(payload) = current.payload.as_mut() {
let handle = Handle::new(self.current as u32, current.generation);
self.ptr = self.ptr.offset(1);
self.current += 1;
return Some((handle, payload));
}
self.ptr = self.ptr.offset(1);
self.current += 1;
}
None
}
}
}
pub struct MultiBorrowContext<'a, const N: usize, T, P = Option<T>>
where
T: Sized,
P: PayloadContainer<Element = T> + 'static,
{
pool: &'a mut Pool<T, P>,
borrowed: ArrayVec<Handle<T>, N>,
}
impl<'a, const N: usize, T, P> MultiBorrowContext<'a, N, T, P>
where
T: Sized,
P: PayloadContainer<Element = T> + 'static,
{
fn new(pool: &'a mut Pool<T, P>) -> Self {
Self {
pool,
borrowed: Default::default(),
}
}
pub fn try_get(&mut self, handle: Handle<T>) -> Option<&'a mut T> {
if self.borrowed.contains(&handle) {
None
} else {
unsafe {
let pool = &mut *(self.pool as *mut Pool<T, P>);
if let Some(payload_ref) = pool.try_borrow_mut(handle) {
if self.borrowed.try_push(handle).is_ok() {
Some(payload_ref)
} else {
None
}
} else {
None
}
}
}
}
}
#[cfg(test)]
mod test {
use crate::pool::{Handle, Pool, INVALID_GENERATION};
#[test]
fn pool_sanity_tests() {
let mut pool: Pool<String> = Pool::new();
let foobar_handle = pool.spawn(String::from("Foobar"));
assert_eq!(foobar_handle.index, 0);
assert_ne!(foobar_handle.generation, INVALID_GENERATION);
let foobar_handle_copy = foobar_handle;
assert_eq!(foobar_handle.index, foobar_handle_copy.index);
assert_eq!(foobar_handle.generation, foobar_handle_copy.generation);
let baz_handle = pool.spawn(String::from("Baz"));
assert_eq!(pool.borrow(foobar_handle), "Foobar");
assert_eq!(pool.borrow(baz_handle), "Baz");
pool.free(foobar_handle);
assert!(!pool.is_valid_handle(foobar_handle_copy));
assert!(pool.is_valid_handle(baz_handle));
let at_foobar_index = pool.spawn(String::from("AtFoobarIndex"));
assert_eq!(at_foobar_index.index, 0);
assert_ne!(at_foobar_index.generation, INVALID_GENERATION);
assert_eq!(pool.borrow(at_foobar_index), "AtFoobarIndex");
let bar_handle = pool.spawn_with(|_handle| String::from("Bar"));
assert_ne!(bar_handle.index, 0);
assert_ne!(bar_handle.generation, INVALID_GENERATION);
assert_eq!(pool.borrow(bar_handle), "Bar");
}
#[test]
fn pool_iterator_mut_test() {
let mut pool: Pool<String> = Pool::new();
let foobar = pool.spawn("Foobar".to_string());
let d = pool.spawn("Foo".to_string());
pool.free(d);
let baz = pool.spawn("Baz".to_string());
for s in pool.iter() {
println!("{}", s);
}
for s in pool.iter_mut() {
println!("{}", s);
}
for s in &pool {
println!("{}", s);
}
for s in &mut pool {
println!("{}", s);
}
pool.free(foobar);
pool.free(baz);
}
#[test]
fn handle_of() {
#[allow(dead_code)]
struct Value {
data: String,
}
let mut pool = Pool::<Value>::new();
let foobar = pool.spawn(Value {
data: "Foobar".to_string(),
});
let bar = pool.spawn(Value {
data: "Bar".to_string(),
});
let baz = pool.spawn(Value {
data: "Baz".to_string(),
});
assert_eq!(pool.handle_of(pool.borrow(foobar)), foobar);
assert_eq!(pool.handle_of(pool.borrow(bar)), bar);
assert_eq!(pool.handle_of(pool.borrow(baz)), baz);
}
#[derive(Debug, Eq, PartialEq)]
struct Payload;
#[test]
fn pool_test_spawn_at() {
let mut pool = Pool::<Payload>::new();
assert_eq!(pool.spawn_at(2, Payload), Ok(Handle::new(2, 1)));
assert_eq!(pool.spawn_at(2, Payload), Err(Payload));
assert_eq!(pool.records[0].payload, None);
assert_eq!(pool.records[1].payload, None);
assert_ne!(pool.records[2].payload, None);
assert_eq!(pool.spawn_at(2, Payload), Err(Payload));
pool.free(Handle::new(2, 1));
assert_eq!(pool.spawn_at(2, Payload), Ok(Handle::new(2, 2)));
assert_eq!(pool.spawn(Payload), Handle::new(1, 2));
assert_eq!(pool.spawn(Payload), Handle::new(0, 2));
}
#[test]
fn pool_test_try_free() {
let mut pool = Pool::<Payload>::new();
assert_eq!(pool.try_free(Handle::NONE), None);
assert_eq!(pool.free_stack.len(), 0);
let handle = pool.spawn(Payload);
assert_eq!(pool.try_free(handle), Some(Payload));
assert_eq!(pool.free_stack.len(), 1);
assert_eq!(pool.try_free(handle), None);
assert_eq!(pool.free_stack.len(), 1);
}
#[test]
fn test_multi_borrow_context() {
let mut pool = Pool::<Payload>::new();
let a = pool.spawn(Payload);
let b = pool.spawn(Payload);
let c = pool.spawn(Payload);
let mut ctx = pool.begin_multi_borrow::<2>();
assert_eq!(ctx.try_get(a), Some(&mut Payload));
assert_eq!(ctx.try_get(a), None);
assert_eq!(ctx.try_get(b), Some(&mut Payload));
assert_eq!(ctx.try_get(c), None);
}
}