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.
let value1 = &*pooled; // Safe deref access
let value2 = &*pooled_copy; // Safe deref access
let value3 = &*pooled_clone; // Safe deref access
assert_eq!(value1, "Hello");
assert_eq!(value2, "Hello");
assert_eq!(value3, "Hello");
// To remove the item from the pool, any handle can be used.
unsafe { 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.
let value = *pooled; // Safe deref access
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 value safely.
// 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.
unsafe { pool.remove(&erased) };Sourcepub fn as_pin(&self) -> Pin<&T>
pub fn as_pin(&self) -> Pin<&T>
Returns a pinned reference to the value stored in the pool.
Since values in the pool are always pinned (they never move once inserted),
this method provides safe access to Pin<&T> without requiring unsafe code.
§Example
use std::pin::Pin;
use blind_pool::RawBlindPool;
let mut pool = RawBlindPool::new();
let handle = unsafe {
pool.insert_with(|uninit| {
uninit.write("hello".to_string());
})
};
let pinned: Pin<&String> = handle.as_pin();
assert_eq!(pinned.len(), 5);Trait Implementations§
Source§impl<T: ?Sized> Deref for RawPooled<T>
impl<T: ?Sized> Deref for RawPooled<T>
Source§fn deref(&self) -> &Self::Target
fn deref(&self) -> &Self::Target
Provides direct access to the value stored in the pool.
This allows the handle to be used as if it were a reference to the stored value.
Since RawPooled<T> provides shared access, this returns a shared reference.
§Example
use blind_pool::RawBlindPool;
let mut pool = RawBlindPool::new();
let string_handle = unsafe {
pool.insert_with(|uninit| {
uninit.write("hello".to_string());
})
};
// Access string methods directly.
assert_eq!(string_handle.len(), 5);
assert!(string_handle.starts_with("he"));