Skip to main content

reinhardt_di/
factory_output.rs

1//! Output wrapper for keyed injectable provider functions.
2
3use crate::InjectableKey;
4use std::marker::PhantomData;
5use std::ops::Deref;
6
7/// Registered output of a keyed provider function.
8///
9/// The DI registry keys this type by `TypeId::of::<FactoryOutput<K, T>>()`.
10#[derive(Clone, Debug, PartialEq, Eq)]
11pub struct FactoryOutput<K, T>
12where
13	K: InjectableKey,
14	T: Send + Sync + 'static,
15{
16	value: T,
17	_key: PhantomData<fn() -> K>,
18}
19
20impl<K, T> FactoryOutput<K, T>
21where
22	K: InjectableKey,
23	T: Send + Sync + 'static,
24{
25	/// Build a keyed provider output from its value.
26	pub fn new(value: T) -> Self {
27		Self {
28			value,
29			_key: PhantomData,
30		}
31	}
32
33	/// Consume the output and return the wrapped value.
34	pub fn into_inner(self) -> T {
35		self.value
36	}
37}
38
39impl<K, T> Deref for FactoryOutput<K, T>
40where
41	K: InjectableKey,
42	T: Send + Sync + 'static,
43{
44	type Target = T;
45
46	fn deref(&self) -> &Self::Target {
47		&self.value
48	}
49}
50
51impl<K, T> AsRef<T> for FactoryOutput<K, T>
52where
53	K: InjectableKey,
54	T: Send + Sync + 'static,
55{
56	fn as_ref(&self) -> &T {
57		&self.value
58	}
59}