Skip to content

Commit 6776715

Browse files
Change set:
- Add more inlining - Fix/simplify comments - Fix Send + Sync for async
1 parent 1b75b14 commit 6776715

26 files changed

+685
-553
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ edition = "2018"
1111

1212
[workspace.dependencies]
1313
cfg-if = "1.0"
14+
maybe-impl = "0.1.0"

src/di/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ colored = { version = "2.0", optional = true }
4343
path = "."
4444
default-features = false
4545
features = ["builder", "lazy", "fmt"]
46+
47+
[dev-dependencies]
48+
maybe-impl.workspace = true

src/di/activator.rs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
use crate::{Mut, Ref, RefMut, ServiceFactory, ServiceProvider, Type};
22
use std::any::Any;
33

4+
macro_rules! new {
5+
($($traits:tt)+) => {
6+
/// Creates a new activator using the specified factory methods to instantiate the service.
7+
///
8+
/// # Arguments
9+
///
10+
/// * `factory` - The factory method used to create a service instance
11+
/// * `factory_mut` - The factory method used to create a mutable service instance
12+
pub fn new<TSvc: ?Sized + $($traits)+, TImpl>(
13+
factory: fn(&ServiceProvider) -> Ref<TSvc>,
14+
factory_mut: fn(&ServiceProvider) -> RefMut<TSvc>,
15+
) -> Self {
16+
Self {
17+
service_type: Type::of::<TSvc>(),
18+
service_type_mut: Type::of::<Mut<TSvc>>(),
19+
implementation_type: Type::of::<TImpl>(),
20+
factory: Ref::new(move |sp| Ref::new(factory(sp))),
21+
factory_mut: Ref::new(move |sp| Ref::new(factory_mut(sp))),
22+
mutable: false,
23+
}
24+
}
25+
};
26+
}
27+
428
/// Represents an activator for a service instance.
529
pub struct Activator {
630
service_type: Type,
@@ -12,7 +36,7 @@ pub struct Activator {
1236
}
1337

1438
impl Activator {
15-
/// Gets the [service type](crate::Type) associated with the service descriptor.
39+
/// Gets the [service type](Type) associated with the service descriptor.
1640
pub fn service_type(&self) -> &Type {
1741
if self.mutable {
1842
&self.service_type_mut
@@ -21,17 +45,19 @@ impl Activator {
2145
}
2246
}
2347

24-
/// Gets the [implementation type](crate::Type) associated with the service descriptor.
48+
/// Gets the [implementation type](Type) associated with the service descriptor.
49+
#[inline]
2550
pub fn implementation_type(&self) -> &Type {
2651
&self.implementation_type
2752
}
2853

2954
/// Sets a value indicating whether the activated instance should be mutable.
55+
#[inline]
3056
pub fn as_mut(&mut self) {
3157
self.mutable = true;
3258
}
3359

34-
/// Gets the factory method the activator represents.
60+
/// Gets the [factory](ServiceFactory) method the activator represents.
3561
pub fn factory(&self) -> Ref<ServiceFactory> {
3662
if self.mutable {
3763
self.factory_mut.clone()
@@ -40,23 +66,11 @@ impl Activator {
4066
}
4167
}
4268

43-
/// Creates a new activator using the specified factory methods to instantiate the service.
44-
///
45-
/// # Arguments
46-
///
47-
/// * `factory` - The factory method used to create a service instance
48-
/// * `factory_mut` - The factory method used to create a mutable service instance
49-
pub fn new<TSvc: Any + ?Sized, TImpl>(
50-
factory: fn(&ServiceProvider) -> Ref<TSvc>,
51-
factory_mut: fn(&ServiceProvider) -> RefMut<TSvc>,
52-
) -> Self {
53-
Self {
54-
service_type: Type::of::<TSvc>(),
55-
service_type_mut: Type::of::<Mut<TSvc>>(),
56-
implementation_type: Type::of::<TImpl>(),
57-
factory: Ref::new(move |sp| Ref::new(factory(sp))),
58-
factory_mut: Ref::new(move |sp| Ref::new(factory_mut(sp))),
59-
mutable: false,
69+
cfg_if::cfg_if! {
70+
if #[cfg(feature = "async")] {
71+
new!(Any + Send + Sync);
72+
} else {
73+
new!(Any);
6074
}
6175
}
6276
}

0 commit comments

Comments
 (0)