diff options
Diffstat (limited to 'syrette/src')
-rw-r--r-- | syrette/src/castable_factory.rs | 6 | ||||
-rw-r--r-- | syrette/src/di_container.rs | 35 | ||||
-rw-r--r-- | syrette/src/interfaces/factory.rs | 1 | ||||
-rw-r--r-- | syrette/src/interfaces/injectable.rs | 4 | ||||
-rw-r--r-- | syrette/src/lib.rs | 2 | ||||
-rw-r--r-- | syrette/src/libs/intertrait/cast_box.rs | 8 | ||||
-rw-r--r-- | syrette/src/libs/intertrait/cast_rc.rs | 8 | ||||
-rw-r--r-- | syrette/src/libs/intertrait/hasher.rs | 16 | ||||
-rw-r--r-- | syrette/src/libs/intertrait/mod.rs | 8 | ||||
-rw-r--r-- | syrette/src/provider.rs | 11 | ||||
-rw-r--r-- | syrette/src/ptr.rs | 2 |
11 files changed, 64 insertions, 37 deletions
diff --git a/syrette/src/castable_factory.rs b/syrette/src/castable_factory.rs index d820c31..5d582bb 100644 --- a/syrette/src/castable_factory.rs +++ b/syrette/src/castable_factory.rs @@ -9,7 +9,7 @@ where Args: 'static, ReturnInterface: 'static + ?Sized, { - _func: &'static dyn Fn<Args, Output = InterfacePtr<ReturnInterface>>, + func: &'static dyn Fn<Args, Output = InterfacePtr<ReturnInterface>>, } impl<Args, ReturnInterface> CastableFactory<Args, ReturnInterface> @@ -21,7 +21,7 @@ where func: &'static dyn Fn<Args, Output = InterfacePtr<ReturnInterface>>, ) -> Self { - Self { _func: func } + Self { func } } } @@ -40,7 +40,7 @@ where { extern "rust-call" fn call(&self, args: Args) -> Self::Output { - self._func.call(args) + self.func.call(args) } } diff --git a/syrette/src/di_container.rs b/syrette/src/di_container.rs index 86724e6..df51140 100644 --- a/syrette/src/di_container.rs +++ b/syrette/src/di_container.rs @@ -19,8 +19,8 @@ pub struct BindingBuilder<'a, Interface> where Interface: 'static + ?Sized, { - _di_container: &'a mut DIContainer, - _phantom_data: PhantomData<Interface>, + di_container: &'a mut DIContainer, + interface_phantom: PhantomData<Interface>, } impl<'a, Interface> BindingBuilder<'a, Interface> @@ -30,8 +30,8 @@ where fn new(di_container: &'a mut DIContainer) -> Self { Self { - _di_container: di_container, - _phantom_data: PhantomData, + di_container, + interface_phantom: PhantomData, } } @@ -43,7 +43,7 @@ where { let interface_typeid = TypeId::of::<Interface>(); - self._di_container._bindings.insert( + self.di_container.bindings.insert( interface_typeid, Rc::new(InjectableTypeProvider::<Implementation>::new()), ); @@ -63,7 +63,7 @@ where let factory_impl = CastableFactory::new(factory_func); - self._di_container._bindings.insert( + self.di_container.bindings.insert( interface_typeid, Rc::new(FactoryProvider::new(FactoryPtr::new(factory_impl))), ); @@ -80,16 +80,17 @@ where /// ``` pub struct DIContainer { - _bindings: HashMap<TypeId, Rc<dyn IProvider>>, + bindings: HashMap<TypeId, Rc<dyn IProvider>>, } impl<'a> DIContainer { /// Returns a new `DIContainer`. + #[must_use] pub fn new() -> Self { Self { - _bindings: HashMap::new(), + bindings: HashMap::new(), } } @@ -102,6 +103,13 @@ impl<'a> DIContainer } /// Returns a new instance of the type bound with `Interface`. + /// + /// # Errors + /// Will return `Err` if: + /// - No binding for `Interface` exists + /// - Resolving the binding for `Interface` fails + /// - Casting the binding for `Interface` fails + /// - The binding for `Interface` is not injectable pub fn get<Interface>( &self, ) -> error_stack::Result<InterfacePtr<Interface>, DIContainerError> @@ -112,7 +120,7 @@ impl<'a> DIContainer let interface_name = type_name::<Interface>(); - let binding = self._bindings.get(&interface_typeid).ok_or_else(|| { + let binding = self.bindings.get(&interface_typeid).ok_or_else(|| { Report::new(DIContainerError) .attach_printable(format!("No binding exists for {}", interface_name)) })?; @@ -145,6 +153,13 @@ impl<'a> DIContainer } /// Returns the factory bound with factory type `Interface`. + /// + /// # Errors + /// Will return `Err` if: + /// - No binding for `Interface` exists + /// - Resolving the binding for `Interface` fails + /// - Casting the binding for `Interface` fails + /// - The binding for `Interface` is not a factory pub fn get_factory<Interface>( &self, ) -> error_stack::Result<FactoryPtr<Interface>, DIContainerError> @@ -155,7 +170,7 @@ impl<'a> DIContainer let interface_name = type_name::<Interface>(); - let binding = self._bindings.get(&interface_typeid).ok_or_else(|| { + let binding = self.bindings.get(&interface_typeid).ok_or_else(|| { Report::new(DIContainerError) .attach_printable(format!("No binding exists for {}", interface_name)) })?; diff --git a/syrette/src/interfaces/factory.rs b/syrette/src/interfaces/factory.rs index 3afd7a8..c97fc09 100644 --- a/syrette/src/interfaces/factory.rs +++ b/syrette/src/interfaces/factory.rs @@ -1,3 +1,4 @@ +#![allow(clippy::module_name_repetitions)] use crate::libs::intertrait::CastFrom; use crate::ptr::InterfacePtr; diff --git a/syrette/src/interfaces/injectable.rs b/syrette/src/interfaces/injectable.rs index 96d4f21..24032a6 100644 --- a/syrette/src/interfaces/injectable.rs +++ b/syrette/src/interfaces/injectable.rs @@ -5,6 +5,10 @@ use crate::DIContainer; pub trait Injectable: CastFrom { + /// Resolves the dependencies of the injectable. + /// + /// # Errors + /// Will return `Err` if resolving the dependencies fails. fn resolve( di_container: &DIContainer, ) -> error_stack::Result<InterfacePtr<Self>, ResolveError> diff --git a/syrette/src/lib.rs b/syrette/src/lib.rs index aee7fe2..992f276 100644 --- a/syrette/src/lib.rs +++ b/syrette/src/lib.rs @@ -1,4 +1,6 @@ #![feature(unboxed_closures, fn_traits)] +#![deny(clippy::all)] +#![deny(clippy::pedantic)] //! Syrette //! diff --git a/syrette/src/libs/intertrait/cast_box.rs b/syrette/src/libs/intertrait/cast_box.rs index d4da740..793efd0 100644 --- a/syrette/src/libs/intertrait/cast_box.rs +++ b/syrette/src/libs/intertrait/cast_box.rs @@ -1,13 +1,13 @@ /** * Originally from Intertrait by CodeChain * - * https://github.com/CodeChain-io/intertrait - * https://crates.io/crates/intertrait/0.2.2 + * <https://github.com/CodeChain-io/intertrait> + * <https://crates.io/crates/intertrait/0.2.2> * * Licensed under either of * - * Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) - * MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) + * Apache License, Version 2.0 (LICENSE-APACHE or <http://www.apache.org/licenses/LICENSE-2.0>) + * MIT license (LICENSE-MIT or <http://opensource.org/licenses/MIT>) * at your option. */ diff --git a/syrette/src/libs/intertrait/cast_rc.rs b/syrette/src/libs/intertrait/cast_rc.rs index 58d212a..79d8d60 100644 --- a/syrette/src/libs/intertrait/cast_rc.rs +++ b/syrette/src/libs/intertrait/cast_rc.rs @@ -1,13 +1,13 @@ /** * Originally from Intertrait by CodeChain * - * https://github.com/CodeChain-io/intertrait - * https://crates.io/crates/intertrait/0.2.2 + * <https://github.com/CodeChain-io/intertrait> + * <https://crates.io/crates/intertrait/0.2.2> * * Licensed under either of * - * Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) - * MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) + * Apache License, Version 2.0 (LICENSE-APACHE or <http://www.apache.org/licenses/LICENSE-2.0>) + * MIT license (LICENSE-MIT or <http://opensource.org/licenses/MIT>) * at your option. */ diff --git a/syrette/src/libs/intertrait/hasher.rs b/syrette/src/libs/intertrait/hasher.rs index 1fe7739..e7f110d 100644 --- a/syrette/src/libs/intertrait/hasher.rs +++ b/syrette/src/libs/intertrait/hasher.rs @@ -1,13 +1,15 @@ +#![allow(clippy::module_name_repetitions)] + /** * Originally from Intertrait by CodeChain * - * https://github.com/CodeChain-io/intertrait - * https://crates.io/crates/intertrait/0.2.2 + * <https://github.com/CodeChain-io/intertrait> + * <https://crates.io/crates/intertrait/0.2.2> * * Licensed under either of * - * Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) - * MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) + * Apache License, Version 2.0 (LICENSE-APACHE or <http://www.apache.org/licenses/LICENSE-2.0>) + * MIT license (LICENSE-MIT or <http://opensource.org/licenses/MIT>) * at your option. */ @@ -34,11 +36,13 @@ impl Hasher for FastHasher let mut bytes = bytes; while bytes.len() > size_of::<u64>() { let (u64_bytes, remaining) = bytes.split_at(size_of::<u64>()); + self.0 ^= u64::from_ne_bytes(u64_bytes.try_into().unwrap()); - bytes = remaining + + bytes = remaining; } self.0 ^= bytes .iter() - .fold(0u64, |result, b| (result << 8) | *b as u64); + .fold(0u64, |result, b| (result << 8) | u64::from(*b)); } } diff --git a/syrette/src/libs/intertrait/mod.rs b/syrette/src/libs/intertrait/mod.rs index a3f24f1..1daca64 100644 --- a/syrette/src/libs/intertrait/mod.rs +++ b/syrette/src/libs/intertrait/mod.rs @@ -1,13 +1,13 @@ /** * Originally from Intertrait by CodeChain * - * https://github.com/CodeChain-io/intertrait - * https://crates.io/crates/intertrait/0.2.2 + * <https://github.com/CodeChain-io/intertrait> + * <https://crates.io/crates/intertrait/0.2.2> * * Licensed under either of * - * Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) - * MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) + * Apache License, Version 2.0 (LICENSE-APACHE or <http://www.apache.org/licenses/LICENSE-2.0>) + * MIT license (LICENSE-MIT or <http://opensource.org/licenses/MIT>) * at your option. */ diff --git a/syrette/src/provider.rs b/syrette/src/provider.rs index c344f25..3b7e04c 100644 --- a/syrette/src/provider.rs +++ b/syrette/src/provider.rs @@ -1,3 +1,4 @@ +#![allow(clippy::module_name_repetitions)] use std::marker::PhantomData; use crate::castable_factory::AnyFactory; @@ -26,7 +27,7 @@ pub struct InjectableTypeProvider<InjectableType> where InjectableType: Injectable, { - _phantom_data: PhantomData<InjectableType>, + injectable_phantom: PhantomData<InjectableType>, } impl<InjectableType> InjectableTypeProvider<InjectableType> @@ -36,7 +37,7 @@ where pub fn new() -> Self { Self { - _phantom_data: PhantomData, + injectable_phantom: PhantomData, } } } @@ -58,14 +59,14 @@ where pub struct FactoryProvider { - _factory: FactoryPtr<dyn AnyFactory>, + factory: FactoryPtr<dyn AnyFactory>, } impl FactoryProvider { pub fn new(factory: FactoryPtr<dyn AnyFactory>) -> Self { - Self { _factory: factory } + Self { factory } } } @@ -76,6 +77,6 @@ impl IProvider for FactoryProvider _di_container: &DIContainer, ) -> error_stack::Result<Providable, ResolveError> { - Ok(Providable::Factory(self._factory.clone())) + Ok(Providable::Factory(self.factory.clone())) } } diff --git a/syrette/src/ptr.rs b/syrette/src/ptr.rs index 164e061..414d086 100644 --- a/syrette/src/ptr.rs +++ b/syrette/src/ptr.rs @@ -1,6 +1,6 @@ +#![allow(clippy::module_name_repetitions)] use std::rc::Rc; pub type InterfacePtr<Interface> = Box<Interface>; pub type FactoryPtr<FactoryInterface> = Rc<FactoryInterface>; - |