pub struct RawOpaquePool { /* private fields */ }Expand description
A pool of objects with uniform memory layout.
Stores objects of any type that match a Layout defined at pool creation
time. All values in the pool remain pinned for their entire lifetime.
The pool automatically expands its capacity when needed.
§Thread safety
The pool is single-threaded, though if all the objects inserted are Send then the owner of
the pool is allowed to treat the pool itself as Send (but must do so via a wrapper type that
implements Send using unsafe code).
§Example
use infinity_pool::RawOpaquePool;
fn work_with_displayable<T: std::fmt::Display + 'static + Unpin>(value: T) {
let mut pool = RawOpaquePool::with_layout_of::<T>();
// Insert an object into the pool
let handle = pool.insert(value);
// Access the object through the handle
let stored_value = unsafe { handle.ptr().as_ref() };
println!("Stored: {}", stored_value);
// Explicitly remove the object from the pool
pool.remove_mut(handle);
}
work_with_displayable("Hello, world!");
work_with_displayable(42);Implementations§
Source§impl RawOpaquePool
impl RawOpaquePool
Sourcepub fn builder() -> RawOpaquePoolBuilder
pub fn builder() -> RawOpaquePoolBuilder
Starts configuring and creating a new instance of the pool.
Sourcepub fn with_layout(object_layout: Layout) -> Self
pub fn with_layout(object_layout: Layout) -> Self
Creates a new instance of the pool with the specified layout.
Shorthand for a builder that keeps all other options at their default values.
§Panics
Panics if the layout is zero-sized.
Sourcepub fn with_layout_of<T: Sized>() -> Self
pub fn with_layout_of<T: Sized>() -> Self
Creates a new instance of the pool with the layout of T.
Shorthand for a builder that keeps all other options at their default values.
§Panics
Panics if T is a zero-sized type.
Sourcepub fn object_layout(&self) -> Layout
pub fn object_layout(&self) -> Layout
The layout of objects stored in this pool.
All inserted objects must match this layout.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
The total capacity of the pool.
This is the maximum number of objects (including current contents) that the pool can contain without capacity extension. The pool will automatically extend its capacity if more than this many objects are inserted.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Ensures that the pool has capacity for at least additional more objects.
§Panics
Panics if the new capacity would exceed the size of virtual memory (usize::MAX).
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Drops unused pool capacity to reduce memory usage.
There is no guarantee that any unused capacity can be dropped. The exact outcome depends on the specific pool structure and which objects remain in the pool.
Sourcepub fn insert<T>(&mut self, value: T) -> RawPooledMut<T>
pub fn insert<T>(&mut self, value: T) -> RawPooledMut<T>
Inserts an object into the pool and returns a handle to it.
§Panics
Panics if the layout of T does not match the object layout of the pool.
§Example
use std::alloc::Layout;
use infinity_pool::RawOpaquePool;
let mut pool = RawOpaquePool::with_layout(Layout::new::<String>());
// Insert an object into the pool
let mut handle = pool.insert("Hello".to_string());
// Mutate the object via the unique handle
// SAFETY: The handle is valid and points to a properly initialized String
unsafe {
handle.as_mut().push_str(", Raw Opaque World!");
assert_eq!(handle.as_ref(), "Hello, Raw Opaque World!");
}
// Transform the unique handle into a shared handle
let shared_handle = handle.into_shared();
// After transformation, you can only immutably dereference the object
// SAFETY: The shared handle is valid and points to a properly initialized String
unsafe {
assert_eq!(shared_handle.as_ref(), "Hello, Raw Opaque World!");
// shared_handle.as_mut(); // This would not compile
}
// Explicitly remove the object from the pool
// SAFETY: The handle belongs to this pool and references a valid object
unsafe {
pool.remove(shared_handle);
}
assert_eq!(pool.len(), 0);Sourcepub unsafe fn insert_unchecked<T>(&mut self, value: T) -> RawPooledMut<T>
pub unsafe fn insert_unchecked<T>(&mut self, value: T) -> RawPooledMut<T>
Inserts an object into the pool and returns a handle to it.
§Safety
The caller must ensure that the layout of T matches the pool’s object layout.
Sourcepub unsafe fn insert_with<T, F>(&mut self, f: F) -> RawPooledMut<T>where
F: FnOnce(&mut MaybeUninit<T>),
pub unsafe fn insert_with<T, F>(&mut self, f: F) -> RawPooledMut<T>where
F: FnOnce(&mut MaybeUninit<T>),
Inserts an object into the pool via closure and returns a handle to it.
This method allows the caller to partially initialize the object, skipping any MaybeUninit
fields that are intentionally not initialized at insertion time. This can make insertion of
objects containing MaybeUninit fields faster, although requires unsafe code to implement.
This method is NOT faster than insert() for fully initialized objects.
Prefer insert() for a better safety posture if you do not intend to
skip initialization of any MaybeUninit fields.
§Example
use std::mem::MaybeUninit;
use infinity_pool::RawOpaquePool;
struct DataBuffer {
id: u32,
data: MaybeUninit<[u8; 1024]>, // Large buffer to skip initializing
}
let mut pool = RawOpaquePool::with_layout_of::<DataBuffer>();
// Initialize only the id, leaving data uninitialized for performance
let handle = unsafe {
pool.insert_with(|uninit: &mut MaybeUninit<DataBuffer>| {
let ptr = uninit.as_mut_ptr();
// SAFETY: Writing to the id field within allocated space
unsafe {
std::ptr::addr_of_mut!((*ptr).id).write(42);
// data field is intentionally left uninitialized
}
})
};
// ID is accessible, data remains uninitialized
let id = unsafe { std::ptr::addr_of!(handle.ptr().as_ref().id).read() };
assert_eq!(id, 42);§Panics
Panics if the layout of T does not match the object layout of the pool.
§Safety
The closure must correctly initialize the object. All fields that
are not MaybeUninit must be initialized when the closure returns.
Sourcepub unsafe fn insert_with_unchecked<T, F>(&mut self, f: F) -> RawPooledMut<T>where
F: FnOnce(&mut MaybeUninit<T>),
pub unsafe fn insert_with_unchecked<T, F>(&mut self, f: F) -> RawPooledMut<T>where
F: FnOnce(&mut MaybeUninit<T>),
Inserts an object into the pool via closure and returns a handle to it.
This method allows the caller to partially initialize the object, skipping any MaybeUninit
fields that are intentionally not initialized at insertion time. This can make insertion of
objects containing MaybeUninit fields faster, although requires unsafe code to implement.
This method is NOT faster than insert() for fully initialized objects.
Prefer insert() for a better safety posture if you do not intend to
skip initialization of any MaybeUninit fields.
§Example
use std::mem::MaybeUninit;
use infinity_pool::RawOpaquePool;
struct DataBuffer {
id: u32,
data: MaybeUninit<[u8; 1024]>, // Large buffer to skip initializing
}
let mut pool = RawOpaquePool::with_layout_of::<DataBuffer>();
// Initialize only the id, leaving data uninitialized for performance
let handle = unsafe {
pool.insert_with_unchecked(|uninit: &mut MaybeUninit<DataBuffer>| {
let ptr = uninit.as_mut_ptr();
// SAFETY: Writing to the id field within allocated space
unsafe {
std::ptr::addr_of_mut!((*ptr).id).write(42);
// data field is intentionally left uninitialized
}
})
};
// ID is accessible, data remains uninitialized
let id = unsafe { std::ptr::addr_of!(handle.ptr().as_ref().id).read() };
assert_eq!(id, 42);§Safety
The caller must ensure that the layout of T matches the pool’s object layout.
The closure must correctly initialize the object. All fields that
are not MaybeUninit must be initialized when the closure returns.
Sourcepub fn remove_mut<T: ?Sized>(&mut self, handle: RawPooledMut<T>)
pub fn remove_mut<T: ?Sized>(&mut self, handle: RawPooledMut<T>)
Removes an object from the pool, dropping it.
§Panics
Panics if the handle does not reference an object in this pool.
Sourcepub fn remove_mut_unpin<T: Unpin>(&mut self, handle: RawPooledMut<T>) -> T
pub fn remove_mut_unpin<T: Unpin>(&mut self, handle: RawPooledMut<T>) -> T
Removes an object from the pool and returns it.
§Panics
Panics if the handle does not reference an object in this pool.
Panics if the handle has been type-erased (T is ()).
Sourcepub unsafe fn remove_unpin<T: Unpin>(&mut self, handle: RawPooled<T>) -> T
pub unsafe fn remove_unpin<T: Unpin>(&mut self, handle: RawPooled<T>) -> T
Removes an object from the pool and returns it.
§Panics
Panics if the handle does not reference an existing object in this pool.
Panics if the handle has been type-erased (T is ()).
§Safety
The caller must ensure that the handle belongs to this pool and that the object it references has not already been removed from the pool.
Sourcepub fn iter(&self) -> RawOpaquePoolIterator<'_> ⓘ
pub fn iter(&self) -> RawOpaquePoolIterator<'_> ⓘ
Returns an iterator over all objects in the pool.
The iterator yields untyped pointers (NonNull<()>) to the objects stored in the pool.
It is the caller’s responsibility to cast these pointers to the appropriate type.
Trait Implementations§
Source§impl Debug for RawOpaquePool
impl Debug for RawOpaquePool
Source§impl<'p> IntoIterator for &'p RawOpaquePool
impl<'p> IntoIterator for &'p RawOpaquePool
Auto Trait Implementations§
impl Freeze for RawOpaquePool
impl RefUnwindSafe for RawOpaquePool
impl !Send for RawOpaquePool
impl !Sync for RawOpaquePool
impl Unpin for RawOpaquePool
impl UnwindSafe for RawOpaquePool
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.