example/
example.rs

1use std::sync::Arc;
2
3use service_rs::{Injectable, ServiceCollection};
4
5struct SomeStringWrapper(String);
6
7impl std::fmt::Display for SomeStringWrapper {
8    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
9        write!(f, "{}", self.0)
10    }
11}
12
13struct TestDep(i32);
14
15impl std::fmt::Display for TestDep {
16    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17        write!(f, "{}", self.0)
18    }
19}
20
21#[derive(Injectable)]
22struct DependingDeps {
23    test_dep: Arc<TestDep>,
24}
25
26impl std::fmt::Display for DependingDeps {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        write!(f, "Hello from DependingDeps (via std::fmt::Display)")
29    }
30}
31
32impl DependingDeps {
33    pub fn test_dep(&self) -> &Arc<TestDep> {
34        &self.test_dep
35    }
36}
37
38trait SomeInterface: Send + Sync {
39    fn do_something(&self);
40}
41
42#[derive(Injectable)]
43struct SomeInterfaceImpl;
44
45impl SomeInterface for SomeInterfaceImpl {
46    fn do_something(&self) {
47        println!("Doing something");
48    }
49}
50
51#[tokio::main]
52async fn main() {
53    let mut collection = ServiceCollection::new();
54    collection.add_singleton_with_factory::<i32, _, _>(|_| async {
55        Ok(Box::new(42) as Box<dyn std::any::Any + Send + Sync>)
56    });
57    collection.add_transient_with_factory::<SomeStringWrapper, _, _>(|_| async {
58        Ok(Box::new(SomeStringWrapper("Hello".to_string()))
59            as Box<dyn std::any::Any + Send + Sync>)
60    });
61    collection.add_scoped_with_factory::<TestDep, _, _>(|_| async {
62        Ok(Box::new(TestDep(42)) as Box<dyn std::any::Any + Send + Sync>)
63    });
64    collection.add_scoped::<DependingDeps>();
65    collection.add_scoped_interface::<dyn SomeInterface, SomeInterfaceImpl>();
66
67    println!("Successfully registered {} services", collection.len());
68
69    let provider = collection.build();
70
71    println!("Successfully built ServiceProvider");
72
73    {
74        match provider.get::<i32>().await {
75            Ok(first) => println!(
76                "(first get attempt) Service with type {} ({:p}) value is {}",
77                std::any::type_name::<i32>(),
78                Arc::as_ptr(&first),
79                first
80            ),
81            Err(e) => println!(
82                "(first get attempt) Failed to get service with type {}: {}",
83                std::any::type_name::<i32>(),
84                e
85            ),
86        }
87
88        match provider.get::<i32>().await {
89            Ok(second) => println!(
90                "(second get attempt) Service with type {} ({:p}) value is {}",
91                std::any::type_name::<i32>(),
92                Arc::as_ptr(&second),
93                second
94            ),
95            Err(e) => println!(
96                "(second get attempt) Failed to get service with type {}: {}",
97                std::any::type_name::<i32>(),
98                e
99            ),
100        }
101    }
102
103    {
104        match provider.get::<SomeStringWrapper>().await {
105            Ok(first) => println!(
106                "(first get attempt) Service with type {} ({:p}) value is {}",
107                std::any::type_name::<SomeStringWrapper>(),
108                Arc::as_ptr(&first),
109                first
110            ),
111            Err(e) => println!(
112                "(first get attempt) Failed to get service with type {}: {}",
113                std::any::type_name::<SomeStringWrapper>(),
114                e
115            ),
116        }
117
118        match provider.get::<SomeStringWrapper>().await {
119            Ok(second) => println!(
120                "(second get attempt) Service with type {} ({:p}) value is {}",
121                std::any::type_name::<SomeStringWrapper>(),
122                Arc::as_ptr(&second),
123                second
124            ),
125            Err(e) => println!(
126                "(second get attempt) Failed to get service with type {}: {}",
127                std::any::type_name::<SomeStringWrapper>(),
128                e
129            ),
130        }
131    }
132
133    {
134        match provider.get::<TestDep>().await {
135            Ok(first) => println!(
136                "(first get attempt) Service with type {} ({:p}) value is {}",
137                std::any::type_name::<TestDep>(),
138                Arc::as_ptr(&first),
139                first
140            ),
141            Err(e) => println!(
142                "(first get attempt) Failed to get service with type {}: {}",
143                std::any::type_name::<TestDep>(),
144                e
145            ),
146        }
147
148        match provider.get::<TestDep>().await {
149            Ok(second) => println!(
150                "(second get attempt) Service with type {} ({:p}) value is {}",
151                std::any::type_name::<TestDep>(),
152                Arc::as_ptr(&second),
153                second
154            ),
155            Err(e) => println!(
156                "(second get attempt) Failed to get service with type {}: {}",
157                std::any::type_name::<TestDep>(),
158                e
159            ),
160        }
161    }
162
163    {
164        let scope = provider.create_scope();
165
166        match scope.get::<TestDep>().await {
167            Ok(first) => println!(
168                "(first get attempt) Service with type {} ({:p}) value is {}",
169                std::any::type_name::<TestDep>(),
170                Arc::as_ptr(&first),
171                first
172            ),
173            Err(e) => println!(
174                "(first get attempt) Failed to get service with type {}: {}",
175                std::any::type_name::<TestDep>(),
176                e
177            ),
178        }
179
180        match scope.get::<TestDep>().await {
181            Ok(second) => println!(
182                "(second get attempt) Service with type {} ({:p}) value is {}",
183                std::any::type_name::<TestDep>(),
184                Arc::as_ptr(&second),
185                second
186            ),
187            Err(e) => println!(
188                "(second get attempt) Failed to get service with type {}: {}",
189                std::any::type_name::<TestDep>(),
190                e
191            ),
192        }
193
194        match scope.get::<DependingDeps>().await {
195            Ok(first) => println!(
196                "(first get attempt) Service with type {} ({:p}) resolved successfully with test_dep at {:p}",
197                std::any::type_name::<DependingDeps>(),
198                Arc::as_ptr(&first),
199                Arc::as_ptr(first.test_dep())
200            ),
201            Err(e) => println!(
202                "(first get attempt) Failed to get service with type {}: {}",
203                std::any::type_name::<DependingDeps>(),
204                e
205            ),
206        }
207
208        match scope.get::<DependingDeps>().await {
209            Ok(second) => println!(
210                "(second get attempt) Service with type {} ({:p}) resolved successfully with test_dep at {:p}",
211                std::any::type_name::<DependingDeps>(),
212                Arc::as_ptr(&second),
213                Arc::as_ptr(second.test_dep())
214            ),
215            Err(e) => println!(
216                "(second get attempt) Failed to get service with type {}: {}",
217                std::any::type_name::<DependingDeps>(),
218                e
219            ),
220        }
221    }
222
223    {
224        match provider
225            .create_scope()
226            .get::<Box<dyn SomeInterface>>()
227            .await
228        {
229            Ok(first) => {
230                println!(
231                    "(first get attempt) Service with type {} ({:p}) resolved successfully",
232                    std::any::type_name::<Box<dyn SomeInterface>>(),
233                    Arc::as_ptr(&first)
234                );
235                first.do_something();
236            }
237            Err(e) => println!(
238                "(first get attempt) Failed to get service with type {}: {}",
239                std::any::type_name::<Box<dyn SomeInterface>>(),
240                e
241            ),
242        }
243    }
244}