Skip to main content

di/
lib.rs

1#![doc = include_str!("README.md")]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3
4use cfg_if::cfg_if;
5use std::any::Any;
6
7// Mut<T> is public primarily for code generation in the proc macro. it is
8// generally uninteresting, but is required because, while we can detect a
9// mutable service, we don't know which alias is behind the 'async' feature.
10// the documentation will remain hidden to avoid confusion unless you really,
11// really know and need to use it.
12
13cfg_if! {
14    if #[cfg(feature = "async")] {
15        /// Represents the type alias for a service reference.
16        pub type Ref<T> = std::sync::Arc<T>;
17
18        /// Represents the type alias for a mutable service reference.
19        #[doc(hidden)]
20        pub type Mut<T> = std::sync::RwLock<T>;
21
22        /// Represents the callback function used to create a service.
23        pub type ServiceFactory = dyn (Fn(&ServiceProvider) -> Ref<dyn Any + Send + Sync>) + Send + Sync;
24    } else {
25        /// Represents the type alias for a service reference.
26        pub type Ref<T> = std::rc::Rc<T>;
27
28        /// Represents the type alias for a mutable service reference.
29        #[doc(hidden)]
30        pub type Mut<T> = std::cell::RefCell<T>;
31
32        /// Represents the callback function used to create a service.
33        pub type ServiceFactory = dyn Fn(&ServiceProvider) -> Ref<dyn Any>;
34    }
35}
36
37/// Represents the type alias for a mutable service reference.
38pub type RefMut<T> = Ref<Mut<T>>;
39
40mod collection;
41mod dependency;
42mod description;
43pub(crate) mod fmt;
44mod keyed;
45mod provider;
46mod r#type;
47mod validation;
48
49pub use collection::ServiceCollection;
50pub use dependency::{ServiceCardinality, ServiceDependency};
51pub use description::{ServiceDescriptor, ServiceLifetime};
52pub use keyed::{KeyedRef, KeyedRefMut};
53pub use provider::{ScopedServiceProvider, ServiceProvider};
54pub use r#type::Type;
55pub use validation::{validate, ValidationError};
56
57cfg_if! {
58    if #[cfg(feature = "builder")] {
59        mod builder;
60
61        pub use builder::{
62            exactly_one, exactly_one_with_key, existing, existing_as_self, existing_with_key, existing_with_key_as_self,
63            scoped, scoped_factory, scoped_with_key, scoped_with_key_factory, singleton, singleton_as_self,
64            singleton_factory, singleton_with_key, singleton_with_key_factory, transient, transient_as_self,
65            transient_factory, transient_with_key, transient_with_key_as_self, transient_with_key_factory, zero_or_more,
66            zero_or_more_with_key, zero_or_one, zero_or_one_with_key,
67        };
68        pub use description::ServiceDescriptorBuilder;
69    }
70}
71
72cfg_if! {
73    if #[cfg(feature = "inject")] {
74        mod activator;
75        mod inject;
76
77        pub use activator::Activator;
78        pub use inject::{InjectBuilder, Injectable, inject, injectable};
79    }
80}
81
82cfg_if! {
83    if #[cfg(feature = "lazy")] {
84        /// Contains support for lazy service resolution.
85        pub mod lazy;
86    }
87}
88
89#[cfg(test)]
90mod test;