Skip to main content

InjectableViewSet

Trait InjectableViewSet 

Source
pub trait InjectableViewSet: ViewSet {
    // Provided methods
    fn resolve<'life0, 'life1, 'async_trait, T>(
        &'life0 self,
        request: &'life1 Request,
    ) -> Pin<Box<dyn Future<Output = Result<T, Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             T: Injectable + Clone + Send + Sync + 'static + 'async_trait,
             Self: Sync + 'async_trait { ... }
    fn resolve_uncached<'life0, 'life1, 'async_trait, T>(
        &'life0 self,
        request: &'life1 Request,
    ) -> Pin<Box<dyn Future<Output = Result<T, Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             T: Injectable + Clone + Send + Sync + 'static + 'async_trait,
             Self: Sync + 'async_trait { ... }
    fn try_resolve<'life0, 'life1, 'async_trait, T>(
        &'life0 self,
        request: &'life1 Request,
    ) -> Pin<Box<dyn Future<Output = Option<T>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             T: Injectable + Clone + Send + Sync + 'static + 'async_trait,
             Self: Sync + 'async_trait { ... }
}
Available on non-WebAssembly only.
Expand description

Extension trait for ViewSets that enables dependency injection

This trait is automatically implemented for all types that implement ViewSet. It provides helper methods to resolve dependencies from the request’s DI context.

§Examples

use reinhardt_views::viewsets::{InjectableViewSet, ModelViewSet, ViewSet};
use std::sync::Arc;

struct UserViewSet {
    basename: String,
}

impl UserViewSet {
    async fn handle_list(&self, request: Request) -> Result<Response> {
        // Resolve with caching (default)
        let db: Arc<DatabaseConnection> = self.resolve(&request).await?;

        // Resolve without caching
        let fresh_config: Config = self.resolve_uncached(&request).await?;

        // Use dependencies...
        Ok(Response::ok())
    }
}

Provided Methods§

Source

fn resolve<'life0, 'life1, 'async_trait, T>( &'life0 self, request: &'life1 Request, ) -> Pin<Box<dyn Future<Output = Result<T, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, T: Injectable + Clone + Send + Sync + 'static + 'async_trait, Self: Sync + 'async_trait,

Resolve a dependency from the request’s DI context with caching

This method extracts the InjectionContext from the request and resolves the requested dependency type. The resolved dependency is cached for the duration of the request.

§Errors

Returns an error if:

  • The DI context is not set on the request (router misconfiguration)
  • The dependency cannot be resolved (not registered, circular dependency, etc.)
§Examples
let db: Arc<DatabaseConnection> = self.resolve(&request).await?;
Source

fn resolve_uncached<'life0, 'life1, 'async_trait, T>( &'life0 self, request: &'life1 Request, ) -> Pin<Box<dyn Future<Output = Result<T, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, T: Injectable + Clone + Send + Sync + 'static + 'async_trait, Self: Sync + 'async_trait,

Resolve a dependency from the request’s DI context without caching

This method is similar to resolve() but creates a fresh instance of the dependency each time, bypassing the cache.

Use this when you need:

  • A fresh instance that won’t share state with other resolutions
  • To avoid caching for dependencies with mutable state
§Errors

Returns an error if:

  • The DI context is not set on the request (router misconfiguration)
  • The dependency cannot be resolved (not registered, circular dependency, etc.)
§Examples
let fresh_service: MyService = self.resolve_uncached(&request).await?;
Source

fn try_resolve<'life0, 'life1, 'async_trait, T>( &'life0 self, request: &'life1 Request, ) -> Pin<Box<dyn Future<Output = Option<T>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, T: Injectable + Clone + Send + Sync + 'static + 'async_trait, Self: Sync + 'async_trait,

Try to resolve a dependency, returning None if DI context is not available

This is useful for optional dependencies or when you want to gracefully handle the case where DI is not configured.

§Examples
if let Some(cache) = self.try_resolve::<CacheService>(&request).await {
    // Use cache
} else {
    // Fallback without cache
}

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§

Source§

impl<V> InjectableViewSet for V
where V: ViewSet,