topsoil_core/traits/
dynamic_params.rs1use codec::MaxEncodedLen;
13use topsoil_core::Parameter;
14
15pub trait RuntimeParameterStore {
17 type AggregatedKeyValue: AggregatedKeyValue;
18
19 fn get<KV, K>(key: K) -> Option<K::Value>
23 where
24 KV: AggregatedKeyValue,
25 K: Key + Into<<KV as AggregatedKeyValue>::Key>,
26 <KV as AggregatedKeyValue>::Key: IntoKey<
27 <<Self as RuntimeParameterStore>::AggregatedKeyValue as AggregatedKeyValue>::Key,
28 >,
29 <<Self as RuntimeParameterStore>::AggregatedKeyValue as AggregatedKeyValue>::Value:
30 TryIntoKey<<KV as AggregatedKeyValue>::Value>,
31 <KV as AggregatedKeyValue>::Value: TryInto<K::WrappedValue>;
32}
33
34pub trait ParameterStore<KV: AggregatedKeyValue> {
36 fn get<K>(key: K) -> Option<K::Value>
38 where
39 K: Key + Into<<KV as AggregatedKeyValue>::Key>,
40 <KV as AggregatedKeyValue>::Value: TryInto<K::WrappedValue>;
41}
42
43pub trait Key {
45 type Value;
47
48 type WrappedValue: Into<Self::Value>;
50}
51
52pub trait AggregatedKeyValue: Parameter {
54 type Key: Parameter + MaxEncodedLen;
56
57 type Value: Parameter + MaxEncodedLen;
59
60 fn into_parts(self) -> (Self::Key, Option<Self::Value>);
62}
63
64impl AggregatedKeyValue for () {
65 type Key = ();
66 type Value = ();
67
68 fn into_parts(self) -> (Self::Key, Option<Self::Value>) {
69 ((), None)
70 }
71}
72
73pub struct ParameterStoreAdapter<PS, KV>(core::marker::PhantomData<(PS, KV)>);
78
79impl<PS, KV> ParameterStore<KV> for ParameterStoreAdapter<PS, KV>
80where
81 PS: RuntimeParameterStore,
82 KV: AggregatedKeyValue,
83 <KV as AggregatedKeyValue>::Key:
84 IntoKey<<<PS as RuntimeParameterStore>::AggregatedKeyValue as AggregatedKeyValue>::Key>,
85 <KV as AggregatedKeyValue>::Value: TryFromKey<
86 <<PS as RuntimeParameterStore>::AggregatedKeyValue as AggregatedKeyValue>::Value,
87 >,
88{
89 fn get<K>(key: K) -> Option<K::Value>
90 where
91 K: Key + Into<<KV as AggregatedKeyValue>::Key>,
92 <KV as AggregatedKeyValue>::Value: TryInto<K::WrappedValue>,
93 {
94 PS::get::<KV, K>(key)
95 }
96}
97
98mod workaround {
100 pub trait FromKey<T>: Sized {
101 #[must_use]
102 fn from_key(value: T) -> Self;
103 }
104
105 pub trait IntoKey<T>: Sized {
106 #[must_use]
107 fn into_key(self) -> T;
108 }
109
110 impl<T, U> IntoKey<U> for T
111 where
112 U: FromKey<T>,
113 {
114 fn into_key(self) -> U {
115 U::from_key(self)
116 }
117 }
118
119 pub trait TryIntoKey<T>: Sized {
120 type Error;
121
122 fn try_into_key(self) -> Result<T, Self::Error>;
123 }
124
125 pub trait TryFromKey<T>: Sized {
126 type Error;
127
128 fn try_from_key(value: T) -> Result<Self, Self::Error>;
129 }
130
131 impl<T, U> TryIntoKey<U> for T
132 where
133 U: TryFromKey<T>,
134 {
135 type Error = U::Error;
136
137 fn try_into_key(self) -> Result<U, U::Error> {
138 U::try_from_key(self)
139 }
140 }
141}
142pub use workaround::*;