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.
// SAFETY: The pointers are valid and the memory contains the values we just inserted.
let value_string1 = unsafe { pooled_string1.ptr().as_ref() };
let value_string2 = unsafe { pooled_string2.ptr().as_ref() };
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());
// SAFETY: The pointer is valid and contains the value we just inserted.
let value = unsafe { pooled.ptr().as_ref() };
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 allows the caller to initialize the item in-place using a closure that receives
a &mut MaybeUninit<T>. This can be more efficient than constructing the value
separately and then moving it into the pool, 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();
// SAFETY: We properly initialize the value in the closure.
let pooled = unsafe {
pool.insert_with(|uninit: &mut MaybeUninit<String>| {
uninit.write(String::from("Hello, World!"));
})
};
// Read the value back.
// SAFETY: The pointer is valid and contains the initialized String.
let value = unsafe { pooled.ptr().as_ref() };
assert_eq!(value, "Hello, World!");
// Clean up.
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 remove<T: ?Sized>(&mut self, pooled: &RawPooled<T>)
pub 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);
pool.remove(&pooled);
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());
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();