pub struct Pooled<T: ?Sized> { /* private fields */ }Expand description
A reference to a value stored in a BlindPool.
This type provides automatic lifetime management for values in the pool.
When the last Pooled instance for a value is dropped, the value
is automatically removed from the pool.
Multiple Pooled instances can reference the same value through
cloning, implementing reference counting semantics.
§Thread Safety
Pooled<T> implements thread safety traits conditionally based on the stored type T:
-
Send:
Pooled<T>isSendif and only ifTisSync. This allows moving pooled references between threads when the referenced type supports concurrent access. -
Sync:
Pooled<T>isSyncif and only ifTisSync. This allows sharing the samePooled<T>instance between multiple threads when the referenced type supports concurrent access.
§Trait Objects
You can convert to trait objects using the standard dereferencing approach:
use blind_pool::BlindPool;
trait MyTrait {
fn do_something(&self);
}
struct MyStruct(u32);
impl MyTrait for MyStruct {
fn do_something(&self) {
println!("Doing something with value: {}", self.0);
}
}
let pool = BlindPool::new();
let handle = pool.insert(MyStruct(42));
// Convert to trait object using standard dereferencing
let trait_ref: &dyn MyTrait = &*handle;
trait_ref.do_something();§Example
use blind_pool::BlindPool;
let pool = BlindPool::new();
let value_handle = pool.insert("Test".to_string());
// Access the value through dereferencing.
assert_eq!(*value_handle, "Test".to_string());
// Clone to create additional references.
let cloned_handle = value_handle.clone();
assert_eq!(*cloned_handle, "Test".to_string());Implementations§
Source§impl<T: ?Sized> Pooled<T>
impl<T: ?Sized> Pooled<T>
Sourcepub fn erase(self) -> Pooled<()>
pub fn erase(self) -> Pooled<()>
Erases the type information from this Pooled<T> handle,
returning a Pooled<()>.
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 returned handle shares the same underlying reference count as the original handle. Multiple handles (both typed and type-erased) can coexist for the same pooled item, and the item will only be removed from the pool when all handles are dropped.
§Example
use blind_pool::BlindPool;
let pool = BlindPool::new();
let value_handle = pool.insert("Test".to_string());
let cloned_handle = value_handle.clone();
// Erase type information while keeping the original handle via clone.
let erased = value_handle.erase();
// Both handles are valid and refer to the same item.
assert_eq!(*cloned_handle, "Test".to_string());
// The erased handle shares the same reference count.
drop(erased);
assert_eq!(*cloned_handle, "Test".to_string()); // Still accessible via typed handle.Sourcepub fn ptr(&self) -> NonNull<T>
pub fn ptr(&self) -> NonNull<T>
Returns a pointer to the stored value.
This provides direct access to the underlying pointer while maintaining the safety
guarantees of the pooled reference. The pointer remains valid as long as any
Pooled<T> handle exists for the same value.
§Example
use blind_pool::BlindPool;
let pool = BlindPool::new();
let value_handle = pool.insert("Test".to_string());
// Get the pointer to the stored value.
let ptr = value_handle.ptr();
// SAFETY: The pointer is valid as long as value_handle exists.
let value = unsafe { ptr.as_ref() };
assert_eq!(value, "Test");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::BlindPool;
let pool = BlindPool::new();
let handle = pool.insert("hello".to_string());
let pinned: Pin<&String> = handle.as_pin();
assert_eq!(pinned.len(), 5);Trait Implementations§
Source§impl<T: ?Sized> Clone for Pooled<T>
impl<T: ?Sized> Clone for Pooled<T>
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Creates another handle to the same pooled value.
This increases the reference count for the underlying value. The value will only be removed from the pool when all cloned handles are dropped.
§Example
use blind_pool::BlindPool;
let pool = BlindPool::new();
let value_handle = pool.insert("Test".to_string());
let cloned_handle = value_handle.clone();
// Both handles refer to the same value.
assert_eq!(*value_handle, *cloned_handle);
// Value remains in pool until all handles are dropped.
drop(value_handle);
assert_eq!(*cloned_handle, "Test".to_string()); // Still accessible.1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: ?Sized> Deref for Pooled<T>
impl<T: ?Sized> Deref for Pooled<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.
§Example
use blind_pool::BlindPool;
let pool = BlindPool::new();
let string_handle = pool.insert("hello".to_string());
// Access string methods directly.
assert_eq!(string_handle.len(), 5);
assert!(string_handle.starts_with("he"));