-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(di): reduce lifetime uses (#35)
This tries to remove the `'a` lifetime from the generated container. However, 'impl Trait' inside the generic of managed types, when the 'impl Trait` itself is managed too, makes the complete removal of the lifetime impossible. This is because `impl Trait` cannot be used on struct fields. [So the struct field ends up having a reference](https://github.com/chesedo/despatma/blob/f69d18c7245f88a58d96e5919a5ee7181a6e8c4f/despatma-dependency-container/tests/expand/lifetime_with_managed_impl_trait_generic.expanded.rs#L25) which needs to have a lifetime. This still reduces the use of the `'a` lifetime though.
- Loading branch information
Showing
9 changed files
with
242 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,23 +12,31 @@ env: | |
jobs: | ||
fmt: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/checkout@v4 | ||
- uses: dtolnay/[email protected] | ||
with: | ||
components: rustfmt | ||
- name: Check format | ||
run: bash scripts.sh cf | ||
|
||
clippy: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/checkout@v4 | ||
- uses: dtolnay/[email protected] | ||
with: | ||
components: clippy | ||
- name: Clippy | ||
run: bash scripts.sh cc | ||
|
||
test: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/checkout@v4 | ||
- uses: dtolnay/[email protected] | ||
with: | ||
components: rustfmt | ||
- uses: cargo-bins/cargo-binstall@main | ||
- name: Install cargo-expand | ||
run: cargo binstall -y cargo-expand | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
despatma-dependency-container/tests/expand/box_dyn_trait_with_lifetime.expanded.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use auto_impl::auto_impl; | ||
trait DAL {} | ||
const _: () = { | ||
extern crate alloc; | ||
impl<T: DAL + ?::core::marker::Sized> DAL for alloc::boxed::Box<T> {} | ||
}; | ||
const _: () = { | ||
impl<'a, T: 'a + DAL + ?::core::marker::Sized> DAL for &'a T {} | ||
}; | ||
struct PostgresDAL; | ||
impl DAL for PostgresDAL {} | ||
struct SQLiteDAL; | ||
impl DAL for SQLiteDAL {} | ||
struct Service<D: DAL> { | ||
dal: D, | ||
} | ||
impl<D: DAL> Service<D> { | ||
fn new(dal: D) -> Self { | ||
{ | ||
::std::io::_print(format_args!("Box dyn Trait singleton service started\n")); | ||
}; | ||
Self { dal } | ||
} | ||
} | ||
struct DependencyContainer<'a> { | ||
dal: std::rc::Rc<std::cell::OnceCell<std::boxed::Box<dyn DAL>>>, | ||
_phantom: std::marker::PhantomData<&'a ()>, | ||
} | ||
#[automatically_derived] | ||
impl<'a> ::core::clone::Clone for DependencyContainer<'a> { | ||
#[inline] | ||
fn clone(&self) -> DependencyContainer<'a> { | ||
DependencyContainer { | ||
dal: ::core::clone::Clone::clone(&self.dal), | ||
_phantom: ::core::clone::Clone::clone(&self._phantom), | ||
} | ||
} | ||
} | ||
impl<'a> DependencyContainer<'a> { | ||
pub fn new() -> Self { | ||
Self { | ||
dal: Default::default(), | ||
_phantom: Default::default(), | ||
} | ||
} | ||
pub fn new_scope(&self) -> Self { | ||
Self { | ||
dal: self.dal.clone(), | ||
_phantom: Default::default(), | ||
} | ||
} | ||
pub fn dal(&'a self) -> &std::boxed::Box<dyn DAL> { | ||
self.dal | ||
.get_or_init(|| { | ||
if true { Box::new(PostgresDAL) } else { Box::new(SQLiteDAL) } | ||
}) | ||
} | ||
pub fn service(&'a self) -> Service<&Box<dyn DAL>> { | ||
let dal = self | ||
.dal | ||
.get_or_init(|| { | ||
if true { Box::new(PostgresDAL) } else { Box::new(SQLiteDAL) } | ||
}); | ||
Service::new(dal) | ||
} | ||
} | ||
fn main() { | ||
let container = DependencyContainer::new(); | ||
let _service = container.service(); | ||
} |
44 changes: 44 additions & 0 deletions
44
despatma-dependency-container/tests/expand/box_dyn_trait_with_lifetime.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use auto_impl::auto_impl; | ||
|
||
#[auto_impl(Box, &)] | ||
trait DAL {} | ||
|
||
struct PostgresDAL; | ||
|
||
impl DAL for PostgresDAL {} | ||
|
||
struct SQLiteDAL; | ||
|
||
impl DAL for SQLiteDAL {} | ||
|
||
struct Service<D: DAL> { | ||
dal: D, | ||
} | ||
|
||
impl<D: DAL> Service<D> { | ||
fn new(dal: D) -> Self { | ||
println!("Box dyn Trait singleton service started"); | ||
Self { dal } | ||
} | ||
} | ||
|
||
#[despatma_dependency_container::dependency_container] | ||
impl DependencyContainer { | ||
#[Singleton] | ||
fn dal(&self) -> Box<dyn DAL> { | ||
if true { | ||
Box::new(PostgresDAL) | ||
} else { | ||
Box::new(SQLiteDAL) | ||
} | ||
} | ||
|
||
fn service(&self, dal: &Box<dyn DAL>) -> Service<&Box<dyn DAL>> { | ||
Service::new(dal) | ||
} | ||
} | ||
|
||
fn main() { | ||
let container = DependencyContainer::new(); | ||
let _service = container.service(); | ||
} |
65 changes: 65 additions & 0 deletions
65
...ma-dependency-container/tests/expand/lifetime_with_managed_impl_trait_generic.expanded.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use auto_impl::auto_impl; | ||
trait DAL {} | ||
const _: () = { | ||
impl<'a, T: 'a + DAL + ?::core::marker::Sized> DAL for &'a T {} | ||
}; | ||
struct PostgresDAL; | ||
impl DAL for PostgresDAL {} | ||
struct Service<D: DAL> { | ||
dal: D, | ||
} | ||
impl<D: DAL> Service<D> { | ||
fn new(dal: D) -> Self { | ||
{ | ||
::std::io::_print( | ||
format_args!( | ||
"Lifetime service with managed impl trait generic started\n", | ||
), | ||
); | ||
}; | ||
Self { dal } | ||
} | ||
} | ||
struct DependencyContainer<'a> { | ||
dal: std::rc::Rc<std::cell::OnceCell<PostgresDAL>>, | ||
service: std::rc::Rc<std::cell::OnceCell<Service<&'a PostgresDAL>>>, | ||
_phantom: std::marker::PhantomData<&'a ()>, | ||
} | ||
#[automatically_derived] | ||
impl<'a> ::core::clone::Clone for DependencyContainer<'a> { | ||
#[inline] | ||
fn clone(&self) -> DependencyContainer<'a> { | ||
DependencyContainer { | ||
dal: ::core::clone::Clone::clone(&self.dal), | ||
service: ::core::clone::Clone::clone(&self.service), | ||
_phantom: ::core::clone::Clone::clone(&self._phantom), | ||
} | ||
} | ||
} | ||
impl<'a> DependencyContainer<'a> { | ||
pub fn new() -> Self { | ||
Self { | ||
dal: Default::default(), | ||
service: Default::default(), | ||
_phantom: Default::default(), | ||
} | ||
} | ||
pub fn new_scope(&self) -> Self { | ||
Self { | ||
dal: self.dal.clone(), | ||
service: Default::default(), | ||
_phantom: Default::default(), | ||
} | ||
} | ||
pub fn dal(&'a self) -> &impl DAL { | ||
self.dal.get_or_init(|| { PostgresDAL }) | ||
} | ||
pub fn service(&'a self) -> &Service<impl DAL + use<'a>> { | ||
let dal = self.dal.get_or_init(|| { PostgresDAL }); | ||
self.service.get_or_init(|| { Service::new(dal) }) | ||
} | ||
} | ||
fn main() { | ||
let container = DependencyContainer::new(); | ||
let _service = container.service(); | ||
} |
37 changes: 37 additions & 0 deletions
37
despatma-dependency-container/tests/expand/lifetime_with_managed_impl_trait_generic.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use auto_impl::auto_impl; | ||
|
||
#[auto_impl(&)] | ||
trait DAL {} | ||
|
||
struct PostgresDAL; | ||
|
||
impl DAL for PostgresDAL {} | ||
|
||
struct Service<D: DAL> { | ||
dal: D, | ||
} | ||
|
||
impl<D: DAL> Service<D> { | ||
fn new(dal: D) -> Self { | ||
println!("Lifetime service with managed impl trait generic started",); | ||
Self { dal } | ||
} | ||
} | ||
|
||
#[despatma_dependency_container::dependency_container] | ||
impl DependencyContainer { | ||
#[Singleton(PostgresDAL)] | ||
fn dal(&self) -> impl DAL { | ||
PostgresDAL | ||
} | ||
|
||
#[Scoped] | ||
fn service(&self, dal: impl DAL) -> Service<impl DAL> { | ||
Service::new(dal) | ||
} | ||
} | ||
|
||
fn main() { | ||
let container = DependencyContainer::new(); | ||
let _service = container.service(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters