From 31f62ea0c634360030dcf89203268fa5684b5905 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 2 Oct 2022 12:59:05 +0200 Subject: docs: add binding builder examples --- src/di_container.rs | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) (limited to 'src') diff --git a/src/di_container.rs b/src/di_container.rs index d86f593..7ccfd3f 100644 --- a/src/di_container.rs +++ b/src/di_container.rs @@ -213,6 +213,36 @@ where /// # Errors /// Will return Err if the associated [`DIContainer`] already have a binding for /// the interface. + /// + /// # Examples + /// ``` + /// # use std::error::Error; + /// # + /// # use syrette::{DIContainer, injectable}; + /// # + /// # trait Foo {} + /// # + /// # struct Bar {} + /// # + /// # #[injectable(Foo)] + /// # impl Bar { + /// # fn new() -> Self + /// # { + /// # Self {} + /// # } + /// # } + /// # + /// # impl Foo for Bar {} + /// # + /// # fn main() -> Result<(), Box> + /// # { + /// # let mut di_container = DIContainer::new(); + /// # + /// di_container.bind::().to::(); + /// # + /// # Ok(()) + /// # } + /// ``` pub fn to( &self, ) -> Result, BindingBuilderError> @@ -246,6 +276,63 @@ where /// # Errors /// Will return Err if the associated [`DIContainer`] already have a binding for /// the interface. + /// + /// # Examples + /// ``` + /// # use std::error::Error; + /// # + /// # use syrette::{DIContainer, factory}; + /// # use syrette::ptr::TransientPtr; + /// # + /// # trait ICustomerID {} + /// # trait ICustomer {} + /// # + /// # struct Customer + /// # { + /// # name: String, + /// # id: TransientPtr + /// # } + /// # + /// # impl Customer { + /// # fn new(name: String, id: TransientPtr) -> Self + /// # { + /// # Self { name, id } + /// # } + /// # } + /// # + /// # impl ICustomer for Customer {} + /// # + /// # #[factory] + /// # type ICustomerFactory = dyn Fn(String, u32) -> dyn ICustomer; + /// # + /// # #[factory] + /// # type ICustomerIDFactory = dyn Fn(u32) -> dyn ICustomerID; + /// # + /// # fn main() -> Result<(), Box> + /// # { + /// # let mut di_container = DIContainer::new(); + /// # + /// di_container + /// .bind::() + /// .to_factory(&|context| { + /// Box::new(move |name, id| { + /// let customer_id_factory = context + /// .get::() + /// .unwrap() + /// .factory() + /// .unwrap(); + /// + /// let customer_id = customer_id_factory(id); + /// + /// let customer = TransientPtr::new(Customer::new(name, customer_id)); + /// + /// customer as TransientPtr + /// }) + /// }); + /// # + /// # Ok(()) + /// # } + /// ``` #[cfg(feature = "factory")] pub fn to_factory( &self, @@ -293,6 +380,50 @@ where /// # Errors /// Will return Err if the associated [`DIContainer`] already have a binding for /// the interface. + /// + /// # Examples + /// ``` + /// # use std::error::Error; + /// # + /// # use syrette::{DIContainer, factory}; + /// # use syrette::ptr::TransientPtr; + /// # + /// # trait IBuffer {} + /// # + /// # struct Buffer + /// # { + /// # buf: [u8; SIZE] + /// # } + /// # + /// # impl Buffer + /// # { + /// # fn new() -> Self + /// # { + /// # Self { + /// # buf: [0; SIZE] + /// # } + /// # } + /// # } + /// # + /// # impl IBuffer for Buffer {} + /// # + /// # const BUFFER_SIZE: usize = 12; + /// # + /// # fn main() -> Result<(), Box> + /// # { + /// # let mut di_container = DIContainer::new(); + /// # + /// di_container.bind::().to_default_factory(&|_| { + /// Box::new(|| { + /// let buffer = TransientPtr::new(Buffer::::new()); + /// + /// buffer as TransientPtr + /// }) + /// }); + /// # + /// # Ok(()) + /// # } + /// ``` #[cfg(feature = "factory")] pub fn to_default_factory( &self, -- cgit v1.2.3-18-g5258