From 4cb3884e24b3cba3347ff93475bbabd6fe18d2fa Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 22 Jul 2022 13:25:45 +0200 Subject: refactor: make factories an optional feature --- src/castable_factory.rs | 4 +--- src/di_container.rs | 21 ++++++++++++++------- src/interfaces/any_factory.rs | 3 +++ src/interfaces/mod.rs | 5 ++++- src/lib.rs | 6 ++++-- src/provider.rs | 6 +++++- 6 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 src/interfaces/any_factory.rs (limited to 'src') 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 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( &mut self, factory_func: &'static dyn Fn>, ) where Args: 'static, Return: 'static + ?Sized, - Interface: IFactory, + Interface: crate::interfaces::factory::IFactory, { let interface_typeid = TypeId::of::(); @@ -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( &self, - ) -> error_stack::Result, DIContainerError> + ) -> error_stack::Result, 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::(); 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), + #[allow(dead_code)] Factory(FactoryPtr), } @@ -57,11 +58,13 @@ where } } +#[cfg(feature = "factory")] pub struct FactoryProvider { factory: FactoryPtr, } +#[cfg(feature = "factory")] impl FactoryProvider { pub fn new(factory: FactoryPtr) -> Self @@ -70,6 +73,7 @@ impl FactoryProvider } } +#[cfg(feature = "factory")] impl IProvider for FactoryProvider { fn provide( -- cgit v1.2.3-18-g5258