diff --git a/teloc/Cargo.toml b/teloc/Cargo.toml index df979e7..bacb456 100644 --- a/teloc/Cargo.toml +++ b/teloc/Cargo.toml @@ -11,16 +11,18 @@ documentation = "https://docs.rs/teloc" readme = "../README.md" [features] -actix-support = ["actix-web", "actix-http", "actix-router", "pin-project"] -default = [] +default = ["std"] +actix-support = ["actix-web", "actix-http", "actix-router", "pin-project", "alloc"] +alloc = [] +std = ["alloc", "once_cell/std"] [dependencies] actix-web = { version = "3", optional = true } actix-http = { version = "2", optional = true } actix-router = { version = "0.2", optional = true } pin-project = { version = "1", optional = true } -frunk = "0.4.0" -once_cell = "1.4.1" +frunk = { version = "0.4.0", default-features = false } +once_cell = { version = "1.4.1", features = ["critical-section"], default-features = false } [dependencies.teloc_macros] path = "../teloc_macros" diff --git a/teloc/src/container.rs b/teloc/src/container.rs index a7b888f..a3e2143 100644 --- a/teloc/src/container.rs +++ b/teloc/src/container.rs @@ -2,9 +2,9 @@ use crate::dependency::DependencyClone; use crate::get_dependencies::GetDependencies; use crate::service_provider::SelectContainer; use crate::{Dependency, Resolver}; +use core::marker::PhantomData; use frunk::HNil; use once_cell::sync::OnceCell; -use std::marker::PhantomData; /// Init is a trait used in [`ServiceProvider`] for create an empty version of `Container`. If you /// create your own version of container and you want that it can work with other container like diff --git a/teloc/src/dependency.rs b/teloc/src/dependency.rs index f9195e0..06e3521 100644 --- a/teloc/src/dependency.rs +++ b/teloc/src/dependency.rs @@ -1,6 +1,7 @@ -use std::cell::RefCell; -use std::rc::Rc; -use std::sync::Arc; +use core::cell::RefCell; + +#[cfg(feature = "alloc")] +use alloc::{boxed::Box, rc::Rc, sync::Arc}; /// Trait is used to working with `Resolver` trait. If you want that your service can be resolved by /// `Resolver`, you may implement this trait for your service. There are three ways: @@ -11,6 +12,7 @@ pub trait Dependency { fn init(deps: Deps) -> Self; } +#[cfg(feature = "alloc")] impl Dependency for Rc where D: Dependency, @@ -19,6 +21,7 @@ where Rc::new(D::init(deps)) } } + impl Dependency for RefCell where D: Dependency, @@ -27,6 +30,8 @@ where RefCell::new(D::init(deps)) } } + +#[cfg(feature = "alloc")] impl Dependency for Box where D: Dependency, @@ -35,6 +40,8 @@ where Box::new(D::init(deps)) } } + +#[cfg(feature = "alloc")] impl Dependency for Arc where D: Dependency, @@ -49,8 +56,10 @@ where /// immutable reference. pub trait DependencyClone: Clone {} +#[cfg(feature = "alloc")] impl DependencyClone for Rc {} +#[cfg(feature = "alloc")] impl DependencyClone for Arc {} impl DependencyClone for &D {} diff --git a/teloc/src/lib.rs b/teloc/src/lib.rs index 5746c09..a5aa179 100644 --- a/teloc/src/lib.rs +++ b/teloc/src/lib.rs @@ -55,9 +55,12 @@ //! let controller: Controller = scope.resolve(); //! assert_eq!(*controller.number_service.number, 10); //! ``` - +#![cfg_attr(not(any(feature = "std", test)), no_std)] #![deny(unsafe_code)] +#[cfg(feature = "alloc")] +extern crate alloc; + #[cfg(feature = "actix-support")] mod actix_support; mod container; diff --git a/teloc/src/service_provider.rs b/teloc/src/service_provider.rs index 0a2e40d..0bdc662 100644 --- a/teloc/src/service_provider.rs +++ b/teloc/src/service_provider.rs @@ -4,8 +4,9 @@ use crate::container::{ use crate::index::{ParentIndex, SelfIndex}; use frunk::hlist::{HList, Selector}; use frunk::{HCons, HNil}; -use std::rc::Rc; -use std::sync::Arc; + +#[cfg(feature = "alloc")] +use alloc::{rc::Rc, sync::Arc}; /// `ServiceProvider` struct is used as an IoC-container in which you declare your dependencies. /// @@ -89,6 +90,7 @@ impl ServiceProvider { /// Forking `ServiceProvider` creates a new `ServiceProvider` with reference to the parent. /// `resolve` method on forked `ServiceProvider` will find dependencies form self and parent. + #[cfg(feature = "alloc")] pub fn fork_rc(self: &Rc>) -> ServiceProvider, HNil> { ServiceProvider { parent: self.clone(), @@ -98,6 +100,7 @@ impl ServiceProvider { /// Forking `ServiceProvider` creates a new `ServiceProvider` with reference to the parent. /// `resolve` method on forked `ServiceProvider` will find dependencies form self and parent. + #[cfg(feature = "alloc")] pub fn fork_arc( self: &Arc>, ) -> ServiceProvider, HNil> { @@ -403,6 +406,7 @@ where } } +#[cfg(feature = "alloc")] impl<'this, 'cont, Parent, Conts, Cont, Index> SelectContainer<'this, &'cont Cont, ParentIndex> for ServiceProvider, Conts> where @@ -413,6 +417,7 @@ where } } +#[cfg(feature = "alloc")] impl<'this, 'cont, Parent, Conts, Cont, Index> SelectContainer<'this, &'cont Cont, ParentIndex> for ServiceProvider, Conts> where