pub struct App { /* private fields */ }Expand description
Main application container that holds all registered components and services.
The App struct is the core of the dependency injection framework. It stores
type-safe components that can be retrieved by their type. Components are typically
service handles created during the application build process.
§Examples
use diode::{App, Service, StdError, AddServiceExt as _};
use std::sync::Arc;
struct MyService;
impl Service for MyService {
type Handle = Arc<Self>;
async fn build(_app: &diode::AppBuilder) -> Result<Self::Handle, StdError> {
Ok(Arc::new(Self))
}
}
let app = App::builder()
.add_service::<MyService>()
.build()
.await?;
let service = app.get_component::<Arc<MyService>>().unwrap();Implementations§
Source§impl App
impl App
Sourcepub fn builder() -> AppBuilder
pub fn builder() -> AppBuilder
Sourcepub fn get_component<T>(&self) -> Option<T>
pub fn get_component<T>(&self) -> Option<T>
Retrieves a component by type, returning a clone if the component implements Clone.
§Type Parameters
T- The type of component to retrieve. Must implementClone + Send + Sync + 'static.
§Returns
Returns Some(T) if the component exists and can be cloned, None otherwise.
§Examples
let app = App::builder()
.add_component("Hello, World!".to_string())
.build()
.await?;
let message = app.get_component::<String>().unwrap();
assert_eq!(message, "Hello, World!");Sourcepub fn has_component<T>(&self) -> bool
pub fn has_component<T>(&self) -> bool
Checks if a component of the specified type exists in the application.
§Type Parameters
T- The type of component to check for.
§Returns
Returns true if the component exists, false otherwise.
§Examples
let app = App::builder()
.add_component(42i32)
.build()
.await?;
assert!(app.has_component::<i32>());
assert!(!app.has_component::<String>());Sourcepub fn get_component_ref<T>(&self) -> Option<&T>
pub fn get_component_ref<T>(&self) -> Option<&T>
Retrieves a reference to a component by type.
§Type Parameters
T- The type of component to retrieve a reference to.
§Returns
Returns Some(&T) if the component exists, None otherwise.
§Examples
let app = App::builder()
.add_component(vec![1, 2, 3])
.build()
.await?;
let numbers = app.get_component_ref::<Vec<i32>>().unwrap();
assert_eq!(numbers.len(), 3);Auto Trait Implementations§
impl Freeze for App
impl !RefUnwindSafe for App
impl Send for App
impl Sync for App
impl Unpin for App
impl !UnwindSafe for App
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more