Trait AsyncMakeService

Source
pub trait AsyncMakeService {
    type Service;
    type Error;

    // Required method
    fn make_via_ref(
        &self,
        old: Option<&Self::Service>,
    ) -> impl Future<Output = Result<Self::Service, Self::Error>>;

    // Provided method
    fn make(&self) -> impl Future<Output = Result<Self::Service, Self::Error>> { ... }
}
Expand description

A trait implemented by asynchronous service factories to create instances of services that implement the Service trait.

AsyncMakeService is the asynchronous counterpart to MakeService. It enables flexible service chain construction with state migration between instances, allowing for asynchronous initialization or resource acquisition.

§Key Features

  • Asynchronous service creation, suitable for I/O-bound initialization
  • State preservation and resource reuse between service instances
  • Optional creation of services from scratch
  • Efficient management of stateful resources across service updates

§Example Implementation

use std::convert::Infallible;
use your_crate::{AsyncMakeService, Service};

struct MyAsyncService {
    connection_pool: AsyncConnectionPool,
    config: Config,
}

struct MyAsyncServiceFactory {
    config: Config,
}

impl AsyncMakeService for MyAsyncServiceFactory {
    type Service = MyAsyncService;
    type Error = Infallible;

    async fn make_via_ref(&self, old: Option<&Self::Service>) -> Result<Self::Service, Self::Error> {
        Ok(match old {
            Some(existing) => MyAsyncService {
                connection_pool: existing.connection_pool.clone(),
                config: self.config.clone(),
            },
            None => MyAsyncService {
                connection_pool: AsyncConnectionPool::new().await,
                config: self.config.clone(),
            },
        })
    }
}

In this example, MyAsyncServiceFactory implements AsyncMakeService to create MyAsyncService instances asynchronously, demonstrating how to reuse a connection pool or create a new one when needed.

Required Associated Types§

Source

type Service

The type of service this factory creates.

Source

type Error

The type of error that can occur during service creation.

Required Methods§

Source

fn make_via_ref( &self, old: Option<&Self::Service>, ) -> impl Future<Output = Result<Self::Service, Self::Error>>

Asynchronously creates a new service, optionally using an existing service as a reference.

This method allows for sophisticated asynchronous service creation logic that can reuse state from an existing service instance.

§Arguments
  • old - An optional reference to an existing service instance.
§Returns

A Future that resolves to a Result containing either the newly created service or an error.

Provided Methods§

Source

fn make(&self) -> impl Future<Output = Result<Self::Service, Self::Error>>

Asynchronously creates a new service without referencing an existing one.

This is a convenience method that calls make_via_ref with None.

§Returns

A Future that resolves to a Result containing either the newly created service or an error.

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.

Implementations on Foreign Types§

Source§

impl<T: AsyncMakeService + ?Sized> AsyncMakeService for &T

Source§

type Service = <T as AsyncMakeService>::Service

Source§

type Error = <T as AsyncMakeService>::Error

Source§

async fn make_via_ref( &self, old: Option<&Self::Service>, ) -> Result<Self::Service, Self::Error>

Source§

impl<T: AsyncMakeService + ?Sized> AsyncMakeService for Box<T>

Source§

type Service = <T as AsyncMakeService>::Service

Source§

type Error = <T as AsyncMakeService>::Error

Source§

async fn make_via_ref( &self, old: Option<&Self::Service>, ) -> Result<Self::Service, Self::Error>

Source§

impl<T: AsyncMakeService + ?Sized> AsyncMakeService for Arc<T>

Source§

type Service = <T as AsyncMakeService>::Service

Source§

type Error = <T as AsyncMakeService>::Error

Source§

async fn make_via_ref( &self, old: Option<&Self::Service>, ) -> Result<Self::Service, Self::Error>

Implementors§