sp_externalities/
extensions.rs1use crate::Error;
26use alloc::{
27 boxed::Box,
28 collections::btree_map::{BTreeMap, Entry},
29};
30use core::{
31 any::{Any, TypeId},
32 iter::FromIterator,
33 ops::DerefMut,
34};
35
36#[derive(Clone, Copy, Debug, PartialEq, Eq)]
38pub enum TransactionType {
39 Host,
41 Runtime,
43}
44
45impl TransactionType {
46 pub fn is_host(self) -> bool {
48 matches!(self, Self::Host)
49 }
50
51 pub fn is_runtime(self) -> bool {
53 matches!(self, Self::Runtime)
54 }
55}
56
57pub trait Extension: Send + 'static {
63 fn as_mut_any(&mut self) -> &mut dyn Any;
67
68 fn type_id(&self) -> TypeId;
70
71 fn start_transaction(&mut self, ty: TransactionType) {
73 let _ty = ty;
74 }
75
76 fn commit_transaction(&mut self, ty: TransactionType) {
78 let _ty = ty;
79 }
80
81 fn rollback_transaction(&mut self, ty: TransactionType) {
83 let _ty = ty;
84 }
85}
86
87impl Extension for Box<dyn Extension> {
88 fn as_mut_any(&mut self) -> &mut dyn Any {
89 (**self).as_mut_any()
90 }
91
92 fn type_id(&self) -> TypeId {
93 (**self).type_id()
94 }
95
96 fn start_transaction(&mut self, ty: TransactionType) {
97 (**self).start_transaction(ty);
98 }
99
100 fn commit_transaction(&mut self, ty: TransactionType) {
101 (**self).commit_transaction(ty);
102 }
103
104 fn rollback_transaction(&mut self, ty: TransactionType) {
105 (**self).rollback_transaction(ty);
106 }
107}
108
109#[macro_export]
142macro_rules! decl_extension {
143 (
144 $( #[ $attr:meta ] )*
145 $vis:vis struct $ext_name:ident ($inner:ty);
146 $(
147 impl $ext_name_impl:ident {
148 $(
149 $impls:tt
150 )*
151 }
152 )*
153 ) => {
154 $( #[ $attr ] )*
155 $vis struct $ext_name (pub $inner);
156
157 impl $crate::Extension for $ext_name {
158 fn as_mut_any(&mut self) -> &mut dyn core::any::Any {
159 self
160 }
161
162 fn type_id(&self) -> core::any::TypeId {
163 core::any::Any::type_id(self)
164 }
165
166 $(
167 $(
168 $impls
169 )*
170 )*
171 }
172
173 impl $ext_name {
174 #[allow(dead_code)]
176 pub fn type_id() -> core::any::TypeId {
177 core::any::TypeId::of::<Self>()
178 }
179 }
180
181 impl core::ops::Deref for $ext_name {
182 type Target = $inner;
183
184 fn deref(&self) -> &Self::Target {
185 &self.0
186 }
187 }
188
189 impl core::ops::DerefMut for $ext_name {
190 fn deref_mut(&mut self) -> &mut Self::Target {
191 &mut self.0
192 }
193 }
194
195 impl From<$inner> for $ext_name {
196 fn from(inner: $inner) -> Self {
197 Self(inner)
198 }
199 }
200 };
201 (
202 $( #[ $attr:meta ] )*
203 $vis:vis struct $ext_name:ident;
204 ) => {
205 $( #[ $attr ] )*
206 $vis struct $ext_name;
207
208 impl $crate::Extension for $ext_name {
209 fn as_mut_any(&mut self) -> &mut dyn core::any::Any {
210 self
211 }
212
213 fn type_id(&self) -> core::any::TypeId {
214 core::any::Any::type_id(self)
215 }
216 }
217
218 impl $ext_name {
219 #[allow(dead_code)]
221 pub fn type_id() -> core::any::TypeId {
222 core::any::TypeId::of::<Self>()
223 }
224 }
225 }
226}
227
228pub trait ExtensionStore {
232 fn extension_by_type_id(&mut self, type_id: TypeId) -> Option<&mut dyn Any>;
238
239 fn register_extension_with_type_id(
243 &mut self,
244 type_id: TypeId,
245 extension: Box<dyn Extension>,
246 ) -> Result<(), Error>;
247
248 fn deregister_extension_by_type_id(&mut self, type_id: TypeId) -> Result<(), Error>;
252}
253
254#[derive(Default)]
256pub struct Extensions {
257 extensions: BTreeMap<TypeId, Box<dyn Extension>>,
258}
259
260impl core::fmt::Debug for Extensions {
261 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
262 write!(f, "Extensions: ({})", self.extensions.len())
263 }
264}
265
266impl Extensions {
267 pub fn new() -> Self {
269 Self::default()
270 }
271
272 pub fn register<E: Extension>(&mut self, ext: E) {
274 let type_id = ext.type_id();
275 self.extensions.insert(type_id, Box::new(ext));
276 }
277
278 pub fn is_registered(&self, type_id: TypeId) -> bool {
280 self.extensions.contains_key(&type_id)
281 }
282
283 pub fn register_with_type_id(
285 &mut self,
286 type_id: TypeId,
287 extension: Box<dyn Extension>,
288 ) -> Result<(), Error> {
289 match self.extensions.entry(type_id) {
290 Entry::Vacant(vacant) => {
291 vacant.insert(extension);
292 Ok(())
293 },
294 Entry::Occupied(_) => Err(Error::ExtensionAlreadyRegistered),
295 }
296 }
297
298 pub fn get_mut(&mut self, ext_type_id: TypeId) -> Option<&mut dyn Any> {
300 self.extensions
301 .get_mut(&ext_type_id)
302 .map(DerefMut::deref_mut)
303 .map(Extension::as_mut_any)
304 }
305
306 pub fn deregister(&mut self, type_id: TypeId) -> bool {
310 self.extensions.remove(&type_id).is_some()
311 }
312
313 pub fn iter_mut(&mut self) -> impl Iterator<Item = (&TypeId, &mut Box<dyn Extension>)> {
315 self.extensions.iter_mut()
316 }
317
318 pub fn merge(&mut self, other: Self) {
323 self.extensions.extend(other.extensions);
324 }
325
326 pub fn start_transaction(&mut self, ty: TransactionType) {
328 self.extensions.values_mut().for_each(|e| e.start_transaction(ty));
329 }
330
331 pub fn commit_transaction(&mut self, ty: TransactionType) {
333 self.extensions.values_mut().for_each(|e| e.commit_transaction(ty));
334 }
335
336 pub fn rollback_transaction(&mut self, ty: TransactionType) {
338 self.extensions.values_mut().for_each(|e| e.rollback_transaction(ty));
339 }
340
341 pub fn into_extensions(self) -> impl Iterator<Item = Box<dyn Extension>> {
343 self.extensions.into_values()
344 }
345}
346
347impl Extend<Extensions> for Extensions {
348 fn extend<T: IntoIterator<Item = Extensions>>(&mut self, iter: T) {
349 iter.into_iter()
350 .for_each(|ext| self.extensions.extend(ext.extensions.into_iter()));
351 }
352}
353
354impl<A: Extension> From<A> for Extensions {
355 fn from(ext: A) -> Self {
356 Self {
357 extensions: FromIterator::from_iter(
358 [(Extension::type_id(&ext), Box::new(ext) as Box<dyn Extension>)].into_iter(),
359 ),
360 }
361 }
362}
363
364impl<A: Extension, B: Extension> From<(A, B)> for Extensions {
365 fn from((ext, ext2): (A, B)) -> Self {
366 Self {
367 extensions: FromIterator::from_iter(
368 [
369 (Extension::type_id(&ext), Box::new(ext) as Box<dyn Extension>),
370 (Extension::type_id(&ext2), Box::new(ext2) as Box<dyn Extension>),
371 ]
372 .into_iter(),
373 ),
374 }
375 }
376}
377
378impl<A: Extension, B: Extension, C: Extension> From<(A, B, C)> for Extensions {
379 fn from((ext, ext2, ext3): (A, B, C)) -> Self {
380 Self {
381 extensions: FromIterator::from_iter(
382 [
383 (Extension::type_id(&ext), Box::new(ext) as Box<dyn Extension>),
384 (Extension::type_id(&ext2), Box::new(ext2) as Box<dyn Extension>),
385 (Extension::type_id(&ext3), Box::new(ext3) as Box<dyn Extension>),
386 ]
387 .into_iter(),
388 ),
389 }
390 }
391}
392
393#[cfg(test)]
394mod tests {
395 use super::*;
396
397 decl_extension! {
398 struct DummyExt(u32);
399 }
400 decl_extension! {
401 struct DummyExt2(u32);
402 }
403
404 #[test]
405 fn register_and_retrieve_extension() {
406 let mut exts = Extensions::new();
407 exts.register(DummyExt(1));
408 exts.register(DummyExt2(2));
409
410 let ext = exts.get_mut(TypeId::of::<DummyExt>()).expect("Extension is registered");
411 let ext_ty = ext.downcast_mut::<DummyExt>().expect("Downcasting works");
412
413 assert_eq!(ext_ty.0, 1);
414 }
415
416 #[test]
417 fn register_box_extension() {
418 let mut exts = Extensions::new();
419 let box1: Box<dyn Extension> = Box::new(DummyExt(1));
420 let box2: Box<dyn Extension> = Box::new(DummyExt2(2));
421 exts.register(box1);
422 exts.register(box2);
423
424 {
425 let ext = exts.get_mut(TypeId::of::<DummyExt>()).expect("Extension 1 is registered");
426 let ext_ty = ext.downcast_mut::<DummyExt>().expect("Downcasting works for Extension 1");
427 assert_eq!(ext_ty.0, 1);
428 }
429 {
430 let ext2 = exts.get_mut(TypeId::of::<DummyExt2>()).expect("Extension 2 is registered");
431 let ext_ty2 =
432 ext2.downcast_mut::<DummyExt2>().expect("Downcasting works for Extension 2");
433 assert_eq!(ext_ty2.0, 2);
434 }
435 }
436
437 #[test]
438 fn from_boxed_extensions() {
439 let exts = Extensions::from((DummyExt(1), DummyExt2(2)));
440
441 assert!(exts.is_registered(DummyExt::type_id()));
442 assert!(exts.is_registered(DummyExt2::type_id()));
443 }
444}