pub struct ServiceCollection { /* private fields */ }Expand description
A collection of service descriptors used to build a service provider.
Use this to register services with different lifetimes before building
the final ServiceProvider.
§Example
use service_rs::ServiceCollection;
let mut collection = ServiceCollection::new();
collection.add_singleton_with_factory::<i32, _, _>(|_| async {
Ok(Box::new(42) as Box<dyn std::any::Any + Send + Sync>)
});
let provider = collection.build();Implementations§
Source§impl ServiceCollection
impl ServiceCollection
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty service collection.
Examples found in repository?
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}Sourcepub fn add_singleton_with_factory<T, F, Fut>(&mut self, factory: F) -> &mut Self
pub fn add_singleton_with_factory<T, F, Fut>(&mut self, factory: F) -> &mut Self
Registers a singleton service using a factory function.
The factory is called once when the service is first requested, and the same instance is returned for all subsequent requests.
§Example
use service_rs::ServiceCollection;
let mut collection = ServiceCollection::new();
collection.add_singleton_with_factory::<i32, _, _>(|_| async {
Ok(Box::new(42) as Box<dyn std::any::Any + Send + Sync>)
});Examples found in repository?
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}Sourcepub fn add_scoped_with_factory<T, F, Fut>(&mut self, factory: F) -> &mut Self
pub fn add_scoped_with_factory<T, F, Fut>(&mut self, factory: F) -> &mut Self
Registers a scoped service using a factory function.
The factory is called once per scope when the service is first requested within that scope. The same instance is returned for all requests within the same scope.
§Example
use service_rs::ServiceCollection;
let mut collection = ServiceCollection::new();
collection.add_scoped_with_factory::<String, _, _>(|_| async {
Ok(Box::new("scoped".to_string()) as Box<dyn std::any::Any + Send + Sync>)
});Examples found in repository?
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}Sourcepub fn add_transient_with_factory<T, F, Fut>(&mut self, factory: F) -> &mut Self
pub fn add_transient_with_factory<T, F, Fut>(&mut self, factory: F) -> &mut Self
Registers a transient service using a factory function.
The factory is called every time the service is requested, creating a new instance each time.
§Example
use service_rs::ServiceCollection;
let mut collection = ServiceCollection::new();
collection.add_transient_with_factory::<String, _, _>(|_| async {
Ok(Box::new("transient".to_string()) as Box<dyn std::any::Any + Send + Sync>)
});Examples found in repository?
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}Sourcepub fn add_singleton<T>(&mut self) -> &mut Selfwhere
T: InjectableExtension,
pub fn add_singleton<T>(&mut self) -> &mut Selfwhere
T: InjectableExtension,
Registers a singleton service using the Injectable derive macro.
The service type must implement InjectableExtension via the #[derive(Injectable)] macro.
Dependencies are automatically resolved from the service provider.
§Example
use service_rs::{Injectable, ServiceCollection};
use std::sync::Arc;
#[derive(Injectable)]
struct MyService {
dependency: Arc<i32>,
}
let mut collection = ServiceCollection::new();
collection.add_singleton::<MyService>();Sourcepub fn add_scoped<T>(&mut self) -> &mut Selfwhere
T: InjectableExtension,
pub fn add_scoped<T>(&mut self) -> &mut Selfwhere
T: InjectableExtension,
Registers a scoped service using the Injectable derive macro.
The service type must implement InjectableExtension via the #[derive(Injectable)] macro.
Dependencies are automatically resolved from the service provider.
§Example
use service_rs::{Injectable, ServiceCollection};
use std::sync::Arc;
#[derive(Injectable)]
struct MyService {
dependency: Arc<i32>,
}
let mut collection = ServiceCollection::new();
collection.add_scoped::<MyService>();Examples found in repository?
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}Sourcepub fn add_transient<T>(&mut self) -> &mut Selfwhere
T: InjectableExtension,
pub fn add_transient<T>(&mut self) -> &mut Selfwhere
T: InjectableExtension,
Registers a transient service using the Injectable derive macro.
The service type must implement InjectableExtension via the #[derive(Injectable)] macro.
Dependencies are automatically resolved from the service provider.
§Example
use service_rs::{Injectable, ServiceCollection};
use std::sync::Arc;
#[derive(Injectable)]
struct MyService {
dependency: Arc<i32>,
}
let mut collection = ServiceCollection::new();
collection.add_transient::<MyService>();Sourcepub fn add_singleton_interface<T, TImpl>(&mut self) -> &mut Self
pub fn add_singleton_interface<T, TImpl>(&mut self) -> &mut Self
Registers a singleton service for a trait object.
This allows you to register an implementation type that will be resolved as a trait object.
The implementation must derive Injectable and implement the trait.
§Example
use service_rs::{Injectable, ServiceCollection};
trait Logger: Send + Sync {
fn log(&self, msg: &str);
}
#[derive(Injectable)]
struct ConsoleLogger;
impl Logger for ConsoleLogger {
fn log(&self, msg: &str) {
println!("{}", msg);
}
}
let mut collection = ServiceCollection::new();
collection.add_singleton_interface::<dyn Logger, ConsoleLogger>();Sourcepub fn add_scoped_interface<T, TImpl>(&mut self) -> &mut Self
pub fn add_scoped_interface<T, TImpl>(&mut self) -> &mut Self
Registers a scoped service for a trait object.
This allows you to register an implementation type that will be resolved as a trait object.
The implementation must derive Injectable and implement the trait.
§Example
use service_rs::{Injectable, ServiceCollection};
trait Repository: Send + Sync {
fn save(&self);
}
#[derive(Injectable)]
struct DbRepository;
impl Repository for DbRepository {
fn save(&self) {}
}
let mut collection = ServiceCollection::new();
collection.add_scoped_interface::<dyn Repository, DbRepository>();Examples found in repository?
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}Sourcepub fn add_transient_interface<T, TImpl>(&mut self) -> &mut Self
pub fn add_transient_interface<T, TImpl>(&mut self) -> &mut Self
Registers a transient service for a trait object.
This allows you to register an implementation type that will be resolved as a trait object.
The implementation must derive Injectable and implement the trait.
§Example
use service_rs::{Injectable, ServiceCollection};
trait Handler: Send + Sync {
fn handle(&self);
}
#[derive(Injectable)]
struct RequestHandler;
impl Handler for RequestHandler {
fn handle(&self) {}
}
let mut collection = ServiceCollection::new();
collection.add_transient_interface::<dyn Handler, RequestHandler>();Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of registered services.
Examples found in repository?
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}Sourcepub fn build(self) -> Arc<ServiceProvider>
pub fn build(self) -> Arc<ServiceProvider>
Builds the service provider from this collection.
Consumes the collection and returns an Arc<ServiceProvider> that can be used
to resolve services.
§Example
use service_rs::ServiceCollection;
let collection = ServiceCollection::new();
let provider = collection.build();Examples found in repository?
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}