aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-07-22 13:25:45 +0200
committerHampusM <hampus@hampusmat.com>2022-07-22 13:25:45 +0200
commit4cb3884e24b3cba3347ff93475bbabd6fe18d2fa (patch)
tree2fa5e6d81de9dc39bd11d64797914e5d305d98e2 /src
parent157f38bc2287dcb9a8b21ef3d5e33c569dc5136e (diff)
refactor: make factories an optional feature
Diffstat (limited to 'src')
-rw-r--r--src/castable_factory.rs4
-rw-r--r--src/di_container.rs21
-rw-r--r--src/interfaces/any_factory.rs3
-rw-r--r--src/interfaces/mod.rs5
-rw-r--r--src/lib.rs6
-rw-r--r--src/provider.rs6
6 files changed, 31 insertions, 14 deletions
diff --git a/src/castable_factory.rs b/src/castable_factory.rs
index 5d582bb..c50456c 100644
--- a/src/castable_factory.rs
+++ b/src/castable_factory.rs
@@ -1,9 +1,7 @@
+use crate::interfaces::any_factory::AnyFactory;
use crate::interfaces::factory::IFactory;
-use crate::libs::intertrait::CastFrom;
use crate::ptr::InterfacePtr;
-pub trait AnyFactory: CastFrom {}
-
pub struct CastableFactory<Args, ReturnInterface>
where
Args: 'static,
diff --git a/src/di_container.rs b/src/di_container.rs
index 6982a10..ae6a851 100644
--- a/src/di_container.rs
+++ b/src/di_container.rs
@@ -5,14 +5,13 @@ use std::rc::Rc;
use error_stack::{Report, ResultExt};
+#[cfg(feature = "factory")]
use crate::castable_factory::CastableFactory;
use crate::errors::di_container::DIContainerError;
-use crate::interfaces::factory::IFactory;
use crate::interfaces::injectable::Injectable;
use crate::libs::intertrait::cast_box::CastBox;
-use crate::libs::intertrait::cast_rc::CastRc;
-use crate::provider::{FactoryProvider, IProvider, InjectableTypeProvider, Providable};
-use crate::ptr::{FactoryPtr, InterfacePtr};
+use crate::provider::{IProvider, InjectableTypeProvider, Providable};
+use crate::ptr::InterfacePtr;
/// Binding builder for type `Interface` inside a [`DIContainer`].
pub struct BindingBuilder<'di_container_lt, Interface>
@@ -51,13 +50,14 @@ where
/// Creates a binding of factory type `Interface` to a factory inside of the
/// associated [`DIContainer`].
+ #[cfg(feature = "factory")]
pub fn to_factory<Args, Return>(
&mut self,
factory_func: &'static dyn Fn<Args, Output = InterfacePtr<Return>>,
) where
Args: 'static,
Return: 'static + ?Sized,
- Interface: IFactory<Args, Return>,
+ Interface: crate::interfaces::factory::IFactory<Args, Return>,
{
let interface_typeid = TypeId::of::<Interface>();
@@ -65,7 +65,9 @@ where
self.di_container.bindings.insert(
interface_typeid,
- Rc::new(FactoryProvider::new(FactoryPtr::new(factory_impl))),
+ Rc::new(crate::provider::FactoryProvider::new(
+ crate::ptr::FactoryPtr::new(factory_impl),
+ )),
);
}
}
@@ -197,9 +199,10 @@ impl DIContainer
/// - Resolving the binding for `Interface` fails
/// - Casting the binding for `Interface` fails
/// - The binding for `Interface` is not a factory
+ #[cfg(feature = "factory")]
pub fn get_factory<Interface>(
&self,
- ) -> error_stack::Result<FactoryPtr<Interface>, DIContainerError>
+ ) -> error_stack::Result<crate::ptr::FactoryPtr<Interface>, DIContainerError>
where
Interface: 'static + ?Sized,
{
@@ -222,6 +225,8 @@ impl DIContainer
match binding_providable {
Providable::Factory(binding_factory) => {
+ use crate::libs::intertrait::cast_rc::CastRc;
+
let factory_box_result = binding_factory.cast::<Interface>();
match factory_box_result {
@@ -306,6 +311,7 @@ mod tests
}
#[test]
+ #[cfg(feature = "factory")]
fn can_bind_to_factory()
{
trait IUserManager
@@ -423,6 +429,7 @@ mod tests
}
#[test]
+ #[cfg(feature = "factory")]
fn can_get_factory() -> error_stack::Result<(), DIContainerError>
{
trait IUserManager
diff --git a/src/interfaces/any_factory.rs b/src/interfaces/any_factory.rs
new file mode 100644
index 0000000..41063e1
--- /dev/null
+++ b/src/interfaces/any_factory.rs
@@ -0,0 +1,3 @@
+use crate::libs::intertrait::CastFrom;
+
+pub trait AnyFactory: CastFrom {}
diff --git a/src/interfaces/mod.rs b/src/interfaces/mod.rs
index 921bb9c..497521e 100644
--- a/src/interfaces/mod.rs
+++ b/src/interfaces/mod.rs
@@ -1,2 +1,5 @@
-pub mod factory;
+pub mod any_factory;
pub mod injectable;
+
+#[cfg(feature = "factory")]
+pub mod factory;
diff --git a/src/lib.rs b/src/lib.rs
index 992f276..5724f10 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,4 +1,4 @@
-#![feature(unboxed_closures, fn_traits)]
+#![cfg_attr(feature = "factory", feature(unboxed_closures, fn_traits))]
#![deny(clippy::all)]
#![deny(clippy::pedantic)]
@@ -6,12 +6,14 @@
//!
//! Syrette is a collection of utilities useful for performing dependency injection.
-pub mod castable_factory;
pub mod di_container;
pub mod errors;
pub mod interfaces;
pub mod ptr;
+#[cfg(feature = "factory")]
+pub mod castable_factory;
+
pub use di_container::*;
pub use syrette_macros::*;
diff --git a/src/provider.rs b/src/provider.rs
index 3b7e04c..bd17474 100644
--- a/src/provider.rs
+++ b/src/provider.rs
@@ -1,8 +1,8 @@
#![allow(clippy::module_name_repetitions)]
use std::marker::PhantomData;
-use crate::castable_factory::AnyFactory;
use crate::errors::injectable::ResolveError;
+use crate::interfaces::any_factory::AnyFactory;
use crate::interfaces::injectable::Injectable;
use crate::ptr::{FactoryPtr, InterfacePtr};
use crate::DIContainer;
@@ -12,6 +12,7 @@ extern crate error_stack;
pub enum Providable
{
Injectable(InterfacePtr<dyn Injectable>),
+ #[allow(dead_code)]
Factory(FactoryPtr<dyn AnyFactory>),
}
@@ -57,11 +58,13 @@ where
}
}
+#[cfg(feature = "factory")]
pub struct FactoryProvider
{
factory: FactoryPtr<dyn AnyFactory>,
}
+#[cfg(feature = "factory")]
impl FactoryProvider
{
pub fn new(factory: FactoryPtr<dyn AnyFactory>) -> Self
@@ -70,6 +73,7 @@ impl FactoryProvider
}
}
+#[cfg(feature = "factory")]
impl IProvider for FactoryProvider
{
fn provide(