aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-10-01 22:15:28 +0200
committerHampusM <hampus@hampusmat.com>2023-10-01 22:16:10 +0200
commite0d399133b0c06011c9b775fd545ce649be37ff4 (patch)
tree5da31283d1cd182708f02c003ec5a741b8436153
parent1fd1b6607f53eba5f753943fe854fa9acc0d9a68 (diff)
feat: implement Injectable & AsyncInjectable for Default types
-rw-r--r--src/interfaces/async_injectable.rs17
-rw-r--r--src/interfaces/injectable.rs13
2 files changed, 30 insertions, 0 deletions
diff --git a/src/interfaces/async_injectable.rs b/src/interfaces/async_injectable.rs
index 4c18487..3600bac 100644
--- a/src/interfaces/async_injectable.rs
+++ b/src/interfaces/async_injectable.rs
@@ -1,5 +1,6 @@
//! Interface for structs that can be injected into or be injected to.
use std::fmt::Debug;
+use std::future::ready;
use crate::errors::injectable::InjectableError;
use crate::future::BoxFuture;
@@ -32,3 +33,19 @@ impl<DIContainerT> Debug for dyn AsyncInjectable<DIContainerT>
f.write_str("{}")
}
}
+
+impl<T, DIContainerT> AsyncInjectable<DIContainerT> for T
+where
+ T: Default + 'static + Send + Sync,
+{
+ fn resolve<'di_container, 'fut>(
+ _: &'di_container DIContainerT,
+ _: DependencyHistory,
+ ) -> BoxFuture<'fut, Result<TransientPtr<Self>, InjectableError>>
+ where
+ Self: Sized + 'fut,
+ 'di_container: 'fut,
+ {
+ Box::pin(ready(Ok(TransientPtr::new(Self::default()))))
+ }
+}
diff --git a/src/interfaces/injectable.rs b/src/interfaces/injectable.rs
index d82f042..1d3a1a7 100644
--- a/src/interfaces/injectable.rs
+++ b/src/interfaces/injectable.rs
@@ -30,3 +30,16 @@ impl<DIContainerT> Debug for dyn Injectable<DIContainerT>
f.write_str("{}")
}
}
+
+impl<T, DIContainerT> Injectable<DIContainerT> for T
+where
+ T: Default + 'static,
+{
+ fn resolve(
+ _: &DIContainerT,
+ _: DependencyHistory,
+ ) -> Result<TransientPtr<Self>, InjectableError>
+ {
+ Ok(TransientPtr::new(Self::default()))
+ }
+}