From e906a0fc5922776b8ecd17e3100b24fc44cedf19 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 16 Jul 2022 14:24:00 +0200 Subject: refactor: use common pointer type aliases --- example/src/main.rs | 18 +++++++++-------- syrette/src/castable_factory.rs | 38 ++++++++++++++++++++---------------- syrette/src/di_container.rs | 11 +++++++---- syrette/src/interfaces/factory.rs | 6 ++++-- syrette/src/interfaces/injectable.rs | 3 ++- syrette/src/lib.rs | 1 + syrette/src/provider.rs | 10 +++++----- syrette/src/ptr.rs | 6 ++++++ syrette_macros/src/lib.rs | 24 ++++++++++++----------- 9 files changed, 69 insertions(+), 48 deletions(-) create mode 100644 syrette/src/ptr.rs diff --git a/example/src/main.rs b/example/src/main.rs index 28f29b4..1f4ddb6 100644 --- a/example/src/main.rs +++ b/example/src/main.rs @@ -1,9 +1,7 @@ -use std::rc::Rc; - use syrette::errors::di_container::DIContainerError; -use syrette::factory; use syrette::interfaces::factory::IFactory; -use syrette::{injectable, DIContainer}; +use syrette::ptr::{FactoryPtr, InterfacePtr}; +use syrette::{factory, injectable, DIContainer}; trait IDog { @@ -91,15 +89,19 @@ trait IHuman struct Human { - _dog: Box, - _cat: Box, - _cow_factory: Rc, + _dog: InterfacePtr, + _cat: InterfacePtr, + _cow_factory: FactoryPtr, } #[injectable(IHuman)] impl Human { - fn new(dog: Box, cat: Box, cow_factory: Rc) -> Self + fn new( + dog: InterfacePtr, + cat: InterfacePtr, + cow_factory: FactoryPtr, + ) -> Self { Self { _dog: dog, diff --git a/syrette/src/castable_factory.rs b/syrette/src/castable_factory.rs index 8713ec4..d820c31 100644 --- a/syrette/src/castable_factory.rs +++ b/syrette/src/castable_factory.rs @@ -1,38 +1,42 @@ use crate::interfaces::factory::IFactory; use crate::libs::intertrait::CastFrom; +use crate::ptr::InterfacePtr; pub trait AnyFactory: CastFrom {} -pub struct CastableFactory +pub struct CastableFactory where Args: 'static, - Return: 'static + ?Sized, + ReturnInterface: 'static + ?Sized, { - _func: &'static dyn Fn>, + _func: &'static dyn Fn>, } -impl CastableFactory +impl CastableFactory where Args: 'static, - Return: 'static + ?Sized, + ReturnInterface: 'static + ?Sized, { - pub fn new(func: &'static dyn Fn>) -> Self + pub fn new( + func: &'static dyn Fn>, + ) -> Self { Self { _func: func } } } -impl IFactory for CastableFactory +impl IFactory + for CastableFactory where Args: 'static, - Return: 'static + ?Sized, + ReturnInterface: 'static + ?Sized, { } -impl Fn for CastableFactory +impl Fn for CastableFactory where Args: 'static, - Return: 'static + ?Sized, + ReturnInterface: 'static + ?Sized, { extern "rust-call" fn call(&self, args: Args) -> Self::Output { @@ -40,10 +44,10 @@ where } } -impl FnMut for CastableFactory +impl FnMut for CastableFactory where Args: 'static, - Return: 'static + ?Sized, + ReturnInterface: 'static + ?Sized, { extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output { @@ -51,12 +55,12 @@ where } } -impl FnOnce for CastableFactory +impl FnOnce for CastableFactory where Args: 'static, - Return: 'static + ?Sized, + ReturnInterface: 'static + ?Sized, { - type Output = Box; + type Output = InterfacePtr; extern "rust-call" fn call_once(self, args: Args) -> Self::Output { @@ -64,9 +68,9 @@ where } } -impl AnyFactory for CastableFactory +impl AnyFactory for CastableFactory where Args: 'static, - Return: 'static + ?Sized, + ReturnInterface: 'static + ?Sized, { } diff --git a/syrette/src/di_container.rs b/syrette/src/di_container.rs index 53c4287..86724e6 100644 --- a/syrette/src/di_container.rs +++ b/syrette/src/di_container.rs @@ -12,6 +12,7 @@ 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}; /// Binding builder for type `Interface` inside a [`DIContainer`]. pub struct BindingBuilder<'a, Interface> @@ -52,7 +53,7 @@ where /// associated [`DIContainer`]. pub fn to_factory( &mut self, - factory_func: &'static dyn Fn>, + factory_func: &'static dyn Fn>, ) where Args: 'static, Return: 'static + ?Sized, @@ -64,7 +65,7 @@ where self._di_container._bindings.insert( interface_typeid, - Rc::new(FactoryProvider::new(Rc::new(factory_impl))), + Rc::new(FactoryProvider::new(FactoryPtr::new(factory_impl))), ); } } @@ -101,7 +102,9 @@ impl<'a> DIContainer } /// Returns a new instance of the type bound with `Interface`. - pub fn get(&self) -> error_stack::Result, DIContainerError> + pub fn get( + &self, + ) -> error_stack::Result, DIContainerError> where Interface: 'static + ?Sized, { @@ -144,7 +147,7 @@ impl<'a> DIContainer /// Returns the factory bound with factory type `Interface`. pub fn get_factory( &self, - ) -> error_stack::Result, DIContainerError> + ) -> error_stack::Result, DIContainerError> where Interface: 'static + ?Sized, { diff --git a/syrette/src/interfaces/factory.rs b/syrette/src/interfaces/factory.rs index ed03cce..3afd7a8 100644 --- a/syrette/src/interfaces/factory.rs +++ b/syrette/src/interfaces/factory.rs @@ -1,7 +1,9 @@ use crate::libs::intertrait::CastFrom; +use crate::ptr::InterfacePtr; -pub trait IFactory: Fn> + CastFrom +pub trait IFactory: + Fn> + CastFrom where - Return: 'static + ?Sized, + ReturnInterface: 'static + ?Sized, { } diff --git a/syrette/src/interfaces/injectable.rs b/syrette/src/interfaces/injectable.rs index e704e02..96d4f21 100644 --- a/syrette/src/interfaces/injectable.rs +++ b/syrette/src/interfaces/injectable.rs @@ -1,12 +1,13 @@ use crate::errors::injectable::ResolveError; use crate::libs::intertrait::CastFrom; +use crate::ptr::InterfacePtr; use crate::DIContainer; pub trait Injectable: CastFrom { fn resolve( di_container: &DIContainer, - ) -> error_stack::Result, ResolveError> + ) -> error_stack::Result, ResolveError> where Self: Sized; } diff --git a/syrette/src/lib.rs b/syrette/src/lib.rs index 945c0c0..40cbb83 100644 --- a/syrette/src/lib.rs +++ b/syrette/src/lib.rs @@ -121,6 +121,7 @@ pub mod castable_factory; pub mod di_container; pub mod errors; pub mod interfaces; +pub mod ptr; pub use di_container::*; pub use syrette_macros::*; diff --git a/syrette/src/provider.rs b/syrette/src/provider.rs index 800315f..c344f25 100644 --- a/syrette/src/provider.rs +++ b/syrette/src/provider.rs @@ -1,17 +1,17 @@ use std::marker::PhantomData; -use std::rc::Rc; use crate::castable_factory::AnyFactory; use crate::errors::injectable::ResolveError; use crate::interfaces::injectable::Injectable; +use crate::ptr::{FactoryPtr, InterfacePtr}; use crate::DIContainer; extern crate error_stack; pub enum Providable { - Injectable(Box), - Factory(Rc), + Injectable(InterfacePtr), + Factory(FactoryPtr), } pub trait IProvider @@ -58,12 +58,12 @@ where pub struct FactoryProvider { - _factory: Rc, + _factory: FactoryPtr, } impl FactoryProvider { - pub fn new(factory: Rc) -> Self + pub fn new(factory: FactoryPtr) -> Self { Self { _factory: factory } } diff --git a/syrette/src/ptr.rs b/syrette/src/ptr.rs new file mode 100644 index 0000000..164e061 --- /dev/null +++ b/syrette/src/ptr.rs @@ -0,0 +1,6 @@ +use std::rc::Rc; + +pub type InterfacePtr = Box; + +pub type FactoryPtr = Rc; + diff --git a/syrette_macros/src/lib.rs b/syrette_macros/src/lib.rs index 0302c07..91a0562 100644 --- a/syrette_macros/src/lib.rs +++ b/syrette_macros/src/lib.rs @@ -29,7 +29,7 @@ const IMPL_NEW_METHOD_SELF_PARAM_ERR_MESSAGE: &str = const IMPL_NEW_METHOD_PARAM_TYPES_ERR_MESSAGE: &str = concat!( "All parameters of the new method of the attached to trait implementation ", - "must be either std::boxed::Box or std::rc:Rc (for factories)" + "must be either syrette::ptr::InterfacePtr or syrrete::ptr::FactoryPtr (for factories)" ); const INVALID_ALIASED_FACTORY_TRAIT_ERR_MESSAGE: &str = @@ -118,18 +118,17 @@ fn get_dependency_types(item_impl: &ItemImpl) -> Vec |mut acc, arg_type_path| { let arg_type_path_string = path_to_string(&arg_type_path.path); - if arg_type_path_string != "Box" - && arg_type_path_string != "std::boxed::Box" - && arg_type_path_string != "boxed::Box" - && arg_type_path_string != "Rc" - && arg_type_path_string != "std::rc::Rc" - && arg_type_path_string != "rc::Rc" + if arg_type_path_string != "InterfacePtr" + && arg_type_path_string != "ptr::InterfacePtr" + && arg_type_path_string != "syrrete::ptr::InterfacePtr" + && arg_type_path_string != "FactoryPtr" + && arg_type_path_string != "ptr::FactoryPtr" + && arg_type_path_string != "syrrete::ptr::FactoryPtr" { panic!("{}", IMPL_NEW_METHOD_PARAM_TYPES_ERR_MESSAGE); } // Assume the type path has a last segment. - // The Box check wouldn't pass if it didn't let last_path_segment = arg_type_path.path.segments.last().unwrap(); match &last_path_segment.arguments { @@ -138,7 +137,8 @@ fn get_dependency_types(item_impl: &ItemImpl) -> Vec let opt_first_generic_arg = generic_args.first(); - // Assume a first generic argument exists because Box requires one + // Assume a first generic argument exists because InterfacePtr and + // FactoryPtr requires one let first_generic_arg = opt_first_generic_arg.as_ref().unwrap(); match first_generic_arg { @@ -266,11 +266,13 @@ pub fn injectable(args_stream: TokenStream, impl_stream: TokenStream) -> TokenSt impl syrette::interfaces::injectable::Injectable for #self_type_path { fn resolve( di_container: &syrette::DIContainer - ) -> error_stack::Result, syrette::errors::injectable::ResolveError> + ) -> error_stack::Result< + syrette::ptr::InterfacePtr, + syrette::errors::injectable::ResolveError> { use error_stack::ResultExt; - return Ok(Box::new(Self::new( + return Ok(syrette::ptr::InterfacePtr::new(Self::new( #(#get_dependencies .change_context(syrette::errors::injectable::ResolveError) .attach_printable( -- cgit v1.2.3-18-g5258