frame_support/traits/
dynamic_params.rs1use codec::MaxEncodedLen;
24use frame_support::Parameter;
25
26pub trait RuntimeParameterStore {
28 type AggregatedKeyValue: AggregatedKeyValue;
29
30 fn get<KV, K>(key: K) -> Option<K::Value>
34 where
35 KV: AggregatedKeyValue,
36 K: Key + Into<<KV as AggregatedKeyValue>::Key>,
37 <KV as AggregatedKeyValue>::Key: IntoKey<
38 <<Self as RuntimeParameterStore>::AggregatedKeyValue as AggregatedKeyValue>::Key,
39 >,
40 <<Self as RuntimeParameterStore>::AggregatedKeyValue as AggregatedKeyValue>::Value:
41 TryIntoKey<<KV as AggregatedKeyValue>::Value>,
42 <KV as AggregatedKeyValue>::Value: TryInto<K::WrappedValue>;
43}
44
45pub trait ParameterStore<KV: AggregatedKeyValue> {
47 fn get<K>(key: K) -> Option<K::Value>
49 where
50 K: Key + Into<<KV as AggregatedKeyValue>::Key>,
51 <KV as AggregatedKeyValue>::Value: TryInto<K::WrappedValue>;
52}
53
54pub trait Key {
56 type Value;
58
59 type WrappedValue: Into<Self::Value>;
61}
62
63pub trait AggregatedKeyValue: Parameter {
65 type Key: Parameter + MaxEncodedLen;
67
68 type Value: Parameter + MaxEncodedLen;
70
71 fn into_parts(self) -> (Self::Key, Option<Self::Value>);
73}
74
75impl AggregatedKeyValue for () {
76 type Key = ();
77 type Value = ();
78
79 fn into_parts(self) -> (Self::Key, Option<Self::Value>) {
80 ((), None)
81 }
82}
83
84pub struct ParameterStoreAdapter<PS, KV>(core::marker::PhantomData<(PS, KV)>);
89
90impl<PS, KV> ParameterStore<KV> for ParameterStoreAdapter<PS, KV>
91where
92 PS: RuntimeParameterStore,
93 KV: AggregatedKeyValue,
94 <KV as AggregatedKeyValue>::Key:
95 IntoKey<<<PS as RuntimeParameterStore>::AggregatedKeyValue as AggregatedKeyValue>::Key>,
96 <KV as AggregatedKeyValue>::Value: TryFromKey<
97 <<PS as RuntimeParameterStore>::AggregatedKeyValue as AggregatedKeyValue>::Value,
98 >,
99{
100 fn get<K>(key: K) -> Option<K::Value>
101 where
102 K: Key + Into<<KV as AggregatedKeyValue>::Key>,
103 <KV as AggregatedKeyValue>::Value: TryInto<K::WrappedValue>,
104 {
105 PS::get::<KV, K>(key)
106 }
107}
108
109mod workaround {
111 pub trait FromKey<T>: Sized {
112 #[must_use]
113 fn from_key(value: T) -> Self;
114 }
115
116 pub trait IntoKey<T>: Sized {
117 #[must_use]
118 fn into_key(self) -> T;
119 }
120
121 impl<T, U> IntoKey<U> for T
122 where
123 U: FromKey<T>,
124 {
125 fn into_key(self) -> U {
126 U::from_key(self)
127 }
128 }
129
130 pub trait TryIntoKey<T>: Sized {
131 type Error;
132
133 fn try_into_key(self) -> Result<T, Self::Error>;
134 }
135
136 pub trait TryFromKey<T>: Sized {
137 type Error;
138
139 fn try_from_key(value: T) -> Result<Self, Self::Error>;
140 }
141
142 impl<T, U> TryIntoKey<U> for T
143 where
144 U: TryFromKey<T>,
145 {
146 type Error = U::Error;
147
148 fn try_into_key(self) -> Result<U, U::Error> {
149 U::try_from_key(self)
150 }
151 }
152}
153pub use workaround::*;