peace_rt_model_core/params/
params_keys.rs

1use std::{fmt::Debug, hash::Hash, marker::PhantomData};
2
3use serde::{de::DeserializeOwned, Deserialize, Serialize};
4
5/// Marks the types used for params keys.
6///
7/// # Design
8///
9/// This allows types such as `CmdContext` and `ParamsTypeRegs` to have a
10/// `ParamsKeys` type parameter without specifying all of the associated type
11/// bounds. This means:
12///
13/// * The code for those types is more understandable.
14/// * We reduce the ripple effect of needing each of these associated types
15///   propagated to callers who use those types in type / method signatures.
16pub trait ParamsKeys: Debug + Unpin + 'static {
17    type WorkspaceParamsKMaybe: KeyMaybe;
18    type ProfileParamsKMaybe: KeyMaybe;
19    type FlowParamsKMaybe: KeyMaybe;
20}
21
22/// Shorter name for `ParamsKeys` without any known keys.
23pub type ParamsKeysUnknown = ParamsKeysImpl<KeyUnknown, KeyUnknown, KeyUnknown>;
24
25/// Concrete implementation of `ParamsKeys`.
26#[derive(Debug)]
27pub struct ParamsKeysImpl<WorkspaceParamsKMaybe, ProfileParamsKMaybe, FlowParamsKMaybe> {
28    /// Marker
29    marker: PhantomData<(WorkspaceParamsKMaybe, ProfileParamsKMaybe, FlowParamsKMaybe)>,
30}
31
32impl<WorkspaceParamsKMaybe, ProfileParamsKMaybe, FlowParamsKMaybe>
33    ParamsKeysImpl<WorkspaceParamsKMaybe, ProfileParamsKMaybe, FlowParamsKMaybe>
34{
35    /// Returns a new `ParamsKeysImpl`.
36    pub fn new() -> Self {
37        Self::default()
38    }
39}
40
41impl<WorkspaceParamsKMaybe, ProfileParamsKMaybe, FlowParamsKMaybe> Default
42    for ParamsKeysImpl<WorkspaceParamsKMaybe, ProfileParamsKMaybe, FlowParamsKMaybe>
43{
44    fn default() -> Self {
45        Self {
46            marker: PhantomData,
47        }
48    }
49}
50
51impl<WorkspaceParamsKMaybe, ProfileParamsKMaybe, FlowParamsKMaybe> ParamsKeys
52    for ParamsKeysImpl<WorkspaceParamsKMaybe, ProfileParamsKMaybe, FlowParamsKMaybe>
53where
54    WorkspaceParamsKMaybe: KeyMaybe,
55    ProfileParamsKMaybe: KeyMaybe,
56    FlowParamsKMaybe: KeyMaybe,
57{
58    type FlowParamsKMaybe = FlowParamsKMaybe;
59    type ProfileParamsKMaybe = ProfileParamsKMaybe;
60    type WorkspaceParamsKMaybe = WorkspaceParamsKMaybe;
61}
62
63// Supporting types that allow keys to not be explicitly specified
64#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
65pub struct KeyUnknown;
66
67#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
68pub struct KeyKnown<K>(PhantomData<K>);
69
70pub trait KeyMaybe: Debug + Unpin + 'static {
71    type Key: Clone + Debug + Eq + Hash + DeserializeOwned + Serialize + Send + Sync + 'static;
72}
73
74impl KeyMaybe for KeyUnknown {
75    type Key = ();
76}
77
78impl<K> KeyMaybe for KeyKnown<K>
79where
80    K: Clone + Debug + Eq + Hash + DeserializeOwned + Serialize + Send + Sync + Unpin + 'static,
81{
82    type Key = K;
83}