simple_di/macros.rs
1/// Inject trait implementation from the DI container.
2///
3/// # Panics
4/// Panics if the trait is not registered in the container.
5#[macro_export]
6macro_rules! inject {
7 ($trait:path) => {
8 $crate::__private::inject_unsized::<dyn $trait + Send + Sync>()
9 };
10}
11
12/// Inject trait implementation from the DI container.
13///
14/// Returns `None` if the trait is not registered in the container.
15#[macro_export]
16macro_rules! inject_optional {
17 ($trait:path) => {
18 $crate::__private::inject_unsized_optional::<dyn $trait + Send + Sync>()
19 };
20}
21
22/// Provide value to the DI container.
23///
24/// # Example
25/// ```rust
26/// use simple_di::provide;
27///
28/// #[derive(Debug)]
29/// struct Foo(i32);
30///
31/// provide!(Foo(42));
32///
33/// #[derive(Debug)]
34/// struct Bar(i32);
35///
36/// trait Baz {
37/// fn baz(&self) -> i32;
38/// }
39///
40/// impl Baz for Bar {
41/// fn baz(&self) -> i32 {
42/// self.0
43/// }
44/// }
45///
46/// trait Qux {
47/// fn qux(&self) -> i32;
48/// }
49///
50/// impl Qux for Bar {
51/// fn qux(&self) -> i32 {
52/// self.0
53/// }
54/// }
55///
56/// provide!(Bar(42) => Baz, Qux);
57/// ```
58#[macro_export]
59macro_rules! provide {
60 ($value:expr) => {
61 $crate::__private::provide_sized(::std::sync::Arc::new($value))
62 };
63 ($value:expr => $($trait:path),+ $(,)?) => {{
64 let value = ::std::sync::Arc::new($value);
65 $(
66 $crate::__private::provide_unsized::<dyn $trait + Send + Sync>(value.clone());
67 )+
68 $crate::__private::provide_sized(value);
69 }};
70}