pub struct RawPooled<T: ?Sized> { /* private fields */ }Expand description
A handle representing an item stored in a RawBlindPool.
Acts as a super-powered pointer that can be copied and cloned freely. This provides access to the stored item and can be used to remove the item from the pool.
Being Copy and Clone, this type behaves like a regular pointer - you can duplicate
handles freely without affecting the underlying stored value. Multiple copies of the same
handle all refer to the same stored value.
§Example
use blind_pool::RawBlindPool;
let mut pool = RawBlindPool::new();
let pooled = pool.insert("Hello".to_string());
// The handle acts like a super-powered pointer - it can be copied freely.
let pooled_copy = pooled;
let pooled_clone = pooled.clone();
// All copies refer to the same stored value.
// SAFETY: All pointers are valid and point to the same value.
let value1 = unsafe { pooled.ptr().as_ref() };
let value2 = unsafe { pooled_copy.ptr().as_ref() };
let value3 = unsafe { pooled_clone.ptr().as_ref() };
assert_eq!(value1, "Hello");
assert_eq!(value2, "Hello");
assert_eq!(value3, "Hello");
// To remove the item from the pool, any handle can be used.
pool.remove(&pooled);§Thread safety
This type is thread-safe (Send + Sync) if and only if T implements Sync.
When T is Sync, multiple threads can safely share handles to the same data.
When T is not Sync, the handle is single-threaded and cannot be moved between threads
or shared between threads, preventing unsafe access to non-thread-safe data.
Implementations§
Source§impl<T: ?Sized> RawPooled<T>
impl<T: ?Sized> RawPooled<T>
Sourcepub fn ptr(&self) -> NonNull<T>
pub fn ptr(&self) -> NonNull<T>
Returns a pointer to the inserted value.
This is the only way to access the value stored in the pool. The owner of the handle has
exclusive access to the value and may both read and write and may create both & shared
and &mut exclusive references to the item.
§Example
use blind_pool::RawBlindPool;
let mut pool = RawBlindPool::new();
let pooled = pool.insert(2.5159_f64);
// Read data back from the memory.
// SAFETY: The pointer is valid and the memory contains the value we just inserted.
let value = unsafe { *pooled.ptr().as_ref() };
assert_eq!(value, 2.5159);Sourcepub fn erase(self) -> RawPooled<()>
pub fn erase(self) -> RawPooled<()>
Erases the type information from this RawPooled<T> handle,
returning a RawPooled<()>.
This is useful when you want to store handles of different types in the same collection or pass them to code that doesn’t need to know the specific type.
The handle remains functionally equivalent and can still be used to remove the item from the pool and drop it. The only change is the removal of the type information.
§Example
use blind_pool::RawBlindPool;
let mut pool = RawBlindPool::new();
let pooled = pool.insert("Test".to_string());
// Erase type information.
let erased = pooled.erase();
// Can still access the raw pointer.
// SAFETY: We know this contains a String.
let value = unsafe { erased.ptr().cast::<String>().as_ref() };
assert_eq!(value.as_str(), "Test");
// Can still remove the item.
pool.remove(&erased);