ConcurrentHandleAllocator

Struct ConcurrentHandleAllocator 

Source
pub struct ConcurrentHandleAllocator<T> { /* private fields */ }
Expand description

A thread-safe handle allocator using parking_lot primitives.

This allocator allows concurrent read access and synchronized write access. Use this when handles need to be shared across threads.

§Performance Characteristics

  • Reads (get): Uses RwLock, multiple concurrent readers allowed
  • Writes (allocate, deallocate): Briefly locks a mutex for the free list

§Example

use std::sync::Arc;

let allocator = Arc::new(ConcurrentHandleAllocator::new());

// Clone for another thread
let alloc_clone = Arc::clone(&allocator);
std::thread::spawn(move || {
    let handle = alloc_clone.allocate(42);
    // ...
});

Implementations§

Source§

impl<T> ConcurrentHandleAllocator<T>

Source

pub fn new() -> Self

Creates a new empty concurrent allocator.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a concurrent allocator with pre-allocated capacity.

Source

pub fn allocate(&self, value: T) -> Handle<T>

Allocates a handle for the given value.

This operation briefly locks the free list and entries for writing.

Source

pub fn deallocate(&self, handle: Handle<T>) -> Option<T>

Deallocates a handle, returning the value if the handle was valid.

Source

pub fn get(&self, handle: Handle<T>) -> Option<ValueRef<'_, T>>

Gets a reference to the value, if the handle is valid.

This operation takes a read lock and returns a guard.

Source

pub fn is_valid(&self, handle: Handle<T>) -> bool

Checks if a handle is valid.

Source

pub fn len(&self) -> usize

Returns the number of allocated handles.

Source

pub fn is_empty(&self) -> bool

Returns true if no handles are allocated.

Source

pub fn clear(&self)

Clears all entries, deallocating all handles.

Trait Implementations§

Source§

impl<T: Debug> Debug for ConcurrentHandleAllocator<T>

Source§

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

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

impl<T> Default for ConcurrentHandleAllocator<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.