Skip to main content

ObjectPool

Struct ObjectPool 

Source
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 implement Default.
  • 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>

Source

pub fn new() -> Self

Create a new empty pool.

Objects must be added via release() or use with_init() for pre-population.

Source

pub fn with_init<F>(factory: F) -> Self
where 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);
Source

pub fn acquire(&mut self) -> Option<T>

Acquire an object from the pool.

Returns None if the pool is exhausted.

§Performance

O(1), no allocation.

§Example
use laminar_core::alloc::ObjectPool;

let mut pool: ObjectPool<u64, 4> = ObjectPool::with_init(|| 0);
let obj = pool.acquire().unwrap();
assert_eq!(pool.available(), 3);
Source

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);
Source

pub fn available(&self) -> usize

Get the number of available objects in the pool.

Source

pub fn in_use(&self) -> usize

Get the number of objects currently in use.

Source

pub const fn capacity(&self) -> usize

Get the maximum capacity of the pool.

Source

pub fn is_empty(&self) -> bool

Check if the pool is empty (no objects available).

Source

pub fn is_full(&self) -> bool

Check if the pool is full (no more room for released objects).

Source

pub fn clear(&mut self)

Clear all objects from the pool.

Drops all pooled objects and resets counters.

Trait Implementations§

Source§

impl<T: Clone, const N: usize> Clone for ObjectPool<T, N>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, const N: usize> Debug for ObjectPool<T, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default, const N: usize> Default for ObjectPool<T, N>

Source§

fn default() -> Self

Create a pool pre-populated with N default objects.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> ArchivePointee for T

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,