PooledMut

Struct PooledMut 

Source
pub struct PooledMut<T: ?Sized> { /* private fields */ }
Expand description

A mutable reference to a value stored in a BlindPool.

This type provides automatic lifetime management for values in the pool with exclusive access. When the PooledMut instance is dropped, the value is automatically removed from the pool.

Unlike Pooled<T>, this type does not implement Clone and provides exclusive access through DerefMut, making it suitable for scenarios where mutable access is required and shared ownership is not needed.

§Thread Safety

PooledMut<T> implements thread safety traits conditionally based on the stored type T:

  • Send: PooledMut<T> is Send if and only if T is Send. This allows moving pooled mutable references between threads when the referenced type can be moved between threads.

  • Sync: PooledMut<T> does NOT implement Sync because it provides exclusive mutable access via DerefMut. Allowing multiple threads to share references to the same PooledMut<T> instance would violate Rust’s borrowing rules and lead to data races.

§Example

use blind_pool::BlindPool;

let pool = BlindPool::new();
let mut value_handle = pool.insert_mut("Test".to_string());

// Mutably access the value.
value_handle.push_str(" - Modified");
assert_eq!(*value_handle, "Test - Modified");

// Value is automatically cleaned up when handle is dropped.

Implementations§

Source§

impl<T: ?Sized> PooledMut<T>

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_mut("hello".to_string());

let pinned: Pin<&String> = handle.as_pin();
assert_eq!(pinned.len(), 5);
Source

pub fn as_pin_mut(&mut self) -> Pin<&mut T>

Returns a pinned mutable 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<&mut T> without requiring unsafe code.

§Example
use std::pin::Pin;

use blind_pool::BlindPool;

let pool = BlindPool::new();
let mut handle = pool.insert_mut("hello".to_string());

let mut pinned: Pin<&mut String> = handle.as_pin_mut();
// Can use Pin methods or deref to &mut String
Source

pub fn into_shared(self) -> Pooled<T>

Converts this exclusive PooledMut<T> handle into a shared Pooled<T> handle.

This operation consumes the PooledMut<T> and returns a Pooled<T> that allows multiple shared references to the same pooled value. Once converted, you can no longer obtain exclusive (mutable) references to the value, but you can create multiple shared references through cloning.

The returned Pooled<T> maintains the same lifetime management semantics - the value will be automatically removed from the pool when all references (including the returned Pooled<T>) are dropped.

§Example
use blind_pool::BlindPool;

let pool = BlindPool::new();
let mut exclusive_handle = pool.insert_mut("Test".to_string());

// Modify the value while we have exclusive access.
exclusive_handle.push_str(" - Modified");
assert_eq!(*exclusive_handle, "Test - Modified");

// Convert to shared access.
let shared_handle = exclusive_handle.into_shared();

// Now we can create multiple shared references.
let cloned_handle = shared_handle.clone();
assert_eq!(*shared_handle, "Test - Modified");
assert_eq!(*cloned_handle, "Test - Modified");

// Value is automatically cleaned up when all handles are dropped.
Source§

impl<T: Unpin> PooledMut<T>

Source

pub fn into_inner(self) -> T

Moves the value out of the pool, returning it to the caller and consuming the PooledMut<T>.

This method extracts the value from the pool and transfers ownership to the caller. The item is removed from the pool, but the value is not dropped - instead, it is returned to the caller who becomes responsible for its lifetime management.

This method is only available for types that implement Unpin, as moving a value out of the pool would break the pinning guarantee for !Unpin types.

§Example
use blind_pool::BlindPool;

let pool = BlindPool::new();
let mut pooled_string = pool.insert_mut("Hello".to_string());

// Modify the value while it is in the pool.
pooled_string.push_str(" World");
assert_eq!(*pooled_string, "Hello World");
assert_eq!(pool.len(), 1);

// Extract the value from the pool.
let extracted_string = pooled_string.into_inner();
assert_eq!(extracted_string, "Hello World");
assert_eq!(pool.len(), 0); // Pool is now empty.

// The caller now owns the value and is responsible for its lifetime.
println!("Extracted: {}", extracted_string);

Trait Implementations§

Source§

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

Source§

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

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

impl<T: ?Sized> Deref for PooledMut<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_mut("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: ?Sized> DerefMut for PooledMut<T>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Provides direct mutable access to the value stored in the pool.

This allows the handle to be used as if it were a mutable reference to the stored value.

§Example
use blind_pool::BlindPool;

let pool = BlindPool::new();
let mut string_handle = pool.insert_mut("hello".to_string());

// Mutate the string directly.
string_handle.push_str(" world");
assert_eq!(*string_handle, "hello world");
Source§

impl<T: ?Sized> Drop for PooledMut<T>

Source§

fn drop(&mut self)

Automatically removes the item from the pool when the handle is dropped.

This ensures that resources are properly cleaned up without requiring manual intervention.

Source§

impl<T: ?Sized + Send> Send for PooledMut<T>

Auto Trait Implementations§

§

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

§

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

§

impl<T> Sync for PooledMut<T>
where T: Sync + ?Sized,

§

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

§

impl<T> UnwindSafe for PooledMut<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> 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, 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.