diff options
author | HampusM <hampus@hampusmat.com> | 2022-11-03 20:23:49 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-11-03 20:23:49 +0100 |
commit | 488faf96336711a527a3610c728a2409087b69fa (patch) | |
tree | 4347592e4ef50fb0120f0c7495a107d92140b344 | |
parent | 3af31ee154417d0fae67f5b490cbb05626593453 (diff) |
docs: add comments explaining the prevent-circular example
-rw-r--r-- | examples/prevent-circular/main.rs | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/examples/prevent-circular/main.rs b/examples/prevent-circular/main.rs index 7e10024..c690a9c 100644 --- a/examples/prevent-circular/main.rs +++ b/examples/prevent-circular/main.rs @@ -1,7 +1,10 @@ +//! Example demonstrating the prevention of circular dependencies. +//! +//! Having circular dependencies is generally bad practice and is detected by Syrette when +//! the `prevent-circular` feature is enabled. #![deny(clippy::all)] #![deny(clippy::pedantic)] #![allow(clippy::disallowed_names)] -use std::error::Error; use syrette::di_container::blocking::prelude::*; use syrette::injectable; @@ -9,7 +12,7 @@ use syrette::ptr::TransientPtr; struct Foo { - bar: TransientPtr<Bar>, + _bar: TransientPtr<Bar>, } #[injectable] @@ -17,13 +20,13 @@ impl Foo { fn new(bar: TransientPtr<Bar>) -> Self { - Self { bar } + Self { _bar: bar } } } struct Bar { - foo: TransientPtr<Foo>, + _foo: TransientPtr<Foo>, } #[injectable] @@ -31,7 +34,7 @@ impl Bar { fn new(foo: TransientPtr<Foo>) -> Self { - Self { foo } + Self { _foo: foo } } } @@ -42,7 +45,8 @@ fn main() -> Result<(), anyhow::Error> di_container.bind::<Foo>().to::<Foo>()?; di_container.bind::<Bar>().to::<Bar>()?; - let foo = di_container.get::<Foo>()?.transient()?; + // The following won't work. Err will be returned. + let _foo = di_container.get::<Foo>()?.transient()?; Ok(()) } |