aboutsummaryrefslogtreecommitdiff
path: root/src/di_container/blocking/binding
diff options
context:
space:
mode:
Diffstat (limited to 'src/di_container/blocking/binding')
-rw-r--r--src/di_container/blocking/binding/scope_configurator.rs79
-rw-r--r--src/di_container/blocking/binding/when_configurator.rs28
2 files changed, 107 insertions, 0 deletions
diff --git a/src/di_container/blocking/binding/scope_configurator.rs b/src/di_container/blocking/binding/scope_configurator.rs
index 3d939ba..ee935a5 100644
--- a/src/di_container/blocking/binding/scope_configurator.rs
+++ b/src/di_container/blocking/binding/scope_configurator.rs
@@ -47,6 +47,33 @@ where
/// Configures the binding to be in a transient scope.
///
/// This is the default.
+ ///
+ /// # Examples
+ /// ```
+ /// # use syrette::{DIContainer, injectable};
+ /// #
+ /// # struct Authenticator {}
+ /// #
+ /// # #[injectable]
+ /// # impl Authenticator
+ /// # {
+ /// # fn new() -> Self
+ /// # {
+ /// # Self {}
+ /// # }
+ /// # }
+ /// #
+ /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
+ /// let mut di_container = DIContainer::new();
+ ///
+ /// di_container
+ /// .bind::<Authenticator>()
+ /// .to::<Authenticator>()?
+ /// .in_transient_scope();
+ /// #
+ /// # Ok(())
+ /// # }
+ /// ```
#[allow(clippy::must_use_candidate)]
pub fn in_transient_scope(self) -> BindingWhenConfigurator<'di_container, Interface>
{
@@ -59,6 +86,58 @@ where
///
/// # Errors
/// Will return Err if resolving the implementation fails.
+ ///
+ /// # Examples
+ /// ```
+ /// # use std::sync::atomic::{AtomicBool, Ordering};
+ /// # use syrette::{DIContainer, injectable};
+ /// #
+ /// # struct AudioManager
+ /// # {
+ /// # is_sound_playing: AtomicBool
+ /// # }
+ /// #
+ /// # #[injectable]
+ /// # impl AudioManager
+ /// # {
+ /// # fn new() -> Self
+ /// # {
+ /// # Self { is_sound_playing: AtomicBool::new(false) }
+ /// # }
+ /// #
+ /// # fn play_long_sound(&self)
+ /// # {
+ /// # self.is_sound_playing.store(true, Ordering::Relaxed);
+ /// # }
+ /// #
+ /// # fn is_sound_playing(&self) -> bool
+ /// # {
+ /// # self.is_sound_playing.load(Ordering::Relaxed)
+ /// # }
+ /// #
+ /// # }
+ /// #
+ /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
+ /// let mut di_container = DIContainer::new();
+ ///
+ /// di_container
+ /// .bind::<AudioManager>()
+ /// .to::<AudioManager>()?
+ /// .in_singleton_scope();
+ ///
+ /// {
+ /// let audio_manager = di_container.get::<AudioManager>()?.singleton()?;
+ ///
+ /// audio_manager.play_long_sound();
+ /// }
+ ///
+ /// let audio_manager = di_container.get::<AudioManager>()?.singleton()?;
+ ///
+ /// assert!(audio_manager.is_sound_playing());
+ /// #
+ /// # Ok(())
+ /// # }
+ /// ```
pub fn in_singleton_scope(
self,
) -> Result<
diff --git a/src/di_container/blocking/binding/when_configurator.rs b/src/di_container/blocking/binding/when_configurator.rs
index 2a1af2c..d23d213 100644
--- a/src/di_container/blocking/binding/when_configurator.rs
+++ b/src/di_container/blocking/binding/when_configurator.rs
@@ -34,6 +34,34 @@ where
///
/// # Errors
/// Will return Err if no binding for the interface already exists.
+ ///
+ /// # Examples
+ /// ```
+ /// # use syrette::{DIContainer, injectable};
+ /// #
+ /// # struct Kitten {}
+ /// #
+ /// # #[injectable]
+ /// # impl Kitten
+ /// # {
+ /// # fn new() -> Self
+ /// # {
+ /// # Self {}
+ /// # }
+ /// # }
+ /// #
+ /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
+ /// let mut di_container = DIContainer::new();
+ ///
+ /// di_container
+ /// .bind::<Kitten>()
+ /// .to::<Kitten>()?
+ /// .in_transient_scope()
+ /// .when_named("Billy")?;
+ /// #
+ /// # Ok(())
+ /// # }
+ /// ```
pub fn when_named(
self,
name: &'static str,