Expand description
§Fibre IoC
A flexible, thread-safe, and dynamic Inversion of Control (IoC) container for Rust.
Fibre IoC provides a way to manage dependencies within your application. Unlike some other containers that require a single, upfront initialization, Fibre IoC allows for dynamic registration of services at any point during the application’s lifecycle.
§Core Concepts
- Container: The central registry for all your services.
- Global Container: A static, globally-available container, accessible via
global(). - Resolution: Services are resolved using the
resolve!macro, which panics if a dependency is missing. - Traits: Services can be registered against a trait and resolved as a trait object.
§Quick Start
use fibre_ioc::{global, resolve};
use std::sync::Arc;
// Define a trait and a concrete implementation.
trait Greeter {
fn greet(&self) -> String;
}
struct EnglishGreeter {
message: String,
}
impl Greeter for EnglishGreeter {
fn greet(&self) -> String {
self.message.clone()
}
}
fn main() {
// Register a simple value from anywhere in your app.
global().add_singleton(Some("greeting_message"), || String::from("Hello, World!"));
// Register a service that implements a trait.
// The factory can itself resolve other dependencies.
global().add_singleton_trait::<dyn Greeter, _>(None, || {
let message = resolve!(String, "greeting_message");
EnglishGreeter { message: (*message).clone() }
});
// In another part of your application, resolve the service by its trait.
let greeter_service = resolve!(trait Greeter);
assert_eq!(greeter_service.greet(), "Hello, World!");
println!("{}", greeter_service.greet());
}Macros§
- maybe_
resolve - Resolves an optional service from the global container.
- maybe_
resolve_ from - Resolves a service from a specific container instance, returning an
Option. - resolve
- Resolves a required service from the global container.
- resolve_
from - Resolves a required service from a specific container instance.
Structs§
- Container
- The Inversion of Control (IoC) container.
Functions§
- global
- Provides a reference to the global container instance.