lifetime-nightmare / src / worker.rs
worker.rs
Raw
use crate::store::{ProductIterator, Store};

pub struct Worker<S> {
    store: S,
}

impl<S> Worker<S> {
    pub fn new(store: S) -> Self {
        Self { store }
    }
}

impl<S> Worker<S>
where
    S: Store,
{
    pub fn run(self, mut handler: impl FnMut(&mut S::Product<'_>)) {
        let Self { mut store } = self;

        let mut products = store.products();

        while let Some(mut product) = products.next() {
            handler(&mut product);
        }
    }

    pub fn run_with_unsafe<'a>(self, mut handler: impl FnMut(&mut S::Product<'a>))
    where
        S: 'a,
    {
        let Self { mut store } = self;

        let mut products = store.products();

        while let Some(mut product) = products.next() {
            // Is it safe ?
            let product_borrow: &mut <S as Store>::Product<'a> =
                unsafe { core::mem::transmute(&mut product) };

            handler(product_borrow);
        }
    }
}