Skip to main content

infinity_pool/builders/
blind_raw_builder.rs

1use std::marker::PhantomData;
2
3use crate::{DropPolicy, RawBlindPool};
4
5/// Creates an instance of [`RawBlindPool`].
6#[derive(Debug, Default)]
7#[must_use]
8pub struct RawBlindPoolBuilder {
9    drop_policy: DropPolicy,
10
11    _not_send: PhantomData<*const ()>,
12}
13
14impl RawBlindPoolBuilder {
15    /// Creates a new instance of the builder with the default options.
16    pub fn new() -> Self {
17        Self::default()
18    }
19
20    /// Defines the drop policy for the pool. Optional.
21    pub fn drop_policy(mut self, drop_policy: DropPolicy) -> Self {
22        self.drop_policy = drop_policy;
23        self
24    }
25
26    /// Validates the options and creates the pool.
27    #[must_use]
28    pub fn build(self) -> RawBlindPool {
29        RawBlindPool::new_inner(self.drop_policy)
30    }
31}
32
33#[cfg(test)]
34#[cfg_attr(coverage_nightly, coverage(off))]
35mod tests {
36    use static_assertions::assert_not_impl_any;
37
38    use super::*;
39
40    assert_not_impl_any!(RawBlindPoolBuilder: Send, Sync);
41
42    #[test]
43    fn builder_creates_functional_pool() {
44        let pool = RawBlindPoolBuilder::new().build();
45
46        assert_eq!(pool.len(), 0);
47        assert!(pool.is_empty());
48        assert_eq!(pool.capacity_for::<u32>(), 0);
49    }
50
51    #[test]
52    fn builder_default_works() {
53        let pool = RawBlindPoolBuilder::default().build();
54
55        assert_eq!(pool.len(), 0);
56        assert!(pool.is_empty());
57        assert_eq!(pool.capacity_for::<String>(), 0);
58    }
59
60    #[test]
61    fn builder_with_may_drop_contents_policy() {
62        // Test that MayDropContents allows pool to be dropped with items still in it
63        let mut pool = RawBlindPoolBuilder::new()
64            .drop_policy(DropPolicy::MayDropContents)
65            .build();
66
67        // Insert some items and deliberately DO NOT remove them
68        let _handle1 = pool.insert("test1".to_string());
69        let _handle2 = pool.insert(42_u32);
70
71        assert_eq!(pool.len(), 2);
72
73        // Pool should be dropped successfully even with items still in it
74        // (This is the whole point of MayDropContents policy)
75        drop(pool);
76    }
77
78    #[test]
79    #[should_panic]
80    fn builder_with_must_not_drop_contents_policy() {
81        // Test that MustNotDropContents panics when pool is dropped with items
82        let mut pool = RawBlindPoolBuilder::new()
83            .drop_policy(DropPolicy::MustNotDropContents)
84            .build();
85
86        // Insert an item and deliberately DO NOT remove it
87        let _handle = pool.insert(100_i64);
88        assert_eq!(pool.len(), 1);
89
90        // Pool should panic when dropped with items still in it
91        // (This is the whole point of MustNotDropContents policy)
92        drop(pool);
93    }
94}