Service

Trait Service 

Source
pub trait Service: Send + Sync {
    type Handle: Send + Sync + 'static;

    // Required method
    fn build(
        app: &AppBuilder,
    ) -> impl Future<Output = Result<Self::Handle, StdError>> + Send;

    // Provided method
    fn dependencies() -> Dependencies { ... }
}
Expand description

Trait for defining injectable services with async initialization and dependency management.

Services are the primary abstraction for business logic components in diode applications. They define how to build themselves asynchronously, what dependencies they require, and what handle type they expose to other parts of the application.

§Type Parameters

  • Handle - The type that represents this service when injected into other services. Typically Arc<Self> for shared ownership, but can be any type that represents the service interface.

§Examples

Basic service implementation:

use diode::{Service, AppBuilder, StdError};
use std::sync::Arc;

struct DatabaseService {
    connection_pool: String,
}

impl Service for DatabaseService {
    type Handle = Arc<Self>;

    async fn build(_app: &AppBuilder) -> Result<Self::Handle, StdError> {
        Ok(Arc::new(Self {
            connection_pool: "sqlite::memory:".to_string(),
        }))
    }
}

Service with dependencies:

use diode::{Service, AppBuilder, StdError, Dependencies, ServiceDependencyExt};
use std::sync::Arc;

struct ConfigService;
struct ApiService {
    config: Arc<ConfigService>,
}

impl Service for ConfigService {
    type Handle = Arc<Self>;
    async fn build(_app: &AppBuilder) -> Result<Self::Handle, StdError> {
        Ok(Arc::new(Self))
    }
}

impl Service for ApiService {
    type Handle = Arc<Self>;

    async fn build(app: &AppBuilder) -> Result<Self::Handle, StdError> {
        let config = app.get_component::<Arc<ConfigService>>()
            .ok_or("ConfigService not found")?;

        Ok(Arc::new(Self { config }))
    }

    fn dependencies() -> Dependencies {
        Dependencies::new().service::<ConfigService>()
    }
}

Required Associated Types§

Source

type Handle: Send + Sync + 'static

The handle type that represents this service when injected into other components.

This is typically Arc<Self> for services that need to be shared across multiple consumers, but can be any type that implements the required bounds.

Required Methods§

Source

fn build( app: &AppBuilder, ) -> impl Future<Output = Result<Self::Handle, StdError>> + Send

Builds an instance of this service asynchronously.

This method is called during the application build process when the service is needed. It receives a reference to the application builder, which can be used to retrieve dependencies that have already been built.

§Arguments
  • app - Reference to the application builder containing registered components.
§Returns

Returns Ok(Self::Handle) on successful service creation, or an error if the service cannot be built (e.g., missing dependencies, initialization failure).

Provided Methods§

Source

fn dependencies() -> Dependencies

Declares the dependencies this service requires.

Dependencies are used to determine the initialization order of services. Services with dependencies will be built after their dependencies are available.

§Returns

Returns a Dependencies object describing required service or plugin dependencies.

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§