pub struct ObjectPool<T, const N: usize> { /* private fields */ }Expand description
Fixed-size object pool for frequently allocated types.
Pre-allocates objects at creation, provides O(1) acquire/release. Used in Ring 0 to avoid heap allocation during event processing.
§Type Parameters
T- The type of objects in the pool. Must implementDefault.N- Maximum capacity of the pool (compile-time constant).
§Example
use laminar_core::alloc::ObjectPool;
// Create a pool of 16 buffers
let mut pool: ObjectPool<Vec<u8>, 16> = ObjectPool::with_init(|| Vec::with_capacity(1024));
// Acquire from pool (O(1), no allocation)
let mut buf = pool.acquire().expect("pool not empty");
buf.extend_from_slice(b"hello");
// Return to pool (O(1), no allocation)
buf.clear();
pool.release(buf);§Thread Safety
This pool is NOT thread-safe. Each core in a thread-per-core architecture should have its own pool.
Implementations§
Source§impl<T, const N: usize> ObjectPool<T, N>
impl<T, const N: usize> ObjectPool<T, N>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty pool.
Objects must be added via release() or use with_init() for pre-population.
Sourcepub fn with_init<F>(factory: F) -> Selfwhere
F: Fn() -> T,
pub fn with_init<F>(factory: F) -> Selfwhere
F: Fn() -> T,
Create a pool pre-populated with objects using a factory function.
The factory is called N times to create the initial pool contents.
This allocation happens at startup, not on the hot path.
§Arguments
factory- Function to create each object
§Example
use laminar_core::alloc::ObjectPool;
let pool: ObjectPool<Vec<u8>, 8> = ObjectPool::with_init(|| Vec::with_capacity(256));
assert_eq!(pool.available(), 8);Sourcepub fn release(&mut self, obj: T) -> bool
pub fn release(&mut self, obj: T) -> bool
Release an object back to the pool.
If the pool is full, the object is dropped instead of stored.
§Performance
O(1), no allocation.
§Returns
true if the object was added to the pool, false if it was dropped.
§Example
use laminar_core::alloc::ObjectPool;
let mut pool: ObjectPool<u64, 4> = ObjectPool::new();
assert!(pool.release(42)); // Added to pool
assert_eq!(pool.available(), 1);Trait Implementations§
Auto Trait Implementations§
impl<T, const N: usize> Freeze for ObjectPool<T, N>where
T: Freeze,
impl<T, const N: usize> RefUnwindSafe for ObjectPool<T, N>where
T: RefUnwindSafe,
impl<T, const N: usize> Send for ObjectPool<T, N>where
T: Send,
impl<T, const N: usize> Sync for ObjectPool<T, N>where
T: Sync,
impl<T, const N: usize> Unpin for ObjectPool<T, N>where
T: Unpin,
impl<T, const N: usize> UnwindSafe for ObjectPool<T, N>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.