Skip to main content

better_auth_core/plugin/
extensions.rs

1use super::*;
2/// Typed settings and callbacks published during plugin initialization.
3///
4/// Registration is complete before requests begin; readers share immutable
5/// values rather than interpreting configuration through JSON metadata.
6#[derive(Clone, Default)]
7pub struct ContextExtensions(HashMap<TypeId, Arc<dyn Any + Send + Sync>>);
8
9impl ContextExtensions {
10    pub fn insert<T: Any + Send + Sync>(&mut self, value: T) {
11        drop(self.0.insert(TypeId::of::<T>(), Arc::new(value)));
12    }
13
14    #[must_use]
15    pub fn get<T: Any + Send + Sync>(&self) -> Option<Arc<T>> {
16        Arc::clone(self.0.get(&TypeId::of::<T>())?).downcast().ok()
17    }
18}
19
20impl std::fmt::Debug for ContextExtensions {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        f.debug_struct("ContextExtensions").finish_non_exhaustive()
23    }
24}