Derive Macro ObjectPool

Source
#[derive(ObjectPool)]
{
    // Attributes available to this derive:
    #[generator]
}
Expand description

Derive macro for ObjectPool trait implementation. Optionally, you can specify a generator function for the pool. If not specified, the trait will try to use Default trait implementation.

Internally, the macro will generate a static variable with the name of #[ident]_OBJECT_POOL (where #[ident] is the name of the type in uppercase) and implement the ObjectPool trait for the type.

§Example

use derivable_object_pool::prelude::*;

#[derive(ObjectPool)]
#[generator(Test2::new_item)]
struct Test2(u32);

impl Test2 {
    fn new_item() -> Self {
        Test2(10)
    }
}

fn main() {
    let mut item = Test2::new();
    item.0 = 20;
    assert_eq!(item.0, 20);
    drop(item);
    assert_eq!(Test2::pool().len(), 1);
}

Generated extra code for the derive macro:

static TEST2_OBJECT_POOL: Pool<Test2> = Pool::new(Test2::new_item);

impl ObjectPool for Test2 {
    #[inline]
    fn pool<'a>() -> &'a Pool<Self> {
        &TEST2_OBJECT_POOL
    }
}

§Attributes

§generator

Specify a generator function for the pool. If not specified, the trait will try to use Default trait implementation.