pub struct RawBlindPool { /* private fields */ }Expand description
A pinned object pool of unbounded size that accepts objects of any type.
The pool returns a RawPooled<T> for each inserted value, which acts as a super-powered
pointer that can be copied and cloned freely. Each handle provides direct access to the
inserted item via a pointer.
§Out of band access
The collection does not create or keep references to the memory blocks. The only way to access
the contents of the collection is via unsafe code by using the pointer from a RawPooled<T>.
The collection does not create or maintain any & shared or &mut exclusive references to
the items it contains, except when explicitly called to operate on an item (e.g. remove()
implies exclusive access).
§Resource usage
The collection automatically grows as items are added. To reduce memory usage after items have
been removed, use the shrink_to_fit() method to release unused capacity.
§Example
use blind_pool::RawBlindPool;
let mut pool = RawBlindPool::new();
// Insert values of different types.
let pooled_string1 = pool.insert("Hello".to_string());
let pooled_string2 = pool.insert("World".to_string());
// Read from the memory using safe deref access.
let value_string1 = &*pooled_string1;
let value_string2 = &*pooled_string2;
assert_eq!(value_string1, "Hello");
assert_eq!(value_string2, "World");§Thread safety
This type is thread-mobile (Send) but not thread-safe (Sync). It can be moved
between threads but cannot be shared between threads simultaneously. For thread-safe
pool operations, use BlindPool instead.
Implementations§
Source§impl RawBlindPool
impl RawBlindPool
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new RawBlindPool with default configuration.
For custom configuration, use RawBlindPool::builder().
§Example
use blind_pool::RawBlindPool;
let mut pool = RawBlindPool::new();
let pooled = pool.insert("Test".to_string());
let value = &*pooled; // Safe deref access
assert_eq!(value.as_str(), "Test");Sourcepub fn builder() -> RawBlindPoolBuilder
pub fn builder() -> RawBlindPoolBuilder
Creates a builder for configuring and constructing a RawBlindPool.
§Example
use blind_pool::{DropPolicy, RawBlindPool};
let pool = RawBlindPool::builder()
.drop_policy(DropPolicy::MustNotDropItems)
.build();Sourcepub fn insert<T>(&mut self, value: T) -> RawPooled<T>
pub fn insert<T>(&mut self, value: T) -> RawPooled<T>
Inserts a value into the pool and returns a handle to access it.
The pool stores the value and provides a handle for later access or removal.
The caller must ensure that if T contains any references or other lifetime-dependent
data, those lifetimes are valid for the entire duration that the value may remain in
the pool. Since access to pool contents is only possible through unsafe code, the caller
is responsible for ensuring that no use-after-free conditions occur.
§Example
use blind_pool::RawBlindPool;
let mut pool = RawBlindPool::new();
// Insert different types into the same pool.
let _pooled_string = pool.insert("Hello".to_string());
let _pooled_float = pool.insert(2.5_f64);
let _pooled_string = pool.insert("hello".to_string());
// All values are stored in the same BlindPool.
assert_eq!(pool.len(), 3);Sourcepub unsafe fn insert_with<T>(
&mut self,
f: impl FnOnce(&mut MaybeUninit<T>),
) -> RawPooled<T>
pub unsafe fn insert_with<T>( &mut self, f: impl FnOnce(&mut MaybeUninit<T>), ) -> RawPooled<T>
Inserts a value into the pool using in-place initialization and returns a handle to it.
This method is designed for partial object initialization, where you want to construct
an object directly in its final memory location. This can provide significant
performance benefits compared to insert() by avoiding temporary allocations
and unnecessary moves, especially for large or complex types.
The pool stores the initialized value and provides a handle for later access or removal.
§Example
use std::mem::MaybeUninit;
use blind_pool::RawBlindPool;
let mut pool = RawBlindPool::new();
// Partial initialization - build complex object directly in pool memory.
// SAFETY: We properly initialize the value in the closure.
let pooled = unsafe {
pool.insert_with(|uninit: &mut MaybeUninit<Vec<u64>>| {
let mut vec = Vec::with_capacity(1000);
vec.extend(0..100);
uninit.write(vec);
})
};
// Read the value back using safe deref access.
let value = &*pooled;
assert_eq!(value.len(), 100);
// Clean up.
unsafe { pool.remove(&pooled) };§Safety
The caller must ensure that:
- The closure properly initializes the
MaybeUninit<T>before returning. - If
Tcontains any references or other lifetime-dependent data, those lifetimes are valid for the entire duration that the value may remain in the pool. Since access to pool contents is only possible through unsafe code, the caller is responsible for ensuring that no use-after-free conditions occur.
Sourcepub fn insert_mut<T>(&mut self, value: T) -> RawPooledMut<T>
pub fn insert_mut<T>(&mut self, value: T) -> RawPooledMut<T>
Inserts a value into the pool and returns an exclusive handle to it.
Unlike insert(), this method returns a RawPooledMut<T> that provides exclusive
ownership guarantees. The handle cannot be copied or cloned, ensuring that only one
handle can exist for each pool item. This enables safe removal methods that consume
the handle, preventing double-use bugs.
§Example
use blind_pool::RawBlindPool;
let mut pool = RawBlindPool::new();
// Insert with exclusive ownership.
let pooled_mut = pool.insert_mut("Test".to_string());
// Safe removal that consumes the handle.
let extracted = pool.remove_unpin_mut(pooled_mut);
assert_eq!(extracted, "Test");Sourcepub unsafe fn insert_with_mut<T>(
&mut self,
f: impl FnOnce(&mut MaybeUninit<T>),
) -> RawPooledMut<T>
pub unsafe fn insert_with_mut<T>( &mut self, f: impl FnOnce(&mut MaybeUninit<T>), ) -> RawPooledMut<T>
Inserts a value into the pool using in-place initialization and returns an exclusive handle.
This method is designed for partial object initialization with exclusive ownership.
It returns a RawPooledMut<T> that provides exclusive ownership guarantees.
The handle cannot be copied or cloned, ensuring that only one handle can exist
for each pool item.
§Example
use std::mem::MaybeUninit;
use blind_pool::RawBlindPool;
let mut pool = RawBlindPool::new();
// Partial initialization with exclusive ownership.
// SAFETY: We properly initialize the value in the closure.
let pooled_mut = unsafe {
pool.insert_with_mut(|uninit: &mut MaybeUninit<Vec<u64>>| {
let mut vec = Vec::with_capacity(1000);
vec.extend(0..100);
uninit.write(vec);
})
};
// Safe removal that consumes the handle.
let extracted = pool.remove_unpin_mut(pooled_mut);
assert_eq!(extracted.len(), 100);§Safety
The caller must ensure that:
- The closure properly initializes the
MaybeUninit<T>before returning. - If
Tcontains any references or other lifetime-dependent data, those lifetimes are valid for the entire duration that the value may remain in the pool.
Sourcepub unsafe fn remove<T: ?Sized>(&mut self, pooled: &RawPooled<T>)
pub unsafe fn remove<T: ?Sized>(&mut self, pooled: &RawPooled<T>)
Removes a value from the pool and drops it.
The RawPooled<T> handle is consumed and the memory is returned to the pool.
The value is dropped.
§Panics
Panics if the provided handle does not belong to this pool.
§Example
use blind_pool::RawBlindPool;
let mut pool = RawBlindPool::new();
let _pooled = pool.insert("Test".to_string());
assert_eq!(pool.len(), 1);
unsafe { pool.remove(&_pooled) };
assert_eq!(pool.len(), 0);§Safety
The caller must guarantee that the pooled handle has not been used for removal before. Using the same pooled handle multiple times may result in undefined behavior.
Sourcepub unsafe fn remove_unpin<T: Unpin>(&mut self, pooled: &RawPooled<T>) -> T
pub unsafe fn remove_unpin<T: Unpin>(&mut self, pooled: &RawPooled<T>) -> T
Removes a value from the pool and returns it.
The RawPooled<T> handle is consumed and the memory is returned to the pool.
The value is moved out and returned to the caller.
§Panics
Panics if the provided handle does not belong to this pool.
§Example
use blind_pool::RawBlindPool;
let mut pool = RawBlindPool::new();
let _pooled = pool.insert("Test".to_string());
assert_eq!(pool.len(), 1);
let extracted = unsafe { pool.remove_unpin(&_pooled) };
assert_eq!(extracted, "Test");
assert_eq!(pool.len(), 0);§Safety
The caller must guarantee that the pooled handle has not been used for removal before.
Sourcepub fn remove_mut<T: ?Sized>(&mut self, pooled_mut: RawPooledMut<T>)
pub fn remove_mut<T: ?Sized>(&mut self, pooled_mut: RawPooledMut<T>)
Removes a value from the pool using an exclusive handle and drops it.
The RawPooledMut<T> handle is consumed and the memory is returned to the pool.
The value is dropped. This is safe because the exclusive handle guarantees that
it cannot be used multiple times.
§Panics
Panics if the provided handle does not belong to this pool.
§Example
use blind_pool::RawBlindPool;
let mut pool = RawBlindPool::new();
let pooled_mut = pool.insert_mut("Test".to_string());
assert_eq!(pool.len(), 1);
pool.remove_mut(pooled_mut); // Safe - no double-use possible
assert_eq!(pool.len(), 0);Sourcepub fn remove_unpin_mut<T: Unpin>(&mut self, pooled_mut: RawPooledMut<T>) -> T
pub fn remove_unpin_mut<T: Unpin>(&mut self, pooled_mut: RawPooledMut<T>) -> T
Removes a value from the pool using an exclusive handle and returns it.
The RawPooledMut<T> handle is consumed and the memory is returned to the pool.
The value is moved out and returned to the caller. This is safe because the
exclusive handle guarantees that it cannot be used multiple times.
§Panics
Panics if the provided handle does not belong to this pool.
§Example
use blind_pool::RawBlindPool;
let mut pool = RawBlindPool::new();
let pooled_mut = pool.insert_mut("Test".to_string());
assert_eq!(pool.len(), 1);
let extracted = pool.remove_unpin_mut(pooled_mut); // Safe - no double-use possible
assert_eq!(extracted, "Test");
assert_eq!(pool.len(), 0);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the total number of items stored in the pool.
§Example
use blind_pool::RawBlindPool;
let mut pool = RawBlindPool::new();
assert_eq!(pool.len(), 0);
let _a = pool.insert("Hello".to_string());
let _b = pool.insert("world".to_string());
let _c = pool.insert(2.5_f64);
assert_eq!(pool.len(), 3);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether the pool has no inserted values.
An empty pool may still be holding unused memory capacity.
§Example
use blind_pool::RawBlindPool;
let mut pool = RawBlindPool::new();
assert!(pool.is_empty());
let _pooled = pool.insert("Test".to_string());
assert!(!pool.is_empty());
unsafe { pool.remove(&_pooled) };
assert!(pool.is_empty());Sourcepub fn capacity_of<T>(&self) -> usize
pub fn capacity_of<T>(&self) -> usize
Returns the capacity for items of type T.
This is the number of items of type T that can be stored without allocating more memory.
If no items of type T have been inserted yet, returns 0.
§Example
use blind_pool::RawBlindPool;
let mut pool = RawBlindPool::new();
// Initially no capacity is allocated for any type.
assert_eq!(pool.capacity_of::<u32>(), 0);
assert_eq!(pool.capacity_of::<f64>(), 0);
// Inserting a String allocates capacity for String but not f64.
let _pooled = pool.insert("Test".to_string());
assert!(pool.capacity_of::<String>() > 0);
assert_eq!(pool.capacity_of::<f64>(), 0);Sourcepub fn reserve_for<T>(&mut self, additional: usize)
pub fn reserve_for<T>(&mut self, additional: usize)
Reserves capacity for at least additional more items of type T.
The pool may reserve more space to speculatively avoid frequent reallocations.
After calling reserve_for, the capacity for type T will be greater than or equal to
the current count of T items plus additional. Does nothing if capacity is already
sufficient.
If no items of type T have been inserted yet, this creates an internal pool for type T
and reserves the requested capacity.
§Example
use blind_pool::RawBlindPool;
let mut pool = RawBlindPool::new();
// Reserve space for 10 u32 values specifically
pool.reserve_for::<u32>(10);
assert!(pool.capacity_of::<u32>() >= 10);
assert_eq!(pool.capacity_of::<f64>(), 0); // Other types unaffected
// Insert String values - should not need to allocate more capacity
let _pooled = pool.insert("Test".to_string());
assert!(pool.capacity_of::<String>() >= 10);Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the pool to fit its current size.
This can help reduce memory usage after items have been removed from the pool.
§Example
use blind_pool::RawBlindPool;
let mut pool = RawBlindPool::new();
// Insert many items to allocate capacity.
for i in 0..1000 {
pool.insert(i);
}
// Remove all items but keep the allocated capacity.
while !pool.is_empty() {
// In a real scenario you'd keep track of handles to remove them properly.
// This is just for the example.
break;
}
// Shrink to fit the current size.
pool.shrink_to_fit();