RawBlindPool

Struct RawBlindPool 

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

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

§Thread safety

The pool is single-threaded, though if all the objects inserted are Send then the owner of the pool is allowed to treat the pool itself as Send (but must do so via a wrapper type that implements Send using unsafe code).

§Example

use infinity_pool::RawBlindPool;

fn work_with_displayable<T: std::fmt::Display + Unpin>(value: T) {
    let mut pool = RawBlindPool::new();

    // Insert an object into the pool (type determined at runtime)
    let handle = pool.insert(value);

    // Access the object through the handle
    let stored_value = unsafe { handle.ptr().as_ref() };
    println!("Stored: {}", stored_value);

    // Explicitly remove the object from the pool
    pool.remove_mut(handle);
}

work_with_displayable("Hello, Raw Blind!");
work_with_displayable(42);

Implementations§

Source§

impl RawBlindPool

Source

pub fn builder() -> RawBlindPoolBuilder

Starts configuring and creating a new instance of the pool.

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>(&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>(&mut 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(&mut 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>(&mut self, value: T) -> RawBlindPooledMut<T>

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

§Example
use infinity_pool::RawBlindPool;

let mut pool = RawBlindPool::new();

// Insert an object into the pool
let mut handle = pool.insert("Hello".to_string());

// Mutate the object via the unique handle
// SAFETY: The handle is valid and points to a properly initialized String
unsafe {
    handle.as_mut().push_str(", Raw Blind World!");
    assert_eq!(handle.as_ref(), "Hello, Raw Blind World!");
}

// Transform the unique handle into a shared handle
let shared_handle = handle.into_shared();

// After transformation, you can only immutably dereference the object
// SAFETY: The shared handle is valid and points to a properly initialized String
unsafe {
    assert_eq!(shared_handle.as_ref(), "Hello, Raw Blind World!");
    // shared_handle.as_mut(); // This would not compile
}

// Explicitly remove the object from the pool
// SAFETY: The handle belongs to this pool and references a valid object
unsafe {
    pool.remove(shared_handle);
}
assert_eq!(pool.len(), 0);
Source

pub unsafe fn insert_with<T, F>(&mut self, f: F) -> RawBlindPooledMut<T>
where 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 infinity_pool::RawBlindPool;

struct DataBuffer {
    id: u32,
    data: MaybeUninit<[u8; 1024]>, // Large buffer to skip initializing
}

let mut pool = RawBlindPool::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: Writing to the id field within allocated space
        unsafe {
            std::ptr::addr_of_mut!((*ptr).id).write(42);
            // data field is intentionally left uninitialized
        }
    })
};

// ID is accessible, data remains uninitialized
let id = unsafe { std::ptr::addr_of!(handle.ptr().as_ref().id).read() };
assert_eq!(id, 42);
§Safety

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

Source

pub fn remove_mut<T: ?Sized>(&mut self, handle: RawBlindPooledMut<T>)

Removes an object from the pool, dropping it.

§Panics

Panics if the handle does not reference an object in this pool.

Source

pub unsafe fn remove<T: ?Sized>(&mut self, handle: RawBlindPooled<T>)

Removes an object from the pool, dropping it.

§Panics

Panics if the handle does not reference an object in this pool.

§Safety

The caller must ensure that the handle belongs to this pool and that the object it references has not already been removed from the pool.

Source

pub fn remove_mut_unpin<T: Unpin>(&mut self, handle: RawBlindPooledMut<T>) -> T

Removes an object from the pool and returns it.

§Panics

Panics if the handle does not reference an object in this pool.

Source

pub unsafe fn remove_unpin<T: Unpin>(&mut self, handle: RawBlindPooled<T>) -> T

Removes an object from the pool and returns it.

§Panics

Panics if the handle does not reference an existing object in this pool.

§Safety

The caller must ensure that the handle belongs to this pool and that the object it references has not already been removed from the pool.

Trait Implementations§

Source§

impl Debug for RawBlindPool

Source§

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

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

impl Default for RawBlindPool

Source§

fn default() -> Self

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