Skip to main content

better_auth_core/
runtime_extensions.rs

1use std::{
2    any::{Any, TypeId},
3    collections::HashMap,
4    sync::Arc,
5};
6
7/// Typed plugin configuration shared after initialization.
8#[derive(Clone, Default)]
9pub struct RuntimeExtensions(HashMap<TypeId, Arc<dyn Any + Send + Sync>>);
10
11impl RuntimeExtensions {
12    /// Publish configuration for other plugins. The latest value replaces the previous value.
13    pub fn insert<T: Send + Sync + 'static>(&mut self, value: T) {
14        let _ = self.0.insert(TypeId::of::<T>(), Arc::new(value));
15    }
16
17    /// Read configuration published during plugin initialization.
18    pub fn get<T: Send + Sync + 'static>(&self) -> Option<&T> {
19        self.0.get(&TypeId::of::<T>())?.downcast_ref()
20    }
21}