App

Struct App 

Source
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

Source

pub fn builder() -> AppBuilder

Creates a new application builder for configuring and building an application.

§Returns

Returns an AppBuilder instance that can be used to register services, plugins, and components before building the final application.

§Examples
use diode::App;

let builder = App::builder();
Source

pub fn get_component<T>(&self) -> Option<T>
where T: Clone + Send + Sync + 'static,

Retrieves a component by type, returning a clone if the component implements Clone.

§Type Parameters
  • T - The type of component to retrieve. Must implement Clone + 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!");
Source

pub fn has_component<T>(&self) -> bool
where T: Send + Sync + 'static,

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>());
Source

pub fn get_component_ref<T>(&self) -> Option<&T>
where T: Send + Sync + 'static,

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.