pub mod subjects { //! Test subjects. use std::fmt::Debug; use std::rc::Rc; use syrette_macros::declare_interface; use crate::dependency_history::IDependencyHistory; use crate::di_container::blocking::IDIContainer; use crate::interfaces::injectable::Injectable; use crate::libs::intertrait::CastFromSync; use crate::ptr::TransientPtr; pub trait IUserManager { fn add_user(&self, user_id: i128); fn remove_user(&self, user_id: i128); } pub struct UserManager {} impl UserManager { pub fn new() -> Self { Self {} } } impl IUserManager for UserManager { fn add_user(&self, _user_id: i128) { // ... } fn remove_user(&self, _user_id: i128) { // ... } } use crate as syrette; declare_interface!(UserManager -> IUserManager); impl Injectable for UserManager where DIContainerType: IDIContainer, DependencyHistoryType: IDependencyHistory, { fn resolve( _di_container: &Rc, _dependency_history: DependencyHistoryType, ) -> Result, crate::errors::injectable::InjectableError> where Self: Sized, { Ok(TransientPtr::new(Self::new())) } } pub trait INumber { fn get(&self) -> i32; fn set(&mut self, number: i32); } impl PartialEq for dyn INumber { fn eq(&self, other: &Self) -> bool { self.get() == other.get() } } impl Debug for dyn INumber { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(format!("{}", self.get()).as_str()) } } pub struct Number { pub num: i32, } impl Number { pub fn new() -> Self { Self { num: 0 } } } impl INumber for Number { fn get(&self) -> i32 { self.num } fn set(&mut self, number: i32) { self.num = number; } } declare_interface!(Number -> INumber); impl Injectable for Number where DIContainerType: IDIContainer, DependencyHistoryType: IDependencyHistory, { fn resolve( _di_container: &Rc, _dependency_history: DependencyHistoryType, ) -> Result, crate::errors::injectable::InjectableError> where Self: Sized, { Ok(TransientPtr::new(Self::new())) } } #[derive(Debug)] pub struct Ninja; pub trait INinja: CastFromSync {} impl INinja for Ninja {} } #[cfg(feature = "async")] pub mod subjects_async { //! Test subjects. use std::fmt::Debug; use std::sync::Arc; use async_trait::async_trait; use syrette_macros::declare_interface; use crate::dependency_history::IDependencyHistory; use crate::di_container::asynchronous::IAsyncDIContainer; use crate::interfaces::async_injectable::AsyncInjectable; use crate::ptr::TransientPtr; pub trait IUserManager: Send + Sync { fn add_user(&self, user_id: i128); fn remove_user(&self, user_id: i128); } pub struct UserManager {} impl UserManager { pub fn new() -> Self { Self {} } } impl IUserManager for UserManager { fn add_user(&self, _user_id: i128) { // ... } fn remove_user(&self, _user_id: i128) { // ... } } use crate as syrette; declare_interface!(UserManager -> IUserManager); #[async_trait] impl AsyncInjectable for UserManager where DIContainerType: IAsyncDIContainer, DependencyHistoryType: IDependencyHistory + Send + Sync + 'static, { async fn resolve( _: &Arc, _dependency_history: DependencyHistoryType, ) -> Result, crate::errors::injectable::InjectableError> where Self: Sized, { Ok(TransientPtr::new(Self::new())) } } pub trait INumber: Send + Sync { fn get(&self) -> i32; fn set(&mut self, number: i32); } impl PartialEq for dyn INumber { fn eq(&self, other: &Self) -> bool { self.get() == other.get() } } impl Debug for dyn INumber { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(format!("{}", self.get()).as_str()) } } pub struct Number { pub num: i32, } impl Number { pub fn new() -> Self { Self { num: 0 } } } impl INumber for Number { fn get(&self) -> i32 { self.num } fn set(&mut self, number: i32) { self.num = number; } } declare_interface!(Number -> INumber, async = true); #[async_trait] impl AsyncInjectable for Number where DIContainerType: IAsyncDIContainer, DependencyHistoryType: IDependencyHistory + Send + Sync + 'static, { async fn resolve( _: &Arc, _dependency_history: DependencyHistoryType, ) -> Result, crate::errors::injectable::InjectableError> where Self: Sized, { Ok(TransientPtr::new(Self::new())) } } } pub mod mocks { #![allow(clippy::ref_option_ref)] // Caused by Mockall #![allow(dead_code)] // Not all mock functions may be used use mockall::mock; pub mod blocking_di_container { use std::rc::Rc; use super::*; use crate::dependency_history::IDependencyHistory; use crate::di_container::blocking::binding::builder::BindingBuilder; use crate::di_container::blocking::details::DIContainerInternals; use crate::di_container::blocking::IDIContainer; use crate::errors::di_container::DIContainerError; use crate::provider::blocking::IProvider; use crate::ptr::SomePtr; mock! { pub DIContainer where DependencyHistoryType: IDependencyHistory + 'static {} impl IDIContainer for DIContainer where DependencyHistoryType: IDependencyHistory + 'static { fn bind(self: &mut Rc) -> BindingBuilder where Interface: 'static + ?Sized; fn get(self: &Rc) -> Result, DIContainerError> where Interface: 'static + ?Sized; fn get_named( self: &Rc, name: &'static str, ) -> Result, DIContainerError> where Interface: 'static + ?Sized; #[doc(hidden)] fn get_bound( self: &Rc, dependency_history: DependencyHistoryType, name: Option<&'static str>, ) -> Result, DIContainerError> where Interface: 'static + ?Sized; } impl DIContainerInternals< DependencyHistoryType > for DIContainer where DependencyHistoryType: IDependencyHistory { fn has_binding(self: &Rc, name: Option<&'static str>) -> bool where Interface: ?Sized + 'static; #[doc(hidden)] fn set_binding( self: &Rc, name: Option<&'static str>, provider: Box>, ) where Interface: 'static + ?Sized; fn remove_binding( self: &Rc, name: Option<&'static str>, ) -> Option>> where Interface: 'static + ?Sized; } } } #[cfg(feature = "async")] pub mod async_di_container { use std::sync::Arc; use super::*; use crate::dependency_history::IDependencyHistory; use crate::di_container::asynchronous::binding::builder::AsyncBindingBuilder; use crate::di_container::asynchronous::details::DIContainerInternals; use crate::di_container::asynchronous::IAsyncDIContainer; use crate::errors::async_di_container::AsyncDIContainerError; use crate::provider::r#async::IAsyncProvider; use crate::ptr::SomeThreadsafePtr; mock! { pub AsyncDIContainer where DependencyHistoryType: IDependencyHistory + 'static + Send + Sync {} #[async_trait::async_trait] impl IAsyncDIContainer< DependencyHistoryType > for AsyncDIContainer where DependencyHistoryType: IDependencyHistory + 'static + Send + Sync { fn bind(self: &mut Arc) -> AsyncBindingBuilder where Interface: 'static + ?Sized + Send + Sync; async fn get( self: &Arc, ) -> Result, AsyncDIContainerError> where Interface: 'static + ?Sized + Send + Sync; async fn get_named( self: &Arc, name: &'static str, ) -> Result, AsyncDIContainerError> where Interface: 'static + ?Sized + Send + Sync; #[doc(hidden)] async fn get_bound( self: &Arc, dependency_history: DependencyHistoryType, name: Option<&'static str>, ) -> Result, AsyncDIContainerError> where Interface: 'static + ?Sized + Send + Sync; } #[async_trait::async_trait] impl DIContainerInternals< DependencyHistoryType > for AsyncDIContainer where DependencyHistoryType: IDependencyHistory + 'static + Send + Sync { async fn has_binding( self: &Arc, name: Option<&'static str>, ) -> bool where Interface: ?Sized + 'static; async fn set_binding( self: &Arc, name: Option<&'static str>, provider: Box>, ) where Interface: 'static + ?Sized; async fn remove_binding( self: &Arc, name: Option<&'static str>, ) -> Option>> where Interface: 'static + ?Sized; } } } #[cfg(feature = "async")] pub mod async_provider { use std::sync::Arc; use async_trait::async_trait; use super::*; use crate::dependency_history::IDependencyHistory; use crate::di_container::asynchronous::IAsyncDIContainer; use crate::errors::injectable::InjectableError; use crate::provider::r#async::{AsyncProvidable, IAsyncProvider}; mock! { pub AsyncProvider< DIContainerType: IAsyncDIContainer, DependencyHistoryType: IDependencyHistory + Send + Sync > {} #[async_trait] impl< DIContainerType: IAsyncDIContainer, DependencyHistoryType: IDependencyHistory + Send + Sync > IAsyncProvider for AsyncProvider< DIContainerType, DependencyHistoryType > { async fn provide( &self, di_container: &Arc, dependency_history: DependencyHistoryType ) -> Result, InjectableError>; fn do_clone(&self) -> Box>; } } } pub mod blocking_provider { use std::rc::Rc; use super::*; use crate::dependency_history::IDependencyHistory; use crate::di_container::blocking::IDIContainer; use crate::errors::injectable::InjectableError; use crate::provider::blocking::{IProvider, Providable}; mock! { pub Provider where DIContainerType: IDIContainer, DependencyHistoryType: IDependencyHistory, {} impl IProvider< DIContainerType, DependencyHistoryType > for Provider where DIContainerType: IDIContainer, DependencyHistoryType: IDependencyHistory { fn provide( &self, di_container: &Rc, dependency_history: DependencyHistoryType, ) -> Result, InjectableError>; } } } mock! { pub DependencyHistory {} impl crate::dependency_history::IDependencyHistory for DependencyHistory { fn push(&mut self); fn contains(&self) -> bool; } impl crate::dependency_history::private::Sealed for DependencyHistory {} } }