Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions teloc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion teloc/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions teloc/src/dependency.rs
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -11,6 +12,7 @@ pub trait Dependency<Deps> {
fn init(deps: Deps) -> Self;
}

#[cfg(feature = "alloc")]
impl<Deps, D> Dependency<Deps> for Rc<D>
where
D: Dependency<Deps>,
Expand All @@ -19,6 +21,7 @@ where
Rc::new(D::init(deps))
}
}

impl<Deps, D> Dependency<Deps> for RefCell<D>
where
D: Dependency<Deps>,
Expand All @@ -27,6 +30,8 @@ where
RefCell::new(D::init(deps))
}
}

#[cfg(feature = "alloc")]
impl<Deps, D> Dependency<Deps> for Box<D>
where
D: Dependency<Deps>,
Expand All @@ -35,6 +40,8 @@ where
Box::new(D::init(deps))
}
}

#[cfg(feature = "alloc")]
impl<Deps, D> Dependency<Deps> for Arc<D>
where
D: Dependency<Deps>,
Expand All @@ -49,8 +56,10 @@ where
/// immutable reference.
pub trait DependencyClone: Clone {}

#[cfg(feature = "alloc")]
impl<D> DependencyClone for Rc<D> {}

#[cfg(feature = "alloc")]
impl<D> DependencyClone for Arc<D> {}

impl<D> DependencyClone for &D {}
5 changes: 4 additions & 1 deletion teloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions teloc/src/service_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -89,6 +90,7 @@ impl<Parent, Conts> ServiceProvider<Parent, Conts> {

/// 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<Parent, Conts>>) -> ServiceProvider<Rc<Self>, HNil> {
ServiceProvider {
parent: self.clone(),
Expand All @@ -98,6 +100,7 @@ impl<Parent, Conts> ServiceProvider<Parent, Conts> {

/// 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<Parent, Conts>>,
) -> ServiceProvider<Arc<Self>, HNil> {
Expand Down Expand Up @@ -403,6 +406,7 @@ where
}
}

#[cfg(feature = "alloc")]
impl<'this, 'cont, Parent, Conts, Cont, Index>
SelectContainer<'this, &'cont Cont, ParentIndex<Index>> for ServiceProvider<Rc<Parent>, Conts>
where
Expand All @@ -413,6 +417,7 @@ where
}
}

#[cfg(feature = "alloc")]
impl<'this, 'cont, Parent, Conts, Cont, Index>
SelectContainer<'this, &'cont Cont, ParentIndex<Index>> for ServiceProvider<Arc<Parent>, Conts>
where
Expand Down