Skip to main content

Dependencies

Struct Dependencies 

Source
pub struct Dependencies<'a> { /* private fields */ }
Expand description

Dependency provider

Implementations§

Source§

impl<'a> Dependencies<'a>

Source

pub fn get<T: Any + Send + Sync + 'static>(&mut self) -> Result<Arc<T>, String>

Get bean with default name

Examples found in repository?
examples/basic.rs (line 70)
57fn main() -> Result<(), String> {
58    // Create container
59    let container = BeanContainer::new();
60
61    // Register Database (Singleton)
62    container.register::<Database, _>(Scope::Singleton, |_deps| {
63        Ok(Database::new(
64            "postgresql://localhost:5432/mydb".to_string(),
65        ))
66    })?;
67
68    // Register UserService (Singleton)
69    container.register::<UserService, _>(Scope::Singleton, |deps| {
70        let db = deps.get::<Database>()?;
71        Ok(UserService::new(db))
72    })?;
73
74    // Register OrderService (Singleton)
75    container.register::<OrderService, _>(Scope::Singleton, |deps| {
76        let db = deps.get::<Database>()?;
77        let user_service = deps.get::<UserService>()?;
78        Ok(OrderService::new(db, user_service))
79    })?;
80
81    println!("\n=== Get and use OrderService ===");
82    let order_service = container.get::<OrderService>();
83    order_service.create_order(1, "Laptop");
84
85    println!("\n=== Get OrderService again (should reuse singleton) ===");
86    let order_service2 = container.get::<OrderService>();
87    order_service2.create_order(2, "Phone");
88
89    println!("\n=== Verify same instance ===");
90    println!(
91        "Same instance: {}",
92        Arc::ptr_eq(&order_service, &order_service2)
93    );
94
95    Ok(())
96}
Source

pub fn get_named<T: Any + Send + Sync + 'static>( &mut self, name: Option<&str>, ) -> Result<Arc<T>, String>

Get bean with specified name

Source

pub fn current_path(&self) -> String

Get current dependency path (for debugging)

Auto Trait Implementations§

§

impl<'a> !UnwindSafe for Dependencies<'a>

§

impl<'a> Freeze for Dependencies<'a>

§

impl<'a> RefUnwindSafe for Dependencies<'a>

§

impl<'a> Send for Dependencies<'a>

§

impl<'a> Sync for Dependencies<'a>

§

impl<'a> Unpin for Dependencies<'a>

§

impl<'a> UnsafeUnpin for Dependencies<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.