aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--example/src/main.rs18
-rw-r--r--syrette/src/castable_factory.rs38
-rw-r--r--syrette/src/di_container.rs11
-rw-r--r--syrette/src/interfaces/factory.rs6
-rw-r--r--syrette/src/interfaces/injectable.rs3
-rw-r--r--syrette/src/lib.rs1
-rw-r--r--syrette/src/provider.rs10
-rw-r--r--syrette/src/ptr.rs6
-rw-r--r--syrette_macros/src/lib.rs24
9 files changed, 69 insertions, 48 deletions
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<dyn IDog>,
- _cat: Box<dyn ICat>,
- _cow_factory: Rc<CowFactory>,
+ _dog: InterfacePtr<dyn IDog>,
+ _cat: InterfacePtr<dyn ICat>,
+ _cow_factory: FactoryPtr<CowFactory>,
}
#[injectable(IHuman)]
impl Human
{
- fn new(dog: Box<dyn IDog>, cat: Box<dyn ICat>, cow_factory: Rc<CowFactory>) -> Self
+ fn new(
+ dog: InterfacePtr<dyn IDog>,
+ cat: InterfacePtr<dyn ICat>,
+ cow_factory: FactoryPtr<CowFactory>,
+ ) -> 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<Args, Return>
+pub struct CastableFactory<Args, ReturnInterface>
where
Args: 'static,
- Return: 'static + ?Sized,
+ ReturnInterface: 'static + ?Sized,
{
- _func: &'static dyn Fn<Args, Output = Box<Return>>,
+ _func: &'static dyn Fn<Args, Output = InterfacePtr<ReturnInterface>>,
}
-impl<Args, Return> CastableFactory<Args, Return>
+impl<Args, ReturnInterface> CastableFactory<Args, ReturnInterface>
where
Args: 'static,
- Return: 'static + ?Sized,
+ ReturnInterface: 'static + ?Sized,
{
- pub fn new(func: &'static dyn Fn<Args, Output = Box<Return>>) -> Self
+ pub fn new(
+ func: &'static dyn Fn<Args, Output = InterfacePtr<ReturnInterface>>,
+ ) -> Self
{
Self { _func: func }
}
}
-impl<Args, Return> IFactory<Args, Return> for CastableFactory<Args, Return>
+impl<Args, ReturnInterface> IFactory<Args, ReturnInterface>
+ for CastableFactory<Args, ReturnInterface>
where
Args: 'static,
- Return: 'static + ?Sized,
+ ReturnInterface: 'static + ?Sized,
{
}
-impl<Args, Return> Fn<Args> for CastableFactory<Args, Return>
+impl<Args, ReturnInterface> Fn<Args> for CastableFactory<Args, ReturnInterface>
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<Args, Return> FnMut<Args> for CastableFactory<Args, Return>
+impl<Args, ReturnInterface> FnMut<Args> for CastableFactory<Args, ReturnInterface>
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<Args, Return> FnOnce<Args> for CastableFactory<Args, Return>
+impl<Args, ReturnInterface> FnOnce<Args> for CastableFactory<Args, ReturnInterface>
where
Args: 'static,
- Return: 'static + ?Sized,
+ ReturnInterface: 'static + ?Sized,
{
- type Output = Box<Return>;
+ type Output = InterfacePtr<ReturnInterface>;
extern "rust-call" fn call_once(self, args: Args) -> Self::Output
{
@@ -64,9 +68,9 @@ where
}
}
-impl<Args, Return> AnyFactory for CastableFactory<Args, Return>
+impl<Args, ReturnInterface> AnyFactory for CastableFactory<Args, ReturnInterface>
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<Args, Return>(
&mut self,
- factory_func: &'static dyn Fn<Args, Output = Box<Return>>,
+ factory_func: &'static dyn Fn<Args, Output = InterfacePtr<Return>>,
) 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<Interface>(&self) -> error_stack::Result<Box<Interface>, DIContainerError>
+ pub fn get<Interface>(
+ &self,
+ ) -> error_stack::Result<InterfacePtr<Interface>, DIContainerError>
where
Interface: 'static + ?Sized,
{
@@ -144,7 +147,7 @@ impl<'a> DIContainer
/// Returns the factory bound with factory type `Interface`.
pub fn get_factory<Interface>(
&self,
- ) -> error_stack::Result<Rc<Interface>, DIContainerError>
+ ) -> error_stack::Result<FactoryPtr<Interface>, 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<Args, Return>: Fn<Args, Output = Box<Return>> + CastFrom
+pub trait IFactory<Args, ReturnInterface>:
+ Fn<Args, Output = InterfacePtr<ReturnInterface>> + 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<Box<Self>, ResolveError>
+ ) -> error_stack::Result<InterfacePtr<Self>, 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<dyn Injectable>),
- Factory(Rc<dyn AnyFactory>),
+ Injectable(InterfacePtr<dyn Injectable>),
+ Factory(FactoryPtr<dyn AnyFactory>),
}
pub trait IProvider
@@ -58,12 +58,12 @@ where
pub struct FactoryProvider
{
- _factory: Rc<dyn AnyFactory>,
+ _factory: FactoryPtr<dyn AnyFactory>,
}
impl FactoryProvider
{
- pub fn new(factory: Rc<dyn AnyFactory>) -> Self
+ pub fn new(factory: FactoryPtr<dyn AnyFactory>) -> 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<Interface> = Box<Interface>;
+
+pub type FactoryPtr<FactoryInterface> = Rc<FactoryInterface>;
+
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<Type>
|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<Type>
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<Box<Self>, syrette::errors::injectable::ResolveError>
+ ) -> error_stack::Result<
+ syrette::ptr::InterfacePtr<Self>,
+ 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(