Skip to main content

Injectable

Trait Injectable 

Source
pub trait Injectable:
    Send
    + Sync
    + 'static {
    // Required method
    fn register(container: &Container) -> Result<()>;
}
Expand description

Injectable is the core trait of NestForge’s dependency injection system.

The #[injectable] macro automatically implements this trait for the decorated struct. It tells NestForge how to create an instance of a service and register it within the global Container.

Manual implementation is possible for complex registration logic that the macro does not yet support.

§Manual Implementation Example

use nestforge::{Injectable, Container};

struct MyService;

impl Injectable for MyService {
    fn register(container: &Container) -> anyhow::Result<()> {
        // Registration logic goes here
        container.register(MyService)?;
        Ok(())
    }
}

Required Methods§

Source

fn register(container: &Container) -> Result<()>

Registers the provider into the provided Container. This is usually called during the module initialization phase.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§