LocalBlindPool

Struct LocalBlindPool 

Source
pub struct LocalBlindPool { /* private fields */ }
Expand description

A single-threaded wrapper around RawBlindPool that provides automatic resource management and reference counting.

This type acts as a cloneable handle to a shared RawBlindPool instance. Multiple handles can exist simultaneously, and the underlying pool remains alive as long as at least one handle exists.

Items inserted into the pool are automatically removed when all references to them are dropped, eliminating the need for manual resource management.

§Single-threaded Design

This type is designed for single-threaded use and is neither Send nor Sync. For multi-threaded scenarios, use crate::BlindPool instead.

§Example

use blind_pool::LocalBlindPool;

let pool = LocalBlindPool::new();

// Clone the pool handle for use in different parts of the code.
let pool_clone = pool.clone();

let item_handle = pool_clone.insert("Test".to_string());
assert_eq!(*item_handle, "Test".to_string());

Implementations§

Source§

impl LocalBlindPool

Source

pub fn new() -> Self

Creates a new LocalBlindPool with default configuration.

This is the equivalent of creating a raw pool and wrapping it in single-threaded management.

§Example
use blind_pool::LocalBlindPool;

let pool = LocalBlindPool::new();

let string1_handle = pool.insert("Test".to_string());
let string2_handle = pool.insert("hello".to_string());

// Access values through dereferencing.
assert_eq!(*string1_handle, "Test".to_string());
assert_eq!(*string2_handle, "hello");
Source

pub fn insert<T: 'static>(&self, value: T) -> LocalPooled<T>

Inserts a value into the pool and returns a handle to access it.

The returned handle automatically manages the lifetime of the inserted value. When all handles to the value are dropped, the value is automatically removed from the pool.

§Example
use blind_pool::LocalBlindPool;

let pool = LocalBlindPool::new();

let string1_handle = pool.insert("Test".to_string());
let string2_handle = pool.insert("hello".to_string());

// Access values through dereferencing.
assert_eq!(*string1_handle, "Test".to_string());
assert_eq!(*string2_handle, "hello");
Source

pub unsafe fn insert_with<T: 'static>( &self, f: impl FnOnce(&mut MaybeUninit<T>), ) -> LocalPooled<T>

Inserts a value into the pool using in-place initialization and returns a handle to it.

This allows the caller to initialize the item in-place using a closure that receives a &mut MaybeUninit<T>. This can be more efficient than constructing the value separately and then moving it into the pool, especially for large or complex types.

The returned handle automatically manages the lifetime of the inserted value. When all handles to the value are dropped, the value is automatically removed from the pool.

§Example
use std::mem::MaybeUninit;

use blind_pool::LocalBlindPool;

let pool = LocalBlindPool::new();

// SAFETY: We properly initialize the value in the closure.
let handle = unsafe {
    pool.insert_with(|uninit: &mut MaybeUninit<String>| {
        uninit.write(String::from("Hello, World!"));
    })
};

// Access value through dereferencing.
assert_eq!(*handle, "Hello, World!");
§Safety

The closure must properly initialize the MaybeUninit<T> before returning.

Source

pub fn insert_mut<T: 'static>(&self, value: T) -> LocalPooledMut<T>

Inserts a value into the pool and returns a mutable handle to access it.

Unlike insert(), this method returns a LocalPooledMut<T> that provides exclusive mutable access to the value and does not implement Clone. This is suitable for scenarios where you need to modify the value and don’t require shared ownership.

The returned handle automatically manages the lifetime of the inserted value. When the handle is dropped, the value is automatically removed from the pool.

§Example
use blind_pool::LocalBlindPool;

let pool = LocalBlindPool::new();

let mut string_handle = pool.insert_mut("Test".to_string());

// Mutate the value directly.
string_handle.push_str(" - Modified");
assert_eq!(*string_handle, "Test - Modified");
Source

pub unsafe fn insert_with_mut<T: 'static>( &self, f: impl FnOnce(&mut MaybeUninit<T>), ) -> LocalPooledMut<T>

Inserts a value into the pool using in-place initialization and returns a mutable handle to it.

This allows the caller to initialize the item in-place using a closure that receives a &mut MaybeUninit<T>. This can be more efficient than constructing the value separately and then moving it into the pool, especially for large or complex types.

Unlike insert_with(), this method returns a LocalPooledMut<T> that provides exclusive mutable access to the value and does not implement Clone.

The returned handle automatically manages the lifetime of the inserted value. When the handle is dropped, the value is automatically removed from the pool.

§Example
use std::mem::MaybeUninit;

use blind_pool::LocalBlindPool;

let pool = LocalBlindPool::new();

// SAFETY: We properly initialize the value in the closure.
let mut handle = unsafe {
    pool.insert_with_mut(|uninit: &mut MaybeUninit<String>| {
        uninit.write(String::from("Hello, World!"));
    })
};

// Mutate the value directly.
handle.push_str(" - Modified");
assert_eq!(*handle, "Hello, World! - Modified");
§Safety

The closure must properly initialize the MaybeUninit<T> before returning.

Source

pub fn len(&self) -> usize

Returns the total number of items currently stored in the pool.

§Example
use blind_pool::LocalBlindPool;

let pool = LocalBlindPool::new();

assert_eq!(pool.len(), 0);

let _item1 = pool.insert("Hello".to_string());
let _item2 = pool.insert("hello".to_string());

assert_eq!(pool.len(), 2);
Source

pub fn is_empty(&self) -> bool

Returns whether the pool has no inserted values.

§Example
use blind_pool::LocalBlindPool;

let pool = LocalBlindPool::new();

assert!(pool.is_empty());

let item = pool.insert("Test".to_string());
assert!(!pool.is_empty());

drop(item);
assert!(pool.is_empty());

Trait Implementations§

Source§

impl Clone for LocalBlindPool

Source§

fn clone(&self) -> LocalBlindPool

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LocalBlindPool

Source§

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

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

impl Default for LocalBlindPool

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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