aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-09-24 17:31:16 +0200
committerHampusM <hampus@hampusmat.com>2022-09-24 17:33:10 +0200
commitfdd7f824fd1244226ca86f525f8439744676688f (patch)
tree073019e506e36578caeab894835ccf51dc5d7584 /examples
parentfebfb927b27ab03041500b16c65bdbc0624a5a72 (diff)
feat: add bind async default factories to async DI container
Diffstat (limited to 'examples')
-rw-r--r--examples/async-factory/main.rs62
1 files changed, 56 insertions, 6 deletions
diff --git a/examples/async-factory/main.rs b/examples/async-factory/main.rs
index 74e12c7..715abf5 100644
--- a/examples/async-factory/main.rs
+++ b/examples/async-factory/main.rs
@@ -2,10 +2,14 @@
#![deny(clippy::pedantic)]
#![allow(clippy::module_name_repetitions)]
+use std::time::Duration;
+
use anyhow::Result;
-use syrette::{async_closure, factory, AsyncDIContainer};
+use syrette::ptr::TransientPtr;
+use syrette::{async_closure, declare_default_factory, factory, AsyncDIContainer};
+use tokio::time::sleep;
-trait IFoo
+trait IFoo: Send + Sync
{
fn bar(&self);
}
@@ -36,6 +40,34 @@ impl IFoo for Foo
}
}
+trait IPerson: Send + Sync
+{
+ fn name(&self) -> String;
+}
+
+struct Person
+{
+ name: String,
+}
+
+impl Person
+{
+ fn new(name: String) -> Self
+ {
+ Self { name }
+ }
+}
+
+impl IPerson for Person
+{
+ fn name(&self) -> String
+ {
+ self.name.clone()
+ }
+}
+
+declare_default_factory!(dyn IPerson, async = true);
+
#[tokio::main]
async fn main() -> Result<()>
{
@@ -45,9 +77,23 @@ async fn main() -> Result<()>
.bind::<IFooFactory>()
.to_async_factory(&|_| {
async_closure!(|cnt| {
- let foo = Box::new(Foo::new(cnt));
+ let foo_ptr = Box::new(Foo::new(cnt));
- foo as Box<dyn IFoo>
+ foo_ptr as Box<dyn IFoo>
+ })
+ })
+ .await?;
+
+ di_container
+ .bind::<dyn IPerson>()
+ .to_async_default_factory(&|_| {
+ async_closure!(|| {
+ // Do some time demanding thing...
+ sleep(Duration::from_secs(1)).await;
+
+ let person = TransientPtr::new(Person::new("Bob".to_string()));
+
+ person as TransientPtr<dyn IPerson>
})
})
.await?;
@@ -57,9 +103,13 @@ async fn main() -> Result<()>
.await?
.threadsafe_factory()?;
- let foo = foo_factory(4).await;
+ let foo_ptr = foo_factory(4).await;
+
+ foo_ptr.bar();
+
+ let person = di_container.get::<dyn IPerson>().await?.transient()?;
- foo.bar();
+ println!("Person name is {}", person.name());
Ok(())
}