fire_http/
resources.rs

1use std::any::{Any, TypeId};
2use std::collections::HashMap;
3use std::sync::Arc;
4
5#[derive(Clone)]
6pub struct Resources {
7	inner: Arc<HashMap<TypeId, Box<dyn Any + Send + Sync>>>,
8}
9
10impl Resources {
11	pub(crate) fn new() -> Self {
12		Self {
13			inner: Arc::new(HashMap::new()),
14		}
15	}
16
17	pub fn exists<R>(&self) -> bool
18	where
19		R: Any,
20	{
21		self.inner.contains_key(&TypeId::of::<R>())
22	}
23
24	/// returns true if the data already existed
25	pub(crate) fn insert<R>(&mut self, data: R) -> bool
26	where
27		R: Any + Send + Sync,
28	{
29		let map = Arc::get_mut(&mut self.inner).unwrap();
30		map.insert(data.type_id(), Box::new(data)).is_some()
31	}
32
33	pub fn get<R>(&self) -> Option<&R>
34	where
35		R: Any,
36	{
37		self.inner
38			.get(&TypeId::of::<R>())
39			.and_then(|a| a.downcast_ref())
40	}
41}
42
43#[cfg(feature = "graphql")]
44impl juniper::Context for Resources {}