roopes_core/patterns/command/
hashable.rs

1//! Contains an implementation of [`Command`]
2//! which requires the instantiation to supply a
3//! method of determining both [`Hash`] and
4//! [`Eq`].  Can be used
5//! with [`observer::HashSubject`].
6
7use crate::prelude::*;
8use std::hash::Hash;
9
10/// Delegates [`Command::execute`] calls to a
11/// delegate command while delegating [`Eq`] and
12/// [`Hash`] to an `id` object.
13#[derive(Debug)]
14pub struct Hashable<D, H>
15where
16    D: Command,
17    H: Hash + Eq,
18{
19    command: D,
20    id: H,
21}
22
23impl<D, H> Hashable<D, H>
24where
25    D: Command,
26    H: Hash + Eq,
27{
28    /// Creates a [`Hashable`] with the specified
29    /// delegate [`Command`] and identifier.
30    pub fn new(
31        command: D,
32        id: H,
33    ) -> Hashable<D, H>
34    {
35        Hashable { command, id }
36    }
37}
38
39impl<D, H> Eq for Hashable<D, H>
40where
41    D: Command,
42    H: Hash + Eq,
43{
44}
45
46impl<D, H> PartialEq<Self> for Hashable<D, H>
47where
48    D: Command,
49    H: Hash + Eq,
50{
51    fn eq(
52        &self,
53        other: &Self,
54    ) -> bool
55    {
56        self.id.eq(&other.id)
57    }
58}
59
60impl<D, H> Hash for Hashable<D, H>
61where
62    D: Command,
63    H: Hash + Eq,
64{
65    fn hash<R: std::hash::Hasher>(
66        &self,
67        state: &mut R,
68    )
69    {
70        self.id.hash(state);
71    }
72}
73
74impl<D, H> Command for Hashable<D, H>
75where
76    D: Command,
77    H: Hash + Eq,
78{
79    fn execute(&self)
80    {
81        self.command.execute();
82    }
83}