Trait Factory

Source
pub trait Factory<T>: Send
where T: Send,
{ // Required method fn produce(&mut self) -> Box<dyn Future<Output = T> + Unpin + Send + '_>; }
Expand description

The factory trait is used to populate the Pool when items are created and replaced. There is a default implementation of factory for boxed sync closures, and creating an asynchronous factory is shown in this example:

use std::future::Future;
use lazy_pool::Factory;

struct AsyncFactory {}

struct AnyObject {
    member: String,
}

impl AsyncFactory {
    async fn get_instance(&self) -> AnyObject {
        AnyObject {
            member: String::from("hello"),
        }
    }
}

impl Factory<AnyObject> for AsyncFactory {
    fn produce(&mut self) -> Box<dyn Future<Output = AnyObject> + Send + Unpin + '_> {
        Box::new(Box::pin(self.get_instance()))
    }
}

Required Methods§

Source

fn produce(&mut self) -> Box<dyn Future<Output = T> + Unpin + Send + '_>

Implementors§

Source§

impl<T> Factory<T> for SyncFactory<T>
where T: Send + 'static,