Struct fyrox_core::pool::Pool
source · pub struct Pool<T, P = Option<T>>where
T: Sized,
P: PayloadContainer<Element = T>,{ /* private fields */ }
Expand description
Pool allows to create as many objects as you want in contiguous memory block. It allows to create and delete objects much faster than if they’ll be allocated on heap. Also since objects stored in contiguous memory block they can be effectively accessed because such memory layout is cache-friendly.
Implementations§
source§impl<T, P> Pool<T, P>where
P: PayloadContainer<Element = T> + 'static,
impl<T, P> Pool<T, P>where
P: PayloadContainer<Element = T> + 'static,
pub fn new() -> Self
pub fn with_capacity(capacity: u32) -> Self
pub fn spawn(&mut self, payload: T) -> Handle<T>
sourcepub fn spawn_at(&mut self, index: u32, payload: T) -> Result<Handle<T>, T>
pub fn spawn_at(&mut self, index: u32, payload: T) -> Result<Handle<T>, T>
Tries to put an object in the pool at given position. Returns Err(payload)
if a corresponding
entry is occupied.
Performance
The method has O(n) complexity in worst case, where n
- amount of free records in the pool.
In typical uses cases n
is very low. It should be noted that if a pool is filled entirely
and you trying to put an object at the end of pool, the method will have O(1) complexity.
Panics
Panics if the index is occupied or reserved (e.g. by take_reserve
).
sourcepub fn spawn_at_handle(
&mut self,
handle: Handle<T>,
payload: T
) -> Result<Handle<T>, T>
pub fn spawn_at_handle(
&mut self,
handle: Handle<T>,
payload: T
) -> Result<Handle<T>, T>
Tries to put an object in the pool at given handle. Returns Err(payload)
if a corresponding
entry is occupied.
Performance
The method has O(n) complexity in worst case, where n
- amount of free records in the pool.
In typical uses cases n
is very low. It should be noted that if a pool is filled entirely
and you trying to put an object at the end of pool, the method will have O(1) complexity.
Panics
Panics if the index is occupied or reserved (e.g. by take_reserve
).
sourcepub fn spawn_with<F: FnOnce(Handle<T>) -> T>(&mut self, callback: F) -> Handle<T>
pub fn spawn_with<F: FnOnce(Handle<T>) -> T>(&mut self, callback: F) -> Handle<T>
Construct a value with the handle it would be given. Note: Handle is not valid until function has finished executing.
sourcepub async fn spawn_with_async<F, Fut>(&mut self, callback: F) -> Handle<T>where
F: FnOnce(Handle<T>) -> Fut,
Fut: Future<Output = T>,
pub async fn spawn_with_async<F, Fut>(&mut self, callback: F) -> Handle<T>where
F: FnOnce(Handle<T>) -> Fut,
Fut: Future<Output = T>,
Asynchronously construct a value with the handle it would be given. Note: Handle is not valid until function has finished executing.
sourcepub fn borrow(&self, handle: Handle<T>) -> &T
pub fn borrow(&self, handle: Handle<T>) -> &T
Borrows shared reference to an object by its handle.
Panics
Panics if handle is out of bounds or generation of handle does not match with generation of pool record at handle index (in other words it means that object at handle’s index is different than the object was there before).
sourcepub fn borrow_mut(&mut self, handle: Handle<T>) -> &mut T
pub fn borrow_mut(&mut self, handle: Handle<T>) -> &mut T
Borrows mutable reference to an object by its handle.
Panics
Panics if handle is out of bounds or generation of handle does not match with generation of pool record at handle index (in other words it means that object at handle’s index is different than the object was there before).
Example
use fyrox_core::pool::Pool;
let mut pool = Pool::<u32>::new();
let a = pool.spawn(1);
let a = pool.borrow_mut(a);
*a = 11;
sourcepub fn try_borrow(&self, handle: Handle<T>) -> Option<&T>
pub fn try_borrow(&self, handle: Handle<T>) -> Option<&T>
Borrows shared reference to an object by its handle.
Returns None if handle is out of bounds or generation of handle does not match with generation of pool record at handle index (in other words it means that object at handle’s index is different than the object was there before).
sourcepub fn try_borrow_mut(&mut self, handle: Handle<T>) -> Option<&mut T>
pub fn try_borrow_mut(&mut self, handle: Handle<T>) -> Option<&mut T>
Borrows mutable reference to an object by its handle.
Returns None if handle is out of bounds or generation of handle does not match with generation of pool record at handle index (in other words it means that object at handle’s index is different than the object was there before).
sourcepub fn borrow_two_mut(
&mut self,
handles: (Handle<T>, Handle<T>)
) -> (&mut T, &mut T)
pub fn borrow_two_mut(
&mut self,
handles: (Handle<T>, Handle<T>)
) -> (&mut T, &mut T)
Borrows mutable references of objects at the same time. This method will succeed only if handles are unique (not equal). Borrowing multiple mutable references at the same time is useful in case if you need to mutate some objects at the same time.
Panics
See borrow_mut
.
Example
use fyrox_core::pool::Pool;
let mut pool = Pool::<u32>::new();
let a = pool.spawn(1);
let b = pool.spawn(2);
let (a, b) = pool.borrow_two_mut((a, b));
*a = 11;
*b = 22;
sourcepub fn borrow_three_mut(
&mut self,
handles: (Handle<T>, Handle<T>, Handle<T>)
) -> (&mut T, &mut T, &mut T)
pub fn borrow_three_mut(
&mut self,
handles: (Handle<T>, Handle<T>, Handle<T>)
) -> (&mut T, &mut T, &mut T)
Borrows mutable references of objects at the same time. This method will succeed only if handles are unique (not equal). Borrowing multiple mutable references at the same time is useful in case if you need to mutate some objects at the same time.
Panics
See borrow_mut
.
Example
use fyrox_core::pool::Pool;
let mut pool = Pool::<u32>::new();
let a = pool.spawn(1);
let b = pool.spawn(2);
let c = pool.spawn(3);
let (a, b, c) = pool.borrow_three_mut((a, b, c));
*a = 11;
*b = 22;
*c = 33;
sourcepub fn borrow_four_mut(
&mut self,
handles: (Handle<T>, Handle<T>, Handle<T>, Handle<T>)
) -> (&mut T, &mut T, &mut T, &mut T)
pub fn borrow_four_mut(
&mut self,
handles: (Handle<T>, Handle<T>, Handle<T>, Handle<T>)
) -> (&mut T, &mut T, &mut T, &mut T)
Borrows mutable references of objects at the same time. This method will succeed only if handles are unique (not equal). Borrowing multiple mutable references at the same time is useful in case if you need to mutate some objects at the same time.
Panics
See borrow_mut
.
Example
use fyrox_core::pool::Pool;
let mut pool = Pool::<u32>::new();
let a = pool.spawn(1);
let b = pool.spawn(2);
let c = pool.spawn(3);
let d = pool.spawn(4);
let (a, b, c, d) = pool.borrow_four_mut((a, b, c, d));
*a = 11;
*b = 22;
*c = 33;
*d = 44;
sourcepub 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>,
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>,
Tries to borrow two objects when a handle to the second object stored in the first object.
sourcepub fn free(&mut self, handle: Handle<T>) -> T
pub fn free(&mut self, handle: Handle<T>) -> T
Moves object out of the pool using the given handle. All handles to the object will become invalid.
Panics
Panics if the given handle is invalid.
sourcepub fn try_free(&mut self, handle: Handle<T>) -> Option<T>
pub fn try_free(&mut self, handle: Handle<T>) -> Option<T>
Tries to move object out of the pool using the given handle. Returns None if given handle is invalid. After object is moved out if the pool, all handles to the object will become invalid.
sourcepub fn take_reserve(&mut self, handle: Handle<T>) -> (Ticket<T>, T)
pub fn take_reserve(&mut self, handle: Handle<T>) -> (Ticket<T>, T)
Moves an object out of the pool using the given handle with a promise that the object will be returned back. Returns pair (ticket, value). The ticket must be used to put the value back!
Motivation
This method is useful when you need to take temporary ownership of an object, do something with it and then put it back while preserving all handles to it and being able to put new objects into the pool without overriding the payload at its handle.
Notes
All handles to the object will be temporarily invalid until the object is returned to the pool! The pool record will
be reserved for a further put_back
call, which means if you lose the ticket you will have an empty
“unusable” pool record forever.
Panics
Panics if the given handle is invalid.
sourcepub fn try_take_reserve(&mut self, handle: Handle<T>) -> Option<(Ticket<T>, T)>
pub fn try_take_reserve(&mut self, handle: Handle<T>) -> Option<(Ticket<T>, T)>
Does the same as take_reserve
but returns an option, instead of panicking.
sourcepub fn put_back(&mut self, ticket: Ticket<T>, value: T) -> Handle<T>
pub fn put_back(&mut self, ticket: Ticket<T>, value: T) -> Handle<T>
Returns the value back into the pool using the given ticket. See take_reserve
for more
information.
sourcepub fn forget_ticket(&mut self, ticket: Ticket<T>)
pub fn forget_ticket(&mut self, ticket: Ticket<T>)
Forgets that value at ticket was reserved and makes it usable again. Useful when you don’t need to put value back by ticket, but just make pool record usable again.
sourcepub fn get_capacity(&self) -> u32
pub fn get_capacity(&self) -> u32
Returns total capacity of pool. Capacity has nothing about real amount of objects in pool!
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Destroys all objects in pool. All handles to objects will become invalid.
Remarks
Use this method cautiously if objects in pool have cross “references” (handles)
to each other. This method will make all produced handles invalid and any further
calls for borrow
or borrow_mut
will raise panic.
pub fn at_mut(&mut self, n: u32) -> Option<&mut T>
pub fn at(&self, n: u32) -> Option<&T>
pub fn handle_from_index(&self, n: u32) -> Handle<T>
sourcepub fn alive_count(&self) -> u32
pub fn alive_count(&self) -> u32
Returns the exact number of “alive” objects in the pool.
Records that have been reserved (e.g. by take_reserve
) are not counted.
It iterates through the entire pool to count the live objects so the complexity is O(n)
.
See also total_count
.
Example
use fyrox_core::pool::Pool;
let mut pool = Pool::<u32>::new();
pool.spawn(123);
pool.spawn(321);
assert_eq!(pool.alive_count(), 2);
sourcepub fn total_count(&self) -> u32
pub fn total_count(&self) -> u32
Returns the number of allocated objects in the pool.
It also counts records that have been reserved (e.g. by take_reserve
).
This method is O(1)
.
See also alive_count
.
pub fn replace(&mut self, handle: Handle<T>, payload: T) -> Option<T>
sourcepub fn is_valid_handle(&self, handle: Handle<T>) -> bool
pub fn is_valid_handle(&self, handle: Handle<T>) -> bool
Checks if given handle “points” to some object.
Example
use fyrox_core::pool::Pool;
let mut pool = Pool::<u32>::new();
let handle = pool.spawn(123);
assert_eq!(pool.is_valid_handle(handle), true)
sourcepub fn iter(&self) -> PoolIterator<'_, T, P> ⓘ
pub fn iter(&self) -> PoolIterator<'_, T, P> ⓘ
Creates new pool iterator that iterates over filled records in pool.
Example
use fyrox_core::pool::Pool;
let mut pool = Pool::<u32>::new();
pool.spawn(123);
pool.spawn(321);
let mut iter = pool.iter();
assert_eq!(*iter.next().unwrap(), 123);
assert_eq!(*iter.next().unwrap(), 321);
sourcepub fn pair_iter(&self) -> PoolPairIterator<'_, T, P> ⓘ
pub fn pair_iter(&self) -> PoolPairIterator<'_, T, P> ⓘ
Creates new pair iterator that iterates over filled records using pair (handle, payload) Can be useful when there is a need to iterate over pool records and know a handle of that record.
sourcepub fn iter_mut(&mut self) -> PoolIteratorMut<'_, T, P> ⓘ
pub fn iter_mut(&mut self) -> PoolIteratorMut<'_, T, P> ⓘ
Creates new pool iterator that iterates over filled records in pool allowing to modify record payload.
Example
use fyrox_core::pool::Pool;
let mut pool = Pool::<u32>::new();
pool.spawn(123);
pool.spawn(321);
let mut iter = pool.iter_mut();
assert_eq!(*iter.next().unwrap(), 123);
assert_eq!(*iter.next().unwrap(), 321);
sourcepub fn pair_iter_mut(&mut self) -> PoolPairIteratorMut<'_, T, P> ⓘ
pub fn pair_iter_mut(&mut self) -> PoolPairIteratorMut<'_, T, P> ⓘ
Creates new pair iterator that iterates over filled records using pair (handle, payload) Can be useful when there is a need to iterate over pool records and know a handle of that record.
sourcepub fn retain<F>(&mut self, pred: F)where
F: FnMut(&T) -> bool,
pub fn retain<F>(&mut self, pred: F)where
F: FnMut(&T) -> bool,
Retains pool records selected by pred
. Useful when you need to remove all pool records
by some criteria.
sourcepub fn begin_multi_borrow<const N: usize>(
&mut self
) -> MultiBorrowContext<'_, N, T, P>
pub fn begin_multi_borrow<const N: usize>(
&mut self
) -> MultiBorrowContext<'_, N, T, P>
Begins multi-borrow that allows you to as many (N
) unique references to the pool
elements as you need. See MultiBorrowContext::try_get
for more info.
pub fn handle_of(&self, ptr: &T) -> Handle<T>
Trait Implementations§
source§impl<T, P> Debug for Pool<T, P>where
T: Sized + Debug,
P: PayloadContainer<Element = T> + Debug,
impl<T, P> Debug for Pool<T, P>where
T: Sized + Debug,
P: PayloadContainer<Element = T> + Debug,
source§impl<T> FromIterator<T> for Pool<T>where
T: 'static,
impl<T> FromIterator<T> for Pool<T>where
T: 'static,
source§fn from_iter<C: IntoIterator<Item = T>>(iter: C) -> Self
fn from_iter<C: IntoIterator<Item = T>>(iter: C) -> Self
source§impl<T, P> Index<Handle<T>> for Pool<T, P>where
T: 'static,
P: PayloadContainer<Element = T> + 'static,
impl<T, P> Index<Handle<T>> for Pool<T, P>where
T: 'static,
P: PayloadContainer<Element = T> + 'static,
source§impl<T, P> IndexMut<Handle<T>> for Pool<T, P>where
T: 'static,
P: PayloadContainer<Element = T> + 'static,
impl<T, P> IndexMut<Handle<T>> for Pool<T, P>where
T: 'static,
P: PayloadContainer<Element = T> + 'static,
source§impl<'a, T, P> IntoIterator for &'a Pool<T, P>where
P: PayloadContainer<Element = T> + 'static,
impl<'a, T, P> IntoIterator for &'a Pool<T, P>where
P: PayloadContainer<Element = T> + 'static,
source§impl<'a, T, P> IntoIterator for &'a mut Pool<T, P>where
P: PayloadContainer<Element = T> + 'static,
impl<'a, T, P> IntoIterator for &'a mut Pool<T, P>where
P: PayloadContainer<Element = T> + 'static,
source§impl<T: PartialEq> PartialEq<Pool<T, Option<T>>> for Pool<T>
impl<T: PartialEq> PartialEq<Pool<T, Option<T>>> for Pool<T>
source§impl<T: Reflect> Reflect for Pool<T>
impl<T: Reflect> Reflect for Pool<T>
fn type_name(&self) -> &'static str
fn fields_info(&self) -> Vec<FieldInfo<'_>>
fn into_any(self: Box<Self>) -> Box<dyn Any>
fn as_any(&self) -> &dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
fn as_reflect(&self) -> &dyn Reflect
fn as_reflect_mut(&mut self) -> &mut dyn Reflect
fn set(
&mut self,
value: Box<dyn Reflect>
) -> Result<Box<dyn Reflect>, Box<dyn Reflect>>
fn field(&self, _name: &str) -> Option<&dyn Reflect>
fn field_mut(&mut self, _name: &str) -> Option<&mut dyn Reflect>
fn as_array(&self) -> Option<&dyn ReflectArray>
fn as_array_mut(&mut self) -> Option<&mut dyn ReflectArray>
source§fn set_field(
&mut self,
field: &str,
value: Box<dyn Reflect>
) -> Result<Box<dyn Reflect>, Box<dyn Reflect>>
fn set_field(
&mut self,
field: &str,
value: Box<dyn Reflect>
) -> Result<Box<dyn Reflect>, Box<dyn Reflect>>
#[reflect(setter = ..)]
or falls back to
Reflect::field_mut
fn fields(&self) -> Vec<&dyn Reflect>
fn fields_mut(&mut self) -> Vec<&mut dyn Reflect>
fn as_list(&self) -> Option<&dyn ReflectList>
fn as_list_mut(&mut self) -> Option<&mut dyn ReflectList>
fn as_inheritable_variable(&self) -> Option<&dyn ReflectInheritableVariable>
fn as_inheritable_variable_mut(
&mut self
) -> Option<&mut dyn ReflectInheritableVariable>
source§impl<T: Reflect> ReflectArray for Pool<T>
impl<T: Reflect> ReflectArray for Pool<T>
Auto Trait Implementations§
impl<T, P> RefUnwindSafe for Pool<T, P>where
P: RefUnwindSafe,
impl<T, P> Send for Pool<T, P>where
P: Send,
impl<T, P> Sync for Pool<T, P>where
P: Sync,
impl<T, P> Unpin for Pool<T, P>where
P: Unpin,
impl<T, P> UnwindSafe for Pool<T, P>where
P: UnwindSafe,
Blanket Implementations§
source§impl<T> FieldValue for Twhere
T: 'static,
impl<T> FieldValue for Twhere
T: 'static,
source§impl<T> ResolvePath for Twhere
T: Reflect,
impl<T> ResolvePath for Twhere
T: Reflect,
fn resolve_path<'r, 'p>(
&'r self,
path: &'p str
) -> Result<&'r (dyn Reflect + 'static), ReflectPathError<'p>>
fn resolve_path_mut<'r, 'p>(
&'r mut self,
path: &'p str
) -> Result<&'r mut (dyn Reflect + 'static), ReflectPathError<'p>>
fn get_resolve_path<'r, 'p, T: Reflect>(
&'r self,
path: &'p str
) -> Result<&'r T, ReflectPathError<'p>>
fn get_resolve_path_mut<'r, 'p, T: Reflect>(
&'r mut self,
path: &'p str
) -> Result<&'r mut T, ReflectPathError<'p>>
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.