Pooled

Struct Pooled 

Source
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> is Send if and only if T is Sync. This allows moving pooled references between threads when the referenced type supports concurrent access.

  • Sync: Pooled<T> is Sync if and only if T is Sync. This allows sharing the same Pooled<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>

Source

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.
Source

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");
Source

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>

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl<T: ?Sized> Debug for Pooled<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: ?Sized> Deref for Pooled<T>

Source§

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"));
Source§

type Target = T

The resulting type after dereferencing.
Source§

impl<T: Sync> Send for Pooled<T>

Source§

impl<T: Sync> Sync for Pooled<T>

Auto Trait Implementations§

§

impl<T> Freeze for Pooled<T>
where T: ?Sized,

§

impl<T> RefUnwindSafe for Pooled<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> Unpin for Pooled<T>
where T: ?Sized,

§

impl<T> UnwindSafe for Pooled<T>
where T: RefUnwindSafe + ?Sized,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.