roopes_core/primitives/transformer/
heap.rs

1//! This module contains types which store a
2//! [`Transformer`] on the heap.  This
3//! is particularly useful to allow for
4//! non-uniform-sized [`Transformer`] types.
5//! The type erasure provided by `dyn` also allows
6//! [`Transformer`]s to be non-uniform in a
7//! collection so long as they implement for the
8//! same generic types.
9
10use super::Transformer;
11use delegate::delegate;
12
13/// Stores an indirected [`Transformer`] in a
14/// [`Box`] for later use.
15pub struct Heap<I, O>
16{
17    delegate: Box<dyn Transformer<I, O>>,
18}
19
20impl<I, O> Heap<I, O>
21{
22    /// Creates a new [`Heap`] which contains a
23    /// [`Box`]ed [`Transformer`].
24    #[must_use]
25    pub fn new(delegate: Box<dyn Transformer<I, O>>) -> Heap<I, O>
26    {
27        Heap { delegate }
28    }
29}
30
31#[allow(clippy::inline_always)]
32impl<I, O> Transformer<I, O> for Heap<I, O>
33{
34    delegate! {
35        to self.delegate {
36           fn transform(&self, input: &I) -> O;
37        }
38    }
39}