pinned_pool 0.1.2

An object pool that guarantees pinning of its items and enables easy item access via unsafe code by not maintaining any Rust references to its items
Documentation
use std::marker::PhantomData;

use crate::{DropPolicy, PinnedPool};

/// Builder for creating an instance of [`PinnedPool`].
///
/// You only need to use this builder if you want to customize the pool configuration.
/// The default configuration used by [`PinnedPool::new()`][1] is sufficient for most use cases.
///
/// # Examples
///
/// ```
/// use pinned_pool::{DropPolicy, PinnedPool};
///
/// let pool = PinnedPool::<u32>::builder()
///     .drop_policy(DropPolicy::MayDropItems)
///     .build();
/// ```
///
/// [1]: PinnedPool::new
#[derive(Debug)]
#[must_use]
pub struct PinnedPoolBuilder<T> {
    drop_policy: DropPolicy,

    _item: PhantomData<T>,
}

impl<T> PinnedPoolBuilder<T> {
    pub(crate) fn new() -> Self {
        Self {
            drop_policy: DropPolicy::default(),
            _item: PhantomData,
        }
    }

    /// Sets the [drop policy][DropPolicy] for the pool. This governs how
    /// to treat remaining items in the pool when the pool is dropped.
    ///
    /// # Examples
    ///
    /// ```
    /// use pinned_pool::{DropPolicy, PinnedPool};
    ///
    /// let pool = PinnedPool::<u32>::builder()
    ///     .drop_policy(DropPolicy::MustNotDropItems)
    ///     .build();
    /// ```
    pub fn drop_policy(mut self, policy: DropPolicy) -> Self {
        self.drop_policy = policy;
        self
    }

    /// Builds the pinned pool with the specified configuration.
    ///
    /// # Panics
    ///
    /// Panics if `T` is zero-sized.
    ///
    /// # Examples
    ///
    /// ```
    /// use pinned_pool::PinnedPool;
    ///
    /// let pool = PinnedPool::<u32>::builder().build();
    /// ```
    #[must_use]
    pub fn build(self) -> PinnedPool<T> {
        PinnedPool::new_inner(self.drop_policy)
    }
}