roopes_core/patterns/command/
heap.rs

1//! Provides a heap-based implementation of
2//! [`Command`].
3
4use crate::{
5    prelude::*,
6    primitives,
7};
8use delegate::delegate;
9
10/// Stores a delegate [`Command`] in a [`Box`] for
11/// later use.
12pub struct Heap
13{
14    delegate: Box<dyn Command>,
15}
16
17impl Heap
18{
19    /// Creates a new [`Heap`] with the supplied
20    /// delegate.
21    #[must_use]
22    pub fn new(delegate: Box<dyn Command>) -> Heap
23    {
24        Heap { delegate }
25    }
26}
27
28#[allow(clippy::inline_always)]
29impl Command for Heap
30{
31    delegate! {
32        to self.delegate {
33           fn execute(&self);
34        }
35    }
36}
37
38impl From<Heap> for Box<dyn Command>
39{
40    fn from(value: Heap) -> Self
41    {
42        value.delegate
43    }
44}
45
46impl From<Box<dyn Command>> for Heap
47{
48    fn from(delegate: Box<dyn Command>) -> Self
49    {
50        Heap { delegate }
51    }
52}
53
54impl<D> From<D> for Heap
55where
56    D: primitives::executable::lambda::Delegate + 'static,
57{
58    fn from(delegate: D) -> Self
59    {
60        let delegate =
61            Box::new(CommandExecutable::new(executable::Lambda::new(delegate)));
62
63        Heap { delegate }
64    }
65}