aboutsummaryrefslogtreecommitdiff
path: root/src/interfaces
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-08-29 20:52:56 +0200
committerHampusM <hampus@hampusmat.com>2022-08-29 21:01:32 +0200
commit080cc42bb1da09059dbc35049a7ded0649961e0c (patch)
tree307ee564124373616022c1ba2b4d5af80845cd92 /src/interfaces
parent6e31d8f9e46fece348f329763b39b9c6f2741c07 (diff)
feat: implement async functionality
Diffstat (limited to 'src/interfaces')
-rw-r--r--src/interfaces/any_factory.rs13
-rw-r--r--src/interfaces/async_injectable.rs35
-rw-r--r--src/interfaces/mod.rs3
3 files changed, 50 insertions, 1 deletions
diff --git a/src/interfaces/any_factory.rs b/src/interfaces/any_factory.rs
index 887bb61..1bf9208 100644
--- a/src/interfaces/any_factory.rs
+++ b/src/interfaces/any_factory.rs
@@ -2,7 +2,7 @@
use std::fmt::Debug;
-use crate::libs::intertrait::CastFrom;
+use crate::libs::intertrait::{CastFrom, CastFromSync};
/// Interface for any factory to ever exist.
pub trait AnyFactory: CastFrom {}
@@ -14,3 +14,14 @@ impl Debug for dyn AnyFactory
f.write_str("{}")
}
}
+
+/// Interface for any threadsafe factory to ever exist.
+pub trait AnyThreadsafeFactory: CastFromSync {}
+
+impl Debug for dyn AnyThreadsafeFactory
+{
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
+ {
+ f.write_str("{}")
+ }
+}
diff --git a/src/interfaces/async_injectable.rs b/src/interfaces/async_injectable.rs
new file mode 100644
index 0000000..badc3c5
--- /dev/null
+++ b/src/interfaces/async_injectable.rs
@@ -0,0 +1,35 @@
+//! Interface for structs that can be injected into or be injected to.
+//!
+//! *This module is only available if Syrette is built with the "async" feature.*
+use std::fmt::Debug;
+
+use async_trait::async_trait;
+
+use crate::async_di_container::AsyncDIContainer;
+use crate::errors::injectable::InjectableError;
+use crate::libs::intertrait::CastFromSync;
+use crate::ptr::TransientPtr;
+
+/// Interface for structs that can be injected into or be injected to.
+#[async_trait]
+pub trait AsyncInjectable: CastFromSync
+{
+ /// Resolves the dependencies of the injectable.
+ ///
+ /// # Errors
+ /// Will return `Err` if resolving the dependencies fails.
+ async fn resolve(
+ di_container: &AsyncDIContainer,
+ dependency_history: Vec<&'static str>,
+ ) -> Result<TransientPtr<Self>, InjectableError>
+ where
+ Self: Sized;
+}
+
+impl Debug for dyn AsyncInjectable
+{
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
+ {
+ f.write_str("{}")
+ }
+}
diff --git a/src/interfaces/mod.rs b/src/interfaces/mod.rs
index 73dde04..ddb3bba 100644
--- a/src/interfaces/mod.rs
+++ b/src/interfaces/mod.rs
@@ -8,3 +8,6 @@ pub mod any_factory;
#[cfg(feature = "factory")]
pub mod factory;
+
+#[cfg(feature = "async")]
+pub mod async_injectable;