RawPooled

Struct RawPooled 

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

Source

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

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) };
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::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> Clone for RawPooled<T>

Source§

fn clone(&self) -> Self

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<T: ?Sized> Debug for RawPooled<T>

Source§

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

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

impl<T: ?Sized> Deref for RawPooled<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. 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"));
Source§

type Target = T

The resulting type after dereferencing.
Source§

impl<T: ?Sized> Copy for RawPooled<T>

Source§

impl<T: ?Sized + Sync> Send for RawPooled<T>

Source§

impl<T: ?Sized + Sync> Sync for RawPooled<T>

Auto Trait Implementations§

§

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

§

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

§

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

§

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