dvcompute_branch/simulation/simulation/
ops.rs

1// Copyright (c) 2020-2022  David Sorokin <davsor@mail.ru>, based in Yoshkar-Ola, Russia
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
7use crate::simulation::simulation::*;
8
9use dvcompute_utils::grc::Grc;
10
11/// It represents the source of `Simulation` computations.
12#[must_use = "computations are lazy and do nothing unless to be run"]
13#[derive(Clone)]
14pub struct SimulationFn<T> where T: 'static {
15    gen: Grc<Box<dyn Fn() -> SimulationBox<T>>>
16}
17
18impl<T> SimulationFn<T> {
19
20    /// Create a new source of computations.
21    #[inline]
22    pub fn new<F, M>(f: F) -> Self
23        where F: Fn() -> M + 'static,
24              M: Simulation<Item = T> + Clone + 'static
25    {
26        SimulationFn {
27            gen: Grc::new(Box::new(move || { f().into_boxed() }))
28        }
29    }
30
31    /// Get the next computation.
32    #[inline]
33    pub fn next(&self) -> SimulationBox<T> {
34        (self.gen)()
35    }
36}