LocalBlindPool

Struct LocalBlindPool 

Source
pub struct LocalBlindPool { /* private fields */ }
Expand description

A single-threaded reference-counting object pool that accepts any type of object.

All values in the pool remain pinned for their entire lifetime.

The pool automatically expands its capacity when needed.

§Lifetime management

The pool itself acts as a handle - any clones of it are functionally equivalent, similar to Rc.

When inserting an object into the pool, a handle to the object is returned. The object is removed from the pool when the last remaining handle to the object is dropped (Rc-like behavior).

§Thread safety

The pool is single-threaded.

§Example: unique object ownership

use std::fmt::Display;

use infinity_pool::LocalBlindPool;

let mut pool = LocalBlindPool::new();

// Insert an object into the pool, returning a unique handle to it.
let mut handle = pool.insert("Hello, world!".to_string());

// A unique handle grants the same access as a `&mut` reference to the object.
handle.push_str(" Welcome to Infinity Pool!");

println!("Updated value: {}", &*handle);

// The object is removed when the handle is dropped.

§Example: shared object ownership

use std::fmt::Display;

use infinity_pool::LocalBlindPool;

let mut pool = LocalBlindPool::new();

// Insert an object into the pool, returning a unique handle to it.
let handle = pool.insert("Hello, world!".to_string());

// The unique handle can be converted into a shared handle,
// allowing multiple clones of the handle to be created.
let shared_handle = handle.into_shared();
let shared_handle_clone = shared_handle.clone();

// Shared handles grant the same access as `&` shared references to the object.
println!("Shared access to value: {}", &*shared_handle);

// The object is removed when the last shared handle is dropped.

§Clones of the pool are functionally equivalent

use infinity_pool::LocalBlindPool;

let mut pool1 = LocalBlindPool::new();
let pool2 = pool1.clone();

assert_eq!(pool1.len(), pool2.len());

_ = pool1.insert(42_i32);

assert_eq!(pool1.len(), pool2.len());

Implementations§

Source§

impl LocalBlindPool

Source

pub fn new() -> Self

Creates a new pool with the default configuration.

Source

pub fn len(&self) -> usize

The number of objects currently in the pool.

Source

pub fn capacity_for<T: 'static>(&self) -> usize

The total capacity of the pool for objects of type T.

This is the maximum number of objects (including current contents) that the pool can contain without capacity extension. The pool will automatically extend its capacity if more than this many objects of type T are inserted.

Capacity may be shared between different types of objects.

Source

pub fn is_empty(&self) -> bool

Whether the pool contains zero objects.

Source

pub fn reserve_for<T: 'static>(&self, additional: usize)

Ensures that the pool has capacity for at least additional more objects of type T.

§Panics

Panics if the new capacity would exceed the size of virtual memory (usize::MAX).

Source

pub fn shrink_to_fit(&self)

Drops unused pool capacity to reduce memory usage.

There is no guarantee that any unused capacity can be dropped. The exact outcome depends on the specific pool structure and which objects remain in the pool.

Source

pub fn insert<T: 'static>(&self, value: T) -> LocalBlindPooledMut<T>

Inserts an object into the pool and returns a handle to it.

Source

pub unsafe fn insert_with<T, F>(&self, f: F) -> LocalBlindPooledMut<T>
where T: 'static, F: FnOnce(&mut MaybeUninit<T>),

Inserts an object into the pool via closure and returns a handle to it.

This method allows the caller to partially initialize the object, skipping any MaybeUninit fields that are intentionally not initialized at insertion time. This can make insertion of objects containing MaybeUninit fields faster, although requires unsafe code to implement.

This method is NOT faster than insert() for fully initialized objects. Prefer insert() for a better safety posture if you do not intend to skip initialization of any MaybeUninit fields.

§Example
use std::mem::MaybeUninit;
use std::ptr;

use infinity_pool::LocalBlindPool;

struct DataBuffer {
    id: u32,
    data: MaybeUninit<[u8; 1024]>,
}

let mut pool = LocalBlindPool::new();

// Initialize only the id, leaving data uninitialized for performance.
let handle = unsafe {
    pool.insert_with(|uninit: &mut MaybeUninit<DataBuffer>| {
        let ptr = uninit.as_mut_ptr();

        // SAFETY: We are writing to a correctly located field within the object.
        unsafe {
            ptr::addr_of_mut!((*ptr).id).write(42);
        }
    })
};

assert_eq!(handle.id, 42);
§Safety

The closure must correctly initialize the object. All fields that are not MaybeUninit must be initialized when the closure returns.

Trait Implementations§

Source§

impl Clone for LocalBlindPool

Source§

fn clone(&self) -> LocalBlindPool

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 Debug for LocalBlindPool

Source§

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

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

impl Default for LocalBlindPool

Source§

fn default() -> LocalBlindPool

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> 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, 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> 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.